Class: Lutaml::Model::MappingRule

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

Direct Known Subclasses

KeyValueMappingRule, XmlMappingRule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, to:, render_nil: false, render_default: false, with: {}, attribute: false, delegate: nil, root_mappings: nil) ⇒ MappingRule

Returns a new instance of MappingRule.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lutaml/model/mapping_rule.rb', line 12

def initialize(
  name,
  to:,
  render_nil: false,
  render_default: false,
  with: {},
  attribute: false,
  delegate: nil,
  root_mappings: nil
)
  @name = name
  @to = to
  @render_nil = render_nil
  @render_default = render_default
  @custom_methods = with
  @attribute = attribute
  @delegate = delegate
  @root_mappings = root_mappings
end

Instance Attribute Details

#attributeObject (readonly) Also known as: attribute?

Returns the value of attribute attribute.



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

def attribute
  @attribute
end

#custom_methodsObject (readonly)

Returns the value of attribute custom_methods.



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

def custom_methods
  @custom_methods
end

#delegateObject (readonly)

Returns the value of attribute delegate.



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

def delegate
  @delegate
end

#nameObject (readonly) Also known as: from

Returns the value of attribute name.



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

def name
  @name
end

#render_defaultObject (readonly) Also known as: render_default?

Returns the value of attribute render_default.



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

def render_default
  @render_default
end

#render_nilObject (readonly) Also known as: render_nil?

Returns the value of attribute render_nil.



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

def render_nil
  @render_nil
end

#toObject (readonly)

Returns the value of attribute to.



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

def to
  @to
end

Instance Method Details

#deep_dupObject

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/lutaml/model/mapping_rule.rb', line 83

def deep_dup
  raise NotImplementedError, "Subclasses must implement `deep_dup`."
end

#deserialize(model, value, attributes, mapper_class = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lutaml/model/mapping_rule.rb', line 61

def deserialize(model, value, attributes, mapper_class = nil)
  if custom_methods[:from]
    mapper_class.new.send(custom_methods[:from], model, value) unless value.nil?
  elsif delegate
    if model.public_send(delegate).nil?
      model.public_send(:"#{delegate}=", attributes[delegate].type.new)
    end

    model.public_send(delegate).public_send(:"#{to}=", value)
  else
    model.public_send(:"#{to}=", value)
  end
end

#multiple_mappings?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/lutaml/model/mapping_rule.rb', line 79

def multiple_mappings?
  name.is_a?(Array)
end

#serialize(model, parent = nil, doc = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/lutaml/model/mapping_rule.rb', line 53

def serialize(model, parent = nil, doc = nil)
  if custom_methods[:to]
    model.send(custom_methods[:to], model, parent, doc)
  else
    to_value_for(model)
  end
end

#serialize_attribute(model, element, doc) ⇒ Object



37
38
39
40
41
# File 'lib/lutaml/model/mapping_rule.rb', line 37

def serialize_attribute(model, element, doc)
  if custom_methods[:to]
    model.send(custom_methods[:to], model, element, doc)
  end
end

#to_value_for(model) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/lutaml/model/mapping_rule.rb', line 43

def to_value_for(model)
  if delegate
    model.public_send(delegate).public_send(to)
  else
    return if to.nil?

    model.public_send(to)
  end
end

#using_custom_methods?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/lutaml/model/mapping_rule.rb', line 75

def using_custom_methods?
  !custom_methods.empty?
end