Class: Intermodal::Mapping::Mapper

Inherits:
Object
  • Object
show all
Extended by:
DSL::PresentationHelpers
Defined in:
lib/intermodal/mapping/mapper.rb

Direct Known Subclasses

Acceptor, Presenter

Constant Summary collapse

INCLUDE_NILS =
lambda { |h, resource, mapped_from, mapped_to| h[mapped_from] = map_attribute(resource, mapped_to) }
EXCLUDE_NILS =
lambda { |h, resource, mapped_from, mapped_to| a = map_attribute(resource, mapped_to); h[mapped_from] = a unless a.nil? }

Class Method Summary collapse

Methods included from DSL::PresentationHelpers

attribute, helper, map_to, maybe, params, presenter, to_datetime, to_presentation

Class Method Details

.call(resource, options = {}) ⇒ Object

:api: Mapper should respond to .call()



17
18
19
# File 'lib/intermodal/mapping/mapper.rb', line 17

def call(resource, options = {})
  map_attributes(resource, options[:scope] || :default)
end

.exclude_properties(*args) ⇒ Object

:public: Blacklists properties



12
13
14
# File 'lib/intermodal/mapping/mapper.rb', line 12

def exclude_properties(*args)
  (self._exclude_properties ||= []).push *(args.map(&:to_s))
end

.map_attribute(resource, mapped_to) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/intermodal/mapping/mapper.rb', line 54

def map_attribute(resource, mapped_to)
  case mapped_to
  when Symbol then resource[mapped_to]
  when Proc then
    if mapped_to.arity == 1
      mapped_to.call(resource)
    else
      mapped_to.call()
    end
  when Array then mapped_to.inject({}) { |m, _element| m.tap { |_m| _m[_element] = map_attribute(resource, _element) } }
  end
end

.map_attributes(resource, scope = :default) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/intermodal/mapping/mapper.rb', line 42

def map_attributes(resource, scope = :default)
  raise "Scope: #{scope} not found when mapping #{resource.inspect}" unless self._property_mappings[scope]
  mappings = if scope == :default
               self._property_mappings[:default]
             else
               self._property_mappings[:default] + self._property_mappings[scope]
             end
  mappings.inject({}) do |m, p|
    m.tap { |h| self._mapping_strategy[h, resource, p[0], p[1]] }
  end.except(*self._exclude_properties)
end

.maps(property, opts = {}) ⇒ Object

Examples:

maps :name
maps :name, :with => lambda { |o| o.name.camelize }
maps :name, :with => [ :first_name, :last_name, :suffix, :prefix ]
maps :name, :scope => :named


33
34
35
36
37
38
39
40
# File 'lib/intermodal/mapping/mapper.rb', line 33

def maps(property, opts = {})
  scopes = Array(opts[:scope] || :default)
  scopes.each do |scope|
    # DEPRECATION: option :as
    remapped_property = opts[:with] || opts[:as] || property
    property_mapping(scope).push [property, remapped_property]
  end
end

.presentersObject



67
68
69
# File 'lib/intermodal/mapping/mapper.rb', line 67

def presenters
  self.api.presenters
end

.property_mapping(scope = :default) ⇒ Object



21
22
23
24
# File 'lib/intermodal/mapping/mapper.rb', line 21

def property_mapping(scope = :default)
  self._property_mappings ||= {}
  self._property_mappings[scope] ||= []
end