Class: ActsAsTable::Mapper::ForeignKey

Inherits:
Base
  • Object
show all
Defined in:
lib/acts_as_table/mapper.rb

Overview

ActsAsTable mapper object for an instance of the ForeignKey class.

Instance Method Summary collapse

Constructor Details

#initialize(row_model, column_model_by_key, record_model, method_name, position_or_key = nil, **options) {|foreign_key| ... } ⇒ ActsAsTable::Mapper::ForeignKey

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

Parameters:

Options Hash (**options):

  • :default (Boolean)
  • :primary_key (#to_s)

Yield Parameters:

Yield Returns:

  • (void)


61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

Instance Method Details

#map(from, **options) ⇒ ActsAsTable::Mapper::ForeignKey

Note:

Target values for regular expressions may refer to capture groups.

Builds a new instance of the ForeignKeyMap class.

Examples:

Map from a string to another string.

map 'source', to: 'target'

Map from a regular expression to a capture group.

map /City:\s+(.+)/i, to: '\1'

Parameters:

  • from (String, Regexp)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :to (String)

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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