Class: Stockboy::AttributeMap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/stockboy/attribute_map.rb

Overview

Table of attributes for finding corresponding field/attribute translations

Instance Method Summary collapse

Constructor Details

#initialize(rows = {}, &block) ⇒ AttributeMap

Initialize a new attribute map



37
38
39
40
41
42
43
44
# File 'lib/stockboy/attribute_map.rb', line 37

def initialize(rows={}, &block)
  @map = rows
  @unmapped = Hash.new
  if block_given?
    DSL.new(self).instance_eval(&block)
  end
  freeze
end

Instance Method Details

#[](key) ⇒ Attribute

Retrieve an attribute by symbolic name

Parameters:

  • key (Symbol)

Returns:



51
52
53
# File 'lib/stockboy/attribute_map.rb', line 51

def [](key)
  @map[key]
end

#attribute_from(key) ⇒ Attribute

Fetch the attribute corresponding to the source field name

Parameters:

  • key (String)

Returns:



60
61
62
# File 'lib/stockboy/attribute_map.rb', line 60

def attribute_from(key)
  find { |a| a.from == key } or @unmapped[key] ||= Attribute.new(nil, key, nil)
end

#each(*args) {|| ... } ⇒ Enumerator

Enumerate over attributes

Yield Parameters:

Returns:

  • (Enumerator)


69
70
71
# File 'lib/stockboy/attribute_map.rb', line 69

def each(*args, &block)
  @map.values.each(*args, &block)
end