Class: Economic::Entity::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/economic/entity/mapper.rb

Overview

Entity::Mapper provides a generic way of building SOAP data structures for entities.

Based on an Entity and a set of rules that define the fields to map to, it returns a Hash named and ordered properly, ready for passing to the endpoint as SOAP data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, fields = []) ⇒ Mapper

Returns a new instance of Mapper.



13
14
15
16
# File 'lib/economic/entity/mapper.rb', line 13

def initialize(entity, fields = [])
  @entity = entity
  @fields = fields
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



11
12
13
# File 'lib/economic/entity/mapper.rb', line 11

def entity
  @entity
end

#fieldsObject (readonly)

Returns the value of attribute fields.



11
12
13
# File 'lib/economic/entity/mapper.rb', line 11

def fields
  @fields
end

Instance Method Details

#to_hashObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/economic/entity/mapper.rb', line 18

def to_hash
  data = {}

  fields.each do |field, method, formatter, required|
    value = entity.send(method)
    present = present?(value)

    if present || required
      value = formatter.call(value) if formatter
      data[field] = value
    end
  end

  data
end