Class: Lutaml::Model::ModelMappingRule

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/mapping/model_mapping_rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, from:, to:, transform: nil, reverse_transform: nil, collection: false, mapping: nil) ⇒ ModelMappingRule

Returns a new instance of ModelMappingRule.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lutaml/model/mapping/model_mapping_rule.rb', line 10

def initialize(
  name = nil,
  from:,
  to:,
  transform: nil,
  reverse_transform: nil,
  collection: false,
  mapping: nil
)
  @name = name
  @from = from
  @to = to
  @transform = transform
  @reverse_transform = transform.is_a?(Class) && reverse_transform.nil? ? transform : reverse_transform
  @collection = collection
  @mapping = mapping
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



4
5
6
# File 'lib/lutaml/model/mapping/model_mapping_rule.rb', line 4

def collection
  @collection
end

#fromObject (readonly)

Returns the value of attribute from.



4
5
6
# File 'lib/lutaml/model/mapping/model_mapping_rule.rb', line 4

def from
  @from
end

#mappingObject (readonly)

Returns the value of attribute mapping.



4
5
6
# File 'lib/lutaml/model/mapping/model_mapping_rule.rb', line 4

def mapping
  @mapping
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/lutaml/model/mapping/model_mapping_rule.rb', line 4

def name
  @name
end

#toObject (readonly)

Returns the value of attribute to.



4
5
6
# File 'lib/lutaml/model/mapping/model_mapping_rule.rb', line 4

def to
  @to
end

Instance Method Details

#transform_value(transformer, attr, value, reverse: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lutaml/model/mapping/model_mapping_rule.rb', line 28

def transform_value(transformer, attr, value, reverse: false)
  if @mapping
    return @mapping.process_mappings(value, reverse: reverse)
  end

  if @reverse_transform && reverse
    transform_call(transformer, @reverse_transform, value,
                   :reverse_transform)
  elsif @transform
    transform_call(transformer, @transform, value, :transform)
  else
    attr.type.cast(value)
  end
end