class ActsAsTable::Mapper::RowModel

ActsAsTable mapper object for an instance of the {ActsAsTable::RowModel} class.

@see ActsAsTable::RowModel#draw

Constants

COLUMN_MODEL_SEPARATOR_

@return [String]

WHITESPACE_PATTERN_

@return [String]

WHITESPACE_REPLACEMENT_

@return [String]

Public Class Methods

new(row_model, &block) click to toggle source

Returns a new ActsAsTable mapper object an instance of the {ActsAsTable::RowModel} class.

@param [ActsAsTable::RowModel] row_model @yieldparam [ActsAsTable::Mapper::RowModel] row_model @yieldreturn [void] @return [ActsAsTable::Mapper::RowModel]

Calls superclass method ActsAsTable::Mapper::Base::new
# File lib/acts_as_table/mapper.rb, line 348
def initialize(row_model, &block)
  @row_model, @column_model_by_key = row_model, {}

  super(&block)
end

Public Instance Methods

columns=(object) click to toggle source

Builds new ActsAsTable column models in the scope of this ActsAsTable row model and caches them by key, if given.

@param [#each_pair, each, to_s] object @return [ActsAsTable::Mapper::RowModel]

# File lib/acts_as_table/mapper.rb, line 358
def columns=(object)
  # @return [Integer]
  column_models_count = @row_model.column_models.size

  # @return [void]
  ::Enumerator.new { |enumerator|
    _dfs(object) { |path|
      enumerator << path
    }
  }.each do |path|
    # @return [Symbol, nil]
    key = path[-1].is_a?(::Symbol) ? path.pop : nil

    column_models_count += 1

    # @return [ActsAsTable::ColumnModel]
    column_model = @row_model.column_models.build(position: column_models_count, **_to_column_model_attributes(path))

    unless key.nil?
      @column_model_by_key[key] = column_model
    end
  end

  self
end
model(class_name, &block) click to toggle source

Returns a new ActsAsTable mapper object an instance of the {ActsAsTable::RecordModel} class.

@param [#to_s] class_name @yieldparam [ActsAsTable::Mapper::RecordModel] record_model @yieldreturn [void] @return [ActsAsTable::Mapper::RecordModel]

# File lib/acts_as_table/mapper.rb, line 400
def model(class_name, &block)
  ActsAsTable::Mapper::RecordModel.new(@row_model, @column_model_by_key, class_name, &block)
end
root_model=(target) click to toggle source

Set the root ActsAsTable record model for this ActsAsTable row model.

@param [ActsAsTable::Mapper::RecordModel] target @return [ActsAsTable::Mapper::RowModel]

# File lib/acts_as_table/mapper.rb, line 388
def root_model=(target)
  @row_model.root_record_model = target.send(:instance_variable_get, :@record_model)

  self
end

Private Instance Methods

_dfs(object, path = [], &block) click to toggle source

Performs a depth-first search of the given object and yields each path from root to leaf.

@param [#each_pair, each, to_s] object @param [Array<#to_s>] path @yieldparam [Array<#to_s>] path @yieldreturn [void] @return [void]

# File lib/acts_as_table/mapper.rb, line 413
def _dfs(object, path = [], &block)
  unless block.nil?
    if object.respond_to?(:each_pair)
      object.each_pair do |pair|
        # @return [Array<#to_s>]
        key, new_object = *pair

        # @return [Array<#to_s>]
        new_path = path + [key]

        _dfs(new_object, new_path, &block)
      end
    elsif object.respond_to?(:each)
      object.each do |new_object|
        _dfs(new_object, path, &block)
      end
    else
      # @return [Array<#to_s>]
      new_path = path + [object]

      case block.arity
        when 1 then block.call(new_path)
        else new_path.instance_exec(&block)
      end
    end
  end

  return
end
_to_column_model_attributes(path) click to toggle source

Returns the +ActsAsTable::ColumnModel#attributes+ for the given path.

@param [Array<#to_s>] path @return [Hash<Symbol, Object>]

# File lib/acts_as_table/mapper.rb, line 456
def _to_column_model_attributes(path)
  {
    name: path.collect(&:to_s).collect(&:strip).collect { |s| s.strip.gsub(WHITESPACE_PATTERN_, WHITESPACE_REPLACEMENT_) }.join(COLUMN_MODEL_SEPARATOR_),
    separator: COLUMN_MODEL_SEPARATOR_,
  }
end