Class: HashMath::Mapper::Lookup

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#byObject (readonly)

Returns the value of attribute by.



19
20
21
# File 'lib/hash_math/mapper/lookup.rb', line 19

def by
  @by
end

#nameObject (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