Class: Lutaml::Model::MappingRule

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

Direct Known Subclasses

KeyValue::MappingRule, Xml::MappingRule

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
EMPTY_TRANSFORMERS =

Frozen empty array for the common case of no transformers

[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

  # Only calculate default_value_map if value_map is not fully provided
  if value_map.empty? || !value_map[:from] || !value_map[:to]
    # Build value_map by starting with defaults from render_nil/render_empty,
    # then overlaying user-provided value_map entries on top.
    # User-provided value_map entries take PRECEDENCE over computed defaults.
    # This ensures that value_map: { to: { empty: :empty } } overrides
    # the default render_empty: false → :omitted behavior.
    vm = {
      from: (value_map[:from] || {}).dup,
      to: (value_map[:to] || {}).dup,
    }
    defaults = default_value_map
    vm[:from] = defaults[:from].merge(vm[:from])
    vm[:to] = defaults[:to].merge(vm[:to])
    @value_map = vm
  else
    # Complete value_map provided (e.g., from deep_dup), use it directly.
    @value_map = value_map
  end

  # Freeze value_map and its inner hashes — they are never mutated after
  # construction, and downstream code reads them on every serialization.
  @value_map[:from].freeze
  @value_map[:to].freeze
  @value_map.freeze
end

Instance Attribute Details

#as_attributeObject (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

#attributeObject (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_methodsObject (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

#delegateObject (readonly)

Returns the value of attribute delegate.



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

def delegate
  @delegate
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#nameObject (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

#polymorphicObject (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_mapObject (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_defaultObject (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_emptyObject (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_nilObject (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

#toObject (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_instanceObject (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

#transformObject (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_emptyObject (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_nilObject (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_omittedObject (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

#can_transform_to?(attribute, format) ⇒ Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 364

def can_transform_to?(attribute, format)
  get_transformers(attribute).any? { |t| t.can_transform?(:to, format) }
end

#deep_dupObject

Raises:

  • (NotImplementedError)


312
313
314
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 312

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

#default_value_map(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 111

def default_value_map(options = {})
  render_nil_as = render_as(:render_nil, :omitted, options)
  render_empty_as = render_as(:render_empty, :empty, options)

  treat_nil_as = treat_as(:treat_nil, :nil, options)
  treat_empty_as = treat_as(:treat_empty, :empty, options)
  treat_omitted_as = treat_as(:treat_omitted, :nil, options)

  {
    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



282
283
284
285
286
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 282

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: ==

Returns:

  • (Boolean)


304
305
306
307
308
309
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 304

def eql?(other)
  other.class == self.class &&
    instance_variables.all? do |var|
      instance_variable_get(var) == other.instance_variable_get(var)
    end
end

#get_transformers(attribute) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 371

def get_transformers(attribute)
  # Fast path: most rules have no transforms at all
  rule_transform = transform
  attr_transform = attribute&.transform

  if !rule_transform && !attr_transform
    return EMPTY_TRANSFORMERS
  end

  # Build transformer list only when needed
  transformers = []
  transformers << rule_transform if rule_transform
  transformers << attr_transform if attr_transform
  transformers.select! { |t| t.is_a?(Class) }
  transformers.freeze
end

#has_custom_method_for_deserialization?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 292

def has_custom_method_for_deserialization?
  !custom_methods.empty? && custom_methods[:from]
end

#has_custom_method_for_serialization?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 288

def has_custom_method_for_serialization?
  !custom_methods.empty? && custom_methods[:to]
end

#has_items?(value) ⇒ Boolean

Check if value is a non-empty collection

Returns:

  • (Boolean)


203
204
205
206
207
208
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 203

def has_items?(value)
  return false if value.nil? || Utils.uninitialized?(value)
  return false unless value.respond_to?(:empty?)

  !value.empty?
end

#multiple_mappings?Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 296

def multiple_mappings?
  name.is_a?(Array)
end

#mutated_collection?(value, instance) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 193

def mutated_collection?(value, instance)
  return false if value.nil? || Utils.uninitialized?(value)
  return false unless value.is_a?(Array) || value.is_a?(Lutaml::Model::Collection)
  return false if value.empty? # Empty collection is still default

  # If it's a non-empty collection and marked as using_default, it was mutated
  instance.respond_to?(:using_default?) && instance.using_default?(to)
end

#polymorphic_mapping?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 241

def polymorphic_mapping?
  polymorphic_map && !polymorphic_map.empty?
end

#raw_mapping?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 300

def raw_mapping?
  name == Constants::RAW_MAPPING_KEY
end

#raw_value_mapObject

Raw value_map hash access (for compiled rules). Use value_map(key, options) for individual lookups with overrides.



318
319
320
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 318

def raw_value_map
  @value_map
end

#render?(value, instance = nil, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 154

def render?(value, instance = nil, options = {})
  if invalid_value?(value, options)
    false
  # FIXED: Check if collection was mutated after initialization
  # A non-empty collection initialized with default [] should render if mutated
  # This handles the case where collection is mutated with << or custom methods
  elsif mutated_collection?(value, instance)
    true
  elsif instance.respond_to?(:using_default?) && instance.using_default?(to)
    render_default? || RenderPolicy.derived_attribute_for?(instance, to)
  else
    true
  end
end

#render_as(key, default_value, options = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 126

def render_as(key, default_value, options = {})
  value = public_send(key)
  value = options[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: :blank,
      as_nil: :nil,
      omit: :omitted,
    }[value]
  else
    default_value
  end
end

#render_empty?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 221

def render_empty?(options = {})
  value_map(:to, options)[:empty] != :omitted
end

#render_nil?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 217

def render_nil?(options = {})
  value_map(:to, options)[:nil] != :omitted
end

#render_omitted?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 225

def render_omitted?(options = {})
  value_map(:to, options)[:omitted] != :omitted
end

#render_value_for(value) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 181

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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 261

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

    # Handle Reference types - extract the key for serialization
    # This ensures references serialize as their key, not the resolved object
    if value.is_a?(Lutaml::Model::Type::Reference)
      value = value.key
    elsif value.is_a?(Array)
      # Handle collection of references
      value = value.map do |v|
        v.is_a?(Lutaml::Model::Type::Reference) ? v.key : v
      end
    end

    value
  end
end

#serialize_attribute(model, element, doc) ⇒ Object



245
246
247
248
249
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 245

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



251
252
253
254
255
256
257
258
259
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 251

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

#transform_value(attribute, value, read_method, format) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 339

def transform_value(attribute, value, read_method, format)
  # Fast path: no transforms at all (covers 95%+ of rules)
  # transform defaults to {}, so check for actual content, not just truthiness
  has_rule_transform = transform.is_a?(Class)
  has_attr_transform = attribute&.transform.is_a?(Class)
  return value unless has_rule_transform || has_attr_transform

  transformers = get_transformers(attribute)
  transformers = transformers.reverse if read_method == :to

  return value if transformers.empty? || transformers.none? do |t|
    t.can_transform?(read_method, format)
  end

  # Apply transformers in sequence
  transformers.reduce(value) do |v, transformer|
    if transformer.is_a?(Class) && transformer < Lutaml::Model::ValueTransformer
      # Call class method directly: NameTransformer.from(value, :json)
    else
      # Hash/proc transformer
    end
    transformer.public_send(read_method, v, format)
  end
end

#treat?(value) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 169

def treat?(value)
  if value.nil?
    treat_nil?
  elsif Utils.uninitialized?(value)
    treat_omitted?
  elsif Utils.empty?(value)
    treat_empty?
  else
    true
  end
end

#treat_as(key, default_value, options = {}) ⇒ Object



146
147
148
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 146

def treat_as(key, default_value, options = {})
  public_send(key) || options[key] || default_value
end

#treat_empty?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 233

def treat_empty?(options = {})
  value_map(:from, options)[:empty] != :omitted
end

#treat_nil?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 229

def treat_nil?(options = {})
  value_map(:from, options)[:nil] != :omitted
end

#treat_omitted?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 237

def treat_omitted?(options = {})
  value_map(:from, options)[:omitted] != :omitted
end

#value_for_option(option, empty_value = nil) ⇒ Object



210
211
212
213
214
215
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 210

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



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/lutaml/model/mapping/mapping_rule.rb', line 322

def value_map(key, options = {})
  # Fast path: when no overrides, return cached value directly
  # Callers MUST NOT mutate the returned hash
  if !options[:nil] && !options[:empty] && !options[:omitted]
    return @value_map[key]
  end

  # Slow path: merge overrides (only when options have actual values)
  overrides = {
    nil: options[:nil],
    empty: options[:empty],
    omitted: options[:omitted],
  }.compact

  @value_map[key].merge(overrides)
end