Class: Lutaml::Xml::MappingRule

Inherits:
Model::MappingRule show all
Defined in:
lib/lutaml/xml/mapping_rule.rb

Constant Summary

Constants inherited from Model::MappingRule

Model::MappingRule::ALLOWED_OPTIONS, Model::MappingRule::EMPTY_TRANSFORMERS, Model::MappingRule::TRANSFORM_DISPATCH

Instance Attribute Summary collapse

Attributes inherited from Model::MappingRule

#as_attribute, #attribute, #custom_methods, #delegate, #format, #name, #polymorphic, #polymorphic_map, #render_default, #render_empty, #render_nil, #to, #to_instance, #transform, #treat_empty, #treat_nil, #treat_omitted

Instance Method Summary collapse

Methods inherited from Model::MappingRule

#can_transform_to?, #custom_method_only?, #default_value_map, #deserialize, #eql?, #get_transformers, #has_custom_method_for_deserialization?, #has_custom_method_for_serialization?, #has_items?, #multiple_mappings?, #mutated_collection?, #name_strings, #polymorphic_mapping?, #raw_mapping?, #raw_value_map, #render?, #render_as, #render_empty?, #render_nil?, #render_omitted?, #render_value_for, #serialize, #serialize?, #to_value_for, transform_dispatch, #transform_value, #treat?, #treat_as, #treat_empty?, #treat_nil?, #treat_omitted?, #value_for_option, #value_map, #when_attribute, #when_attribute?

Constructor Details

#initialize(name, to:, render_nil: false, render_default: false, render_empty: false, treat_nil: nil, treat_empty: nil, treat_omitted: nil, with: {}, delegate: nil, namespace: nil, mixed_content: false, cdata: false, namespace_set: false, attribute: false, default_namespace: nil, polymorphic: {}, polymorphic_map: {}, transform: {}, value_map: {}, when_attribute: {}, as_list: nil, delimiter: nil, form: nil, documentation: nil, raw: nil) ⇒ MappingRule

rubocop:disable Metrics/ParameterLists -- mirrors the DSL surface



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/lutaml/xml/mapping_rule.rb', line 19

def initialize( # rubocop:disable Metrics/ParameterLists -- mirrors the DSL surface
  name,
to:,
render_nil: false,
render_default: false,
render_empty: false,
treat_nil: nil,
treat_empty: nil,
treat_omitted: nil,
with: {},
delegate: nil,
namespace: nil,
mixed_content: false,
cdata: false,
namespace_set: false,
attribute: false,
default_namespace: nil,
polymorphic: {},
polymorphic_map: {},
transform: {},
value_map: {},
when_attribute: {},
as_list: nil,
delimiter: nil,
form: nil,
documentation: nil,
raw: nil
)
  super(
    name,
    to: to,
    render_nil: render_nil,
    render_default: render_default,
    render_empty: render_empty,
    treat_nil: treat_nil,
    treat_empty: treat_empty,
    treat_omitted: treat_omitted,
    with: with,
    delegate: delegate,
    attribute: attribute,
    polymorphic: polymorphic,
    polymorphic_map: polymorphic_map,
    transform: transform,
    value_map: value_map,
    when_attribute: when_attribute,
  )

  # Store original namespace parameter to preserve :inherit symbol
  @namespace_param = namespace

  # Normalize namespace to XmlNamespace class
  @namespace_class = normalize_namespace(namespace)
  @namespace = @namespace_class == :blank ? nil : @namespace_class&.uri
  @prefix = @namespace_class == :blank ? nil : @namespace_class&.prefix_default
  @mixed_content = mixed_content
  @cdata = cdata

  @default_namespace = default_namespace

  @namespace_set = namespace_set
  @as_list = as_list
  @delimiter = delimiter
  @form = validate_form(form)
  @documentation = documentation
  @raw = validate_raw(raw)

  # Memoize prefixed_name at initialization for performance
  # This is safe because prefix and name are immutable after initialization
  @cached_prefixed_name = compute_prefixed_name

  # Memoize castable? at initialization for performance
  # All inputs (raw_mapping?, content_mapping?, custom_methods) are immutable
  @cached_castable = !raw_mapping? && !name.nil? && !custom_methods[:from]
end

Instance Attribute Details

#as_listObject (readonly)

Returns the value of attribute as_list.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def as_list
  @as_list
end

#cdataObject (readonly)

Returns the value of attribute cdata.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def cdata
  @cdata
end

#default_namespaceObject (readonly)

Returns the value of attribute default_namespace.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def default_namespace
  @default_namespace
end

#delimiterObject (readonly)

Returns the value of attribute delimiter.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def delimiter
  @delimiter
end

#documentationObject (readonly)

Returns the value of attribute documentation.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def documentation
  @documentation
end

#formObject (readonly)

