class Stockboy::AttributeMap

Table of attributes for finding corresponding field/attribute translations

Public Class Methods

new(rows={}, &block) click to toggle source

Initialize a new attribute map

# File lib/stockboy/attribute_map.rb, line 26
def initialize(rows={}, &block)
  @map = rows
  @unmapped = Hash.new
  if block_given?
    DSL.new(self).instance_eval(&block)
  end
end

Public Instance Methods

[](key) click to toggle source

Retrieve an attribute by symbolic name

@param [Symbol] key @return [Attribute]

# File lib/stockboy/attribute_map.rb, line 39
def [](key)
  @map[key]
end
attribute_from(key) click to toggle source

Fetch the attribute corresponding to the source field name

@param [String] key @return [Attribute]

# File lib/stockboy/attribute_map.rb, line 65
def attribute_from(key)
  find { |a| a.from == key } or @unmapped[key] ||= Attribute.new(nil, key, nil)
end
each(*args, &block) click to toggle source

Enumerate over attributes

@return [Enumerator] @yieldparam [Attribute]

# File lib/stockboy/attribute_map.rb, line 74
def each(*args, &block)
  @map.values.each(*args, &block)
end
insert(key, opts={}) click to toggle source

Add or replace a mapped attribute

@param [Symbol] key Name of the output attribute @param [Hash] opts @option opts [String] from Name of input field from reader @option opts [Array,Proc,Translator] as One or more translators

# File lib/stockboy/attribute_map.rb, line 50
def insert(key, opts={})
  to = key.to_sym
  from = opts.fetch(:from, key)
  from = from.to_s.freeze if from.is_a? Symbol
  translators = Array(opts[:as]).map { |t| Translations.translator_for(to, t) }
  ignore_condition = opts[:ignore]
  define_singleton_method(key) { @map[key] }
  @map[key] = Attribute.new(to, from, translators, ignore_condition)
end