class Economic::Entity::Mapper

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.

Attributes

entity[R]
fields[R]

Public Class Methods

new(entity, fields = []) click to toggle source
# File lib/economic/entity/mapper.rb, line 13
def initialize(entity, fields = [])
  @entity = entity
  @fields = fields
end

Public Instance Methods

to_hash() click to toggle source
# 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

Private Instance Methods

present?(value) click to toggle source
# File lib/economic/entity/mapper.rb, line 36
def present?(value)
  !(
    (value.respond_to?(:blank?) && value.blank?) ||
    (value.respond_to?(:empty?) && value.empty?)
  )
end