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

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?, #default_value_map, #deserialize, #eql?, #get_transformers, #has_custom_method_for_deserialization?, #has_custom_method_for_serialization?, #has_items?, #multiple_mappings?, #mutated_collection?, #polymorphic_mapping?, #raw_mapping?, #raw_value_map, #render?, #render_as, #render_empty?, #render_nil?, #render_omitted?, #render_value_for, #serialize, #serialize_attribute, #to_value_for, #transform_value, #treat?, #treat_as, #treat_empty?, #treat_nil?, #treat_omitted?, #value_for_option, #value_map

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: {}, as_list: nil, delimiter: nil, form: nil, documentation: nil) ⇒ MappingRule

Returns a new instance of MappingRule.



18
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
# File 'lib/lutaml/xml/mapping_rule.rb', line 18

def 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: {},
as_list: nil,
delimiter: nil,
form: nil,
documentation: 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,
  )

  # 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

  # 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)



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

def namespace
  @namespace
end

#namespace_classObject

Writers for deep_dup (preserves exact object references)



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

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)



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

def prefix
  @prefix
end

Instance Method Details

#castable?Boolean

Returns:

  • (Boolean)


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

def castable?
  @cached_castable
end

#content_keyObject



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

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

#content_mapping?Boolean

Returns:

  • (Boolean)


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

def content_mapping?
  name.nil?
end

#deep_dupObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/lutaml/xml/mapping_rule.rb', line 193

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,
  ).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



139
140
141
# File 'lib/lutaml/xml/mapping_rule.rb', line 139

def form_set?
  !form.nil?
end

#mixed_content?Boolean

Returns:

  • (Boolean)


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

def mixed_content?
  !!@mixed_content
end

#namespace_set?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/lutaml/xml/mapping_rule.rb', line 89

def namespace_set?
  !!@namespace_set
end

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



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

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



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/lutaml/xml/mapping_rule.rb', line 167

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”)



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

def prefixed_name
  @cached_prefixed_name
end

#qualified?Boolean

Check if this mapping specifies qualified form

Returns:

  • (Boolean)

    true if form is :qualified



125
126
127
# File 'lib/lutaml/xml/mapping_rule.rb', line 125

def qualified?
  form == :qualified
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 }



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/lutaml/xml/mapping_rule.rb', line 254

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.



100
101
102
103
104
# File 'lib/lutaml/xml/mapping_rule.rb', line 100

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



132
133
134
# File 'lib/lutaml/xml/mapping_rule.rb', line 132

def unqualified?
  form == :unqualified
end