Returns the value of attribute form.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def form
  @form
end

#mixed_contentObject (readonly)

Returns the value of attribute mixed_content.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def mixed_content
  @mixed_content
end

#namespaceObject

Writers for deep_dup (preserves exact object references)



17
18
19
# File 'lib/lutaml/xml/mapping_rule.rb', line 17

def namespace
  @namespace
end

#namespace_classObject

Writers for deep_dup (preserves exact object references)



17
18
19
# File 'lib/lutaml/xml/mapping_rule.rb', line 17

def namespace_class
  @namespace_class
end

#namespace_paramObject (readonly)

Returns the value of attribute namespace_param.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def namespace_param
  @namespace_param
end

#prefixObject

Writers for deep_dup (preserves exact object references)



17
18
19
# File 'lib/lutaml/xml/mapping_rule.rb', line 17

def prefix
  @prefix
end

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/lutaml/xml/mapping_rule.rb', line 6

def raw
  @raw
end

Instance Method Details

#attribute_match_names(attr, register, parent_ns_class, form_default, default_namespace) ⇒ Object

Match names an attribute rule consumes during parsing, mirroring how serialization qualifies the same attribute (resolve_namespace): a rule serialized with a prefix matches only attributes in its namespace; a rule serialized unprefixed matches only namespace-less attributes.

Performance: memoized on the rule with zero-allocation cache hits. The inputs are constant for a given rule, register and parent model, so every element of a collection reuses the first resolution.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/lutaml/xml/mapping_rule.rb', line 197

def attribute_match_names(attr, register, parent_ns_class, form_default,
                          default_namespace)
  if defined?(@_amn_names) && @_amn_register == register &&
      @_amn_parent_ns == parent_ns_class &&
      @_amn_form_default == form_default &&
      @_amn_default_ns == default_namespace
    return @_amn_names
  end

  result = compute_attribute_match_names(attr, register, parent_ns_class,
                                         form_default, default_namespace)
  @_amn_register = register
  @_amn_parent_ns = parent_ns_class
  @_amn_form_default = form_default
  @_amn_default_ns = default_namespace
  @_amn_names = result
  result
end

#castable?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/lutaml/xml/mapping_rule.rb', line 123

def castable?
  @cached_castable
end

#content_keyObject



119
120
121
# File 'lib/lutaml/xml/mapping_rule.rb', line 119

def content_key
  cdata ? "#cdata-section" : "text"
end

#content_mapping?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/lutaml/xml/mapping_rule.rb', line 115

def content_mapping?
  name.nil?
end

#deep_dupObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/lutaml/xml/mapping_rule.rb', line 239

def deep_dup
  # Preserve @namespace_param exactly as it was (string, Class, :inherit, or nil)
  # This ensures the duplicate has the same internal state as the original
  ns_param = if @namespace_param.is_a?(Class) || @namespace_param == :inherit
               # Classes and symbols are immutable, pass as-is
               @namespace_param
             else
               # Strings need to be duplicated
               @namespace_param&.dup
             end

  self.class.new(
    name.dup,
    to: to,
    treat_nil: @treat_nil,
    treat_empty: @treat_empty,
    treat_omitted: @treat_omitted,
    render_nil: render_nil,
    render_default: render_default,
    with: ::Lutaml::Model::Utils.deep_dup(custom_methods),
    delegate: delegate,
    namespace: ns_param,
    mixed_content: mixed_content,
    cdata: cdata,
    namespace_set: namespace_set?,
    attribute: attribute,
    polymorphic: polymorphic.dup,
    default_namespace: default_namespace&.dup,
    transform: transform.dup,
    render_empty: render_empty,
    value_map: ::Lutaml::Model::Utils.deep_dup(@value_map),
    as_list: @as_list,
    delimiter: @delimiter,
    form: @form,
    documentation: @documentation,
    raw: @raw,
    when_attribute: when_attribute,
  ).tap do |dup_rule|
    # Manually preserve the exact @namespace_class object to avoid
    # recreating anonymous classes (which would have different object_ids)
    dup_rule.namespace_class = @namespace_class

    # Manually ensure @namespace and @prefix are new string objects
    if dup_rule.namespace
      dup_rule.namespace = dup_rule.namespace.dup
    end
    if dup_rule.prefix
      dup_rule.prefix = dup_rule.prefix.dup
    end
  end
end

#form_set?Boolean

Check if form is explicitly set

Returns:

  • (Boolean)

    true if form option was provided



148
149
150
# File 'lib/lutaml/xml/mapping_rule.rb', line 148

def form_set?
  !form.nil?
end

#matches_when_attribute?(element) ⇒ Boolean

Whether a parsed element satisfies this rule's when_attribute discriminator: every expected attribute value matches.

Returns:

  • (Boolean)


