Class: SimpleAttributeMapper::Mapper
- Inherits:
-
Object
- Object
- SimpleAttributeMapper::Mapper
- Defined in:
- lib/simple_attribute_mapper/mapper.rb
Instance Attribute Summary collapse
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
Instance Method Summary collapse
-
#initialize(mappings = {}) ⇒ Mapper
constructor
A new instance of Mapper.
- #map(source, target_class) ⇒ Object
- #map_attributes(source, target_instance) ⇒ Object
- #map_matching_attributes(source, target) ⇒ Object
- #map_specified_attributes(source, target) ⇒ Object
- #resolve_attribute_value(mapping_key, source) ⇒ Object
- #resolve_nested_value(mapping_key, source) ⇒ Object
Constructor Details
#initialize(mappings = {}) ⇒ Mapper
Returns a new instance of Mapper.
3 4 5 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 3 def initialize(mappings = {}) @mappings = mappings end |
Instance Attribute Details
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
7 8 9 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 7 def mappings @mappings end |
Instance Method Details
#map(source, target_class) ⇒ Object
9 10 11 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 9 def map(source, target_class) map_attributes(source, target_class.new) end |
#map_attributes(source, target_instance) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 13 def map_attributes(source, target_instance) raise UnMappableError.new("source has no attributes") unless source.respond_to?(:attributes) map_matching_attributes(source, target_instance) map_specified_attributes(source, target_instance) target_instance end |
#map_matching_attributes(source, target) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 22 def map_matching_attributes(source, target) source.attributes.each do |attribute| attribute_writer = "#{attribute[0]}=".to_sym attribute_value = attribute[1] if target.respond_to?(attribute_writer) target.send(attribute_writer, attribute_value) end end end |
#map_specified_attributes(source, target) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 33 def map_specified_attributes(source, target) mappings.each do |source_attribute, target_attribute| attribute_writer = "#{target_attribute}=".to_sym attribute_value = resolve_attribute_value(source_attribute, source) # puts "#{attribute_writer} - #{attribute_value}" target.send(attribute_writer, attribute_value) end end |
#resolve_attribute_value(mapping_key, source) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 42 def resolve_attribute_value(mapping_key, source) if mapping_key.is_a?(Symbol) source.send(mapping_key) elsif mapping_key.is_a?(Array) resolve_nested_value(mapping_key, source) elsif mapping_key.is_a?(Proc) mapping_key.call(source) else raise "Fatal, mapping for '#{mapping_key}' is unknown" end end |
#resolve_nested_value(mapping_key, source) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/simple_attribute_mapper/mapper.rb', line 54 def resolve_nested_value(mapping_key, source) root_key = mapping_key.shift current = source.send(root_key) mapping_key.each do |nested| value = current.send(nested) if nested == mapping_key.last return value end current = value end end |