Class: Lutaml::Model::MappingRule
- Inherits:
-
Object
- Object
- Lutaml::Model::MappingRule
- Defined in:
- lib/lutaml/model/mapping/mapping_rule.rb
Direct Known Subclasses
Constant Summary collapse
- ALLOWED_OPTIONS =
{ render_nil: i[ omit as_nil as_blank as_empty ], render_empty: i[ omit as_empty as_blank as_nil ], }.freeze
Instance Attribute Summary collapse
-
#as_attribute ⇒ Object
readonly
Returns the value of attribute as_attribute.
-
#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.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
(also: #from)
readonly
Returns the value of attribute name.
-
#polymorphic ⇒ Object
readonly
Returns the value of attribute polymorphic.
-
#polymorphic_map ⇒ Object
readonly
Returns the value of attribute polymorphic_map.
-
#render_default ⇒ Object
(also: #render_default?)
readonly
Returns the value of attribute render_default.
-
#render_empty ⇒ Object
readonly
Returns the value of attribute render_empty.
-
#render_nil ⇒ Object
readonly
Returns the value of attribute render_nil.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
-
#to_instance ⇒ Object
readonly
Returns the value of attribute to_instance.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
-
#treat_empty ⇒ Object
readonly
Returns the value of attribute treat_empty.
-
#treat_nil ⇒ Object
readonly
Returns the value of attribute treat_nil.
-
#treat_omitted ⇒ Object
readonly
Returns the value of attribute treat_omitted.
Instance Method Summary collapse
- #deep_dup ⇒ Object
- #default_value_map(options = {}) ⇒ Object
- #deserialize(model, value, attributes, mapper_class = nil) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #has_custom_method_for_deserialization? ⇒ Boolean
- #has_custom_method_for_serialization? ⇒ Boolean
-
#initialize(name, to:, to_instance: nil, as_attribute: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, attribute: false, delegate: nil, root_mappings: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}) ⇒ MappingRule
constructor
A new instance of MappingRule.
- #multiple_mappings? ⇒ Boolean
- #polymorphic_mapping? ⇒ Boolean
- #raw_mapping? ⇒ Boolean
- #render?(value, instance = nil, options = {}) ⇒ Boolean
- #render_as(key, default_value, options = {}) ⇒ Object
- #render_empty?(options = {}) ⇒ Boolean
- #render_nil?(options = {}) ⇒ Boolean
- #render_omitted?(options = {}) ⇒ Boolean
- #render_value_for(value) ⇒ Object
- #serialize(model, parent = nil, doc = nil) ⇒ Object
- #serialize_attribute(model, element, doc) ⇒ Object
- #to_value_for(model) ⇒ Object
- #treat?(value) ⇒ Boolean
- #treat_as(key, default_value, options = {}) ⇒ Object
- #treat_empty?(options = {}) ⇒ Boolean
- #treat_nil?(options = {}) ⇒ Boolean
- #treat_omitted?(options = {}) ⇒ Boolean
- #value_for_option(option, empty_value = nil) ⇒ Object
- #value_map(key, options = {}) ⇒ Object
Constructor Details
#initialize(name, to:, to_instance: nil, as_attribute: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, attribute: false, delegate: nil, root_mappings: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}) ⇒ MappingRule
Returns a new instance of MappingRule.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 46 def initialize( name, to:, to_instance: nil, as_attribute: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, attribute: false, delegate: nil, root_mappings: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {} ) @name = name @to = to @to_instance = to_instance @as_attribute = as_attribute @render_nil = render_nil @render_default = render_default @render_empty = render_empty @treat_nil = treat_nil @treat_empty = treat_empty @treat_omitted = treat_omitted @custom_methods = with @attribute = attribute @delegate = delegate @root_mappings = root_mappings @polymorphic = polymorphic @polymorphic_map = polymorphic_map @transform = transform @value_map = default_value_map @value_map[:from].merge!(value_map[:from] || {}) @value_map[:to].merge!(value_map[:to] || {}) end |
Instance Attribute Details
#as_attribute ⇒ Object (readonly)
Returns the value of attribute as_attribute.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def as_attribute @as_attribute end |
#attribute ⇒ Object (readonly) Also known as: attribute?
Returns the value of attribute attribute.
4 5 6 |
# File 'lib/lutaml/model/mapping/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/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/mapping_rule.rb', line 4 def delegate @delegate end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def format @format end |
#name ⇒ Object (readonly) Also known as: from
Returns the value of attribute name.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def name @name end |
#polymorphic ⇒ Object (readonly)
Returns the value of attribute polymorphic.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def polymorphic @polymorphic end |
#polymorphic_map ⇒ Object (readonly)
Returns the value of attribute polymorphic_map.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def polymorphic_map @polymorphic_map 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/mapping_rule.rb', line 4 def render_default @render_default end |
#render_empty ⇒ Object (readonly)
Returns the value of attribute render_empty.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def render_empty @render_empty end |
#render_nil ⇒ Object (readonly)
Returns the value of attribute render_nil.
4 5 6 |
# File 'lib/lutaml/model/mapping/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/mapping_rule.rb', line 4 def to @to end |
#to_instance ⇒ Object (readonly)
Returns the value of attribute to_instance.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def to_instance @to_instance end |
#transform ⇒ Object (readonly)
Returns the value of attribute transform.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def transform @transform end |
#treat_empty ⇒ Object (readonly)
Returns the value of attribute treat_empty.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def treat_empty @treat_empty end |
#treat_nil ⇒ Object (readonly)
Returns the value of attribute treat_nil.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def treat_nil @treat_nil end |
#treat_omitted ⇒ Object (readonly)
Returns the value of attribute treat_omitted.
4 5 6 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 4 def treat_omitted @treat_omitted end |
Instance Method Details
#deep_dup ⇒ Object
248 249 250 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 248 def deep_dup raise NotImplementedError, "Subclasses must implement `deep_dup`." end |
#default_value_map(options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 89 def default_value_map( = {}) render_nil_as = render_as(:render_nil, :omitted, ) render_empty_as = render_as(:render_empty, :empty, ) treat_nil_as = treat_as(:treat_nil, :nil, ) treat_empty_as = treat_as(:treat_empty, :empty, ) treat_omitted_as = treat_as(:treat_omitted, :nil, ) { from: { omitted: treat_omitted_as, nil: treat_nil_as, empty: treat_empty_as }, to: { omitted: :omitted, nil: render_nil_as, empty: render_empty_as }, } end |
#deserialize(model, value, attributes, mapper_class = nil) ⇒ Object
218 219 220 221 222 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 218 def deserialize(model, value, attributes, mapper_class = nil) handle_custom_method(model, value, mapper_class) || handle_delegate(model, value, attributes) || handle_transform_method(model, value, attributes) end |
#eql?(other) ⇒ Boolean Also known as: ==
240 241 242 243 244 245 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 240 def eql?(other) other.class == self.class && instance_variables.all? do |var| instance_variable_get(var) == other.instance_variable_get(var) end end |
#has_custom_method_for_deserialization? ⇒ Boolean
228 229 230 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 228 def has_custom_method_for_deserialization? !custom_methods.empty? && custom_methods[:from] end |
#has_custom_method_for_serialization? ⇒ Boolean
224 225 226 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 224 def has_custom_method_for_serialization? !custom_methods.empty? && custom_methods[:to] end |
#multiple_mappings? ⇒ Boolean
232 233 234 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 232 def multiple_mappings? name.is_a?(Array) end |
#polymorphic_mapping? ⇒ Boolean
190 191 192 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 190 def polymorphic_mapping? polymorphic_map && !polymorphic_map.empty? end |
#raw_mapping? ⇒ Boolean
236 237 238 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 236 def raw_mapping? name == Constants::RAW_MAPPING_KEY end |
#render?(value, instance = nil, options = {}) ⇒ Boolean
131 132 133 134 135 136 137 138 139 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 131 def render?(value, instance = nil, = {}) if invalid_value?(value, ) false elsif instance.respond_to?(:using_default?) && instance.using_default?(to) render_default? else true end end |
#render_as(key, default_value, options = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 103 def render_as(key, default_value, = {}) value = public_send(key) value = [key] if value.nil? if value == true key.to_s.split("_").last.to_sym elsif value == false :omitted elsif value { as_empty: :empty, as_blank: :empty, as_nil: :nil, omit: :omitted, }[value] else default_value end end |
#render_empty?(options = {}) ⇒ Boolean
170 171 172 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 170 def render_empty?( = {}) value_map(:to, )[:empty] != :omitted end |
#render_nil?(options = {}) ⇒ Boolean
166 167 168 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 166 def render_nil?( = {}) value_map(:to, )[:nil] != :omitted end |
#render_omitted?(options = {}) ⇒ Boolean
174 175 176 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 174 def render_omitted?( = {}) value_map(:to, )[:omitted] != :omitted end |
#render_value_for(value) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 147 def render_value_for(value) if value.nil? value_for_option(value_map(:to)[:nil]) elsif Utils.empty?(value) value_for_option(value_map(:to)[:empty], value) elsif Utils.uninitialized?(value) value_for_option(value_map(:to)[:omitted]) else value end end |
#serialize(model, parent = nil, doc = nil) ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 210 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
194 195 196 197 198 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 194 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
200 201 202 203 204 205 206 207 208 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 200 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 |
#treat?(value) ⇒ Boolean
141 142 143 144 145 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 141 def treat?(value) (treat_nil? || !value.nil?) && (treat_empty? || !Utils.empty?(value)) && (treat_omitted? || Utils.initialized?(value)) end |
#treat_as(key, default_value, options = {}) ⇒ Object
123 124 125 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 123 def treat_as(key, default_value, = {}) public_send(key) || [key] || default_value end |
#treat_empty?(options = {}) ⇒ Boolean
182 183 184 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 182 def treat_empty?( = {}) value_map(:from, )[:empty] != :omitted end |
#treat_nil?(options = {}) ⇒ Boolean
178 179 180 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 178 def treat_nil?( = {}) value_map(:from, )[:nil] != :omitted end |
#treat_omitted?(options = {}) ⇒ Boolean
186 187 188 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 186 def treat_omitted?( = {}) value_map(:from, )[:omitted] != :omitted end |
#value_for_option(option, empty_value = nil) ⇒ Object
159 160 161 162 163 164 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 159 def value_for_option(option, empty_value = nil) return nil if option == :nil return empty_value || "" if option == :empty Lutaml::Model::UninitializedClass.instance end |
#value_map(key, options = {}) ⇒ Object
252 253 254 255 256 257 258 259 260 |
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 252 def value_map(key, = {}) = { nil: [:nil], empty: [:empty], omitted: [:omitted], }.compact @value_map[key].merge() end |