class ActsAsTable::Mapper::ForeignKey

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

Public Class Methods

new(row_model, column_model_by_key, record_model, method_name, position_or_key = nil, **options, &block) click to toggle source

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

@param [ActsAsTable::RowModel] row_model @param [Hash<Symbol, ActsAsTable::ColumnModel>] column_model_by_key @param [ActsAsTable::RecordModel] record_model @param [#to_s] method_name @param [Integer, Symbol, nil] position_or_key @param [Hash<Symbol, Object>] options @option options [Boolean] :default @option options [#to_s] :primary_key @yieldparam [ActsAsTable::Mapper::ForeignKey] foreign_key @yieldreturn [void] @return [ActsAsTable::Mapper::ForeignKey]

Calls superclass method ActsAsTable::Mapper::Base::new
# File lib/acts_as_table/mapper.rb, line 61
def initialize(row_model, column_model_by_key, record_model, method_name, position_or_key = nil, **options, &block)
  options.assert_valid_keys(:default, :primary_key)

  @row_model, @column_model_by_key, @record_model = row_model, column_model_by_key, record_model

  @foreign_key = @record_model.foreign_keys.build(method_name: method_name, primary_key: options[:primary_key] || 'id', default_value: options[:default]) do |foreign_key|
    unless position_or_key.nil?
      foreign_key.column_model = position_or_key.is_a?(::Symbol) ? @column_model_by_key[position_or_key] : @row_model.column_models[position_or_key - 1]
    end
  end

  super(&block)
end

Public Instance Methods

map(from, **options) click to toggle source

Builds a new instance of the {ActsAsTable::ForeignKeyMap} class.

@note Target values for regular expressions may refer to capture groups.

@param [String, Regexp] from @param [Hash<Symbol, Object>] options @option options [String] :to @return [ActsAsTable::Mapper::ForeignKey]

@example Map from a string to another string.

map 'source', to: 'target'

@example Map from a regular expression to a capture group.

map /City:\s+(.+)/i, to: '\1'
# File lib/acts_as_table/mapper.rb, line 90
def map(from, **options)
  options.assert_valid_keys(:to)

  @foreign_key.foreign_key_maps.build({
    source_value: from.is_a?(::Regexp) ? from.source : from.to_s,
    target_value: options[:to].to_s,
    regexp: from.is_a?(::Regexp),
    extended: from.is_a?(::Regexp) && ((from.options & ::Regexp::EXTENDED) != 0),
    ignore_case: from.is_a?(::Regexp) && ((from.options & ::Regexp::IGNORECASE) != 0),
    multiline: from.is_a?(::Regexp) && ((from.options & ::Regexp::MULTILINE) != 0),
  })

  self
end