232
233
234
235
236
237
# File 'lib/lutaml/xml/mapping_rule.rb', line 232

def matches_when_attribute?(element)
  when_attribute.all? do |name, expected|
    actual = element.find_attribute_value(name.to_s)
    !actual.nil? && actual.to_s == expected.to_s
  end
end

#mixed_content?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/lutaml/xml/mapping_rule.rb', line 127

def mixed_content?
  !!@mixed_content
end

#namespace_set?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/lutaml/xml/mapping_rule.rb', line 94

def namespace_set?
  !!@namespace_set
end

#namespaced_name(parent_namespace = nil, name = self.name) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/lutaml/xml/mapping_rule.rb', line 216

def namespaced_name(parent_namespace = nil, name = self.name)
  if name.to_s == "lang"
    ::Lutaml::Model::Utils.blank?(prefix) ? name.to_s : "#{prefix}:#{name}"
  elsif @namespace_param == :inherit && parent_namespace
    "#{parent_namespace}:#{name}"
  elsif namespace_set? || @attribute
    [namespace, name].compact.join(":")
  elsif default_namespace
    "#{default_namespace}:#{name}"
  else
    [parent_namespace, name].compact.join(":")
  end
end

#namespaced_names(parent_namespace = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/lutaml/xml/mapping_rule.rb', line 176

def namespaced_names(parent_namespace = nil)
  # Performance: 1-entry cache keyed on parent_namespace
  # Same parent_namespace is used for all rules of a given element
  if defined?(@_nn_cache) && @_nn_cache[0] == parent_namespace
    return @_nn_cache[1]
  end

  result = compute_namespaced_names(parent_namespace)
  @_nn_cache = [parent_namespace, result]
  result
end

#prefixed_nameString

Returns the prefixed name for this mapping rule Uses memoized value computed at initialization for performance

Returns:

  • (String)

    The prefixed name (e.g., "ns:name" or "name")



156
157
158
# File 'lib/lutaml/xml/mapping_rule.rb', line 156

def prefixed_name
  @cached_prefixed_name
end

#qualified?Boolean

Check if this mapping specifies qualified form

Returns:

  • (Boolean)

    true if form is :qualified



134
135
136
# File 'lib/lutaml/xml/mapping_rule.rb', line 134

def qualified?
  form == :qualified
end

#raw_element?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/lutaml/xml/mapping_rule.rb', line 111

def raw_element?
  @raw == :element
end

#resolve_namespace(attr:, register: nil, parent_ns_uri: nil, parent_ns_class: nil, form_default: :unqualified, use_prefix: nil, parent_prefix: nil) ⇒ Hash

Resolve namespace for this mapping rule with W3C-compliant priority

Parameters:

  • attr (Attribute)

    the attribute being mapped

  • register (Symbol, nil) (defaults to: nil)

    register ID for type resolution

  • parent_ns_uri (String, nil) (defaults to: nil)

    parent element's namespace URI

  • parent_ns_class (Class, nil) (defaults to: nil)

    parent's XmlNamespace class

  • form_default (Symbol) (defaults to: :unqualified)

    :qualified or :unqualified from schema

  • use_prefix (Boolean, String, nil) (defaults to: nil)

    whether to use prefix for this namespace

  • parent_prefix (String, nil) (defaults to: nil)

    actual prefix parent is using (custom or default)

Returns:

  • (Hash)

    namespace resolution result { uri: String|nil, prefix: String|nil, ns_class: Class|nil }



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/lutaml/xml/mapping_rule.rb', line 302

def resolve_namespace(attr:, register: nil, parent_ns_uri: nil,
                  parent_ns_class: nil, form_default: :unqualified,
                  use_prefix: nil, parent_prefix: nil)
  if attribute?
    resolve_attribute_namespace(attr, register, parent_ns_class,
                                form_default)
  else
    resolve_element_namespace(attr, register, parent_ns_uri,
                              parent_ns_class, form_default, use_prefix,
                              parent_prefix)
  end
end

#static_namespace_optionObject

Pre-computed frozen Hash for the static namespace override case. Returns nil when no static override is needed — the caller should use the parent options directly, avoiding per-rule Hash allocation.

Only rules with an explicit namespace (namespace_set? && != :inherit) produce a static override. The result is frozen and shared across all elements that evaluate this rule.



105
106
107
108
109
# File 'lib/lutaml/xml/mapping_rule.rb', line 105

def static_namespace_option
  return nil unless namespace_set? && @namespace_param != :inherit

  @static_namespace_option ||= { default_namespace: namespace }.freeze
end

#unqualified?Boolean

Check if this mapping specifies unqualified form

Returns:

  • (Boolean)

    true if form is :unqualified



141
142
143
# File 'lib/lutaml/xml/mapping_rule.rb', line 141

def unqualified?
  form == :unqualified
end