Class: Lutaml::Model::MappingRule
- Inherits:
-
Object
- Object
- Lutaml::Model::MappingRule
- Defined in:
- lib/lutaml/model/mapping_rule.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attribute ⇒ Object
(also: #attribute?)
readonly
Returns the value of attribute attribute.
-
#custom_methods ⇒ Object
readonly
Returns the value of attribute custom_methods.
-
#delegate ⇒ Object
readonly
Returns the value of attribute delegate.
-
#name ⇒ Object
(also: #from)
readonly
Returns the value of attribute name.
-
#render_default ⇒ Object
(also: #render_default?)
readonly
Returns the value of attribute render_default.
-
#render_nil ⇒ Object
(also: #render_nil?)
readonly
Returns the value of attribute render_nil.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
- #deep_dup ⇒ Object
- #deserialize(model, value, attributes, mapper_class = nil) ⇒ Object
-
#initialize(name, to:, render_nil: false, render_default: false, with: {}, attribute: false, delegate: nil, root_mappings: nil) ⇒ MappingRule
constructor
A new instance of MappingRule.
- #multiple_mappings? ⇒ Boolean
- #serialize(model, parent = nil, doc = nil) ⇒ Object
- #serialize_attribute(model, element, doc) ⇒ Object
- #to_value_for(model) ⇒ Object
- #using_custom_methods? ⇒ Boolean
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
#attribute ⇒ Object (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_methods ⇒ Object (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 |
#delegate ⇒ Object (readonly)
Returns the value of attribute delegate.
4 5 6 |
# File 'lib/lutaml/model/mapping_rule.rb', line 4 def delegate @delegate end |
#name ⇒ Object (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_default ⇒ Object (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_nil ⇒ Object (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 |
#to ⇒ Object (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_dup ⇒ Object
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
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
75 76 77 |
# File 'lib/lutaml/model/mapping_rule.rb', line 75 def using_custom_methods? !custom_methods.empty? end |