Class: HashMath::Mapper::Mapping

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hash_math/mapper/mapping.rb

Overview

Represents one complete configuration for mapping one key-value pair to one lookup.

Example:


mapping = Mapper.make(

lookup: { name: :patient_statuses, by: :name },
value: :status,
set: :patient_status_id,
with: :id

).add(id: 1, name: ‘active’).add(id: 2, name: ‘inactive’)

patient = { id: 1, code: ‘active’ } mapped_patient = mapping.map!(patient)


mapped_patient now equals: { id: 1, code: ‘active’, patient_status_id: 1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup:, value:, set:, with:) ⇒ Mapping

lookup: can either be a Mapper#Lookup instance or a hash with the attributes to initialize for a Mapper#Lookup instance. value: the key to use to get the ‘value’ from the object to lookup. set: the key to set once the lookup record is identified. with: the key use, on the lookup, to get the new value.



43
44
45
46
47
48
49
50
# File 'lib/hash_math/mapper/mapping.rb', line 43

def initialize(lookup:, value:, set:, with:)
  @lookup = Lookup.make(lookup)
  @value  = value
  @set    = set
  @with   = with

  freeze
end

Instance Attribute Details

#lookupObject (readonly)

Returns the value of attribute lookup.



34
35
36
# File 'lib/hash_math/mapper/mapping.rb', line 34

def lookup
  @lookup
end

#setObject (readonly)

Returns the value of attribute set.



34
35
36
# File 'lib/hash_math/mapper/mapping.rb', line 34

def set
  @set
end

#valueObject (readonly)

Returns the value of attribute value.



34
35
36
# File 'lib/hash_math/mapper/mapping.rb', line 34

def value
  @value
end

#withObject (readonly)

Returns the value of attribute with.



34
35
36
# File 'lib/hash_math/mapper/mapping.rb', line 34

def with
  @with
end

Instance Method Details

#add(object) ⇒ Object

:nodoc:



56
57
58
# File 'lib/hash_math/mapper/mapping.rb', line 56

def add(object) # :nodoc:
  tap { lookup.add(object) }
end

#add_each(array) ⇒ Object

:nodoc:



52
53
54
# File 'lib/hash_math/mapper/mapping.rb', line 52

def add_each(array) # :nodoc:
  tap { lookup.add_each(array) }
end

#map!(hash) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
# File 'lib/hash_math/mapper/mapping.rb', line 60

def map!(hash) # :nodoc:
  lookup_value  = proc_or_brackets(hash, value)
  lookup_object = lookup.get(lookup_value)
  hash[set]     = proc_or_brackets(lookup_object, with)

  self
end