Class: Lutaml::Model::ModelMapping
- Inherits:
-
Object
- Object
- Lutaml::Model::ModelMapping
- Defined in:
- lib/lutaml/model/mapping/model_mapping.rb
Instance Attribute Summary collapse
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
Instance Method Summary collapse
-
#initialize(source = nil, target = nil, transformer = nil) ⇒ ModelMapping
constructor
A new instance of ModelMapping.
- #map(from: nil, to: nil, transform: nil, reverse_transform: nil, collection: false, &block) ⇒ Object
- #map_each(from: nil, to: nil, transform: nil, reverse_transform: nil) ⇒ Object
- #process_mappings(input, reverse: false) ⇒ Object
Constructor Details
#initialize(source = nil, target = nil, transformer = nil) ⇒ ModelMapping
Returns a new instance of ModelMapping.
8 9 10 11 12 13 |
# File 'lib/lutaml/model/mapping/model_mapping.rb', line 8 def initialize(source = nil, target = nil, transformer = nil) @mappings = {} @source = source @target = target @transformer = transformer end |
Instance Attribute Details
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
6 7 8 |
# File 'lib/lutaml/model/mapping/model_mapping.rb', line 6 def mappings @mappings end |
Instance Method Details
#map(from: nil, to: nil, transform: nil, reverse_transform: nil, collection: false, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lutaml/model/mapping/model_mapping.rb', line 15 def map( from: nil, to: nil, transform: nil, reverse_transform: nil, collection: false, &block ) mapping_name = name_for_mapping(from, to) from_attr = @source.attributes[from.to_sym] to_attr = @target.attributes[to.to_sym] validate!(mapping_name, from_attr, to_attr) @mappings[mapping_name] = ModelMappingRule.new( from: from_attr, to: to_attr, transform: transform, reverse_transform: reverse_transform, collection: collection, mapping: if block self.class.new(from_attr.type, to_attr.type, @transformer) end, ) @mappings[mapping_name].mapping.instance_eval(&block) if block end |
#map_each(from: nil, to: nil, transform: nil, reverse_transform: nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lutaml/model/mapping/model_mapping.rb', line 41 def map_each( from: nil, to: nil, transform: nil, reverse_transform: nil, & ) map( from: from, to: to, transform: transform, reverse_transform: reverse_transform, collection: true, & ) end |
#process_mappings(input, reverse: false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lutaml/model/mapping/model_mapping.rb', line 58 def process_mappings(input, reverse: false) return input if Utils.blank?(input) transformed = {} @mappings.each_value do |rule| from_attr, to_attr = transform_attributes(rule, reverse: reverse) next if from_attr.nil? || to_attr.nil? value = input.send(from_attr.name) value = transformed_value(value, rule, from_attr, to_attr, reverse: reverse) transformed[to_attr.name] = value end transformed end |