Class: HashMath::Mapper::Lookup
- Inherits:
-
Object
- Object
- HashMath::Mapper::Lookup
- Defined in:
- lib/hash_math/mapper/lookup.rb
Overview
A Lookup instance maintains its own list of objects using its own key extraction method, called ‘by’ which will be used to extract the key’s value for the lookup. If ‘by’ is a Proc then it will be called when extracting a new lookup record’s lookup value. If it is anything other than a Proc and it will call #[] on the object.
Instance Attribute Summary collapse
-
#by ⇒ Object
readonly
Returns the value of attribute by.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#add(object) ⇒ Object
:nodoc:.
-
#add_each(array) ⇒ Object
:nodoc:.
-
#get(value) ⇒ Object
:nodoc:.
-
#initialize(name:, by:) ⇒ Lookup
constructor
A new instance of Lookup.
Constructor Details
#initialize(name:, by:) ⇒ Lookup
Returns a new instance of Lookup.
21 22 23 24 25 26 27 |
# File 'lib/hash_math/mapper/lookup.rb', line 21 def initialize(name:, by:) @name = name @by = by @objects = {} freeze end |
Instance Attribute Details
#by ⇒ Object (readonly)
Returns the value of attribute by.
19 20 21 |
# File 'lib/hash_math/mapper/lookup.rb', line 19 def by @by end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/hash_math/mapper/lookup.rb', line 19 def name @name end |
Instance Method Details
#add(object) ⇒ Object
:nodoc:
33 34 35 36 37 38 39 |
# File 'lib/hash_math/mapper/lookup.rb', line 33 def add(object) # :nodoc: id = proc_or_brackets(object, by) objects[id] = object self end |
#add_each(array) ⇒ Object
:nodoc:
29 30 31 |
# File 'lib/hash_math/mapper/lookup.rb', line 29 def add_each(array) # :nodoc: tap { array.each { |o| add(o) } } end |
#get(value) ⇒ Object
:nodoc:
41 42 43 |
# File 'lib/hash_math/mapper/lookup.rb', line 41 def get(value) # :nodoc: objects[value] end |