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)


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

def castable?
  @cached_castable
end

#content_keyObject



97
98
99
# File 'lib/lutaml/xml/mapping_rule.rb', line 97

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

#content_mapping?Boolean

Returns:

  • (Boolean)


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

def content_mapping?
  name.nil?
end

#deep_dupObject



180
181
182
183
184
185
186
187
188
189
190
191
192
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
# File 'lib/lutaml/xml/mapping_rule.rb', line 180

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.send(:namespace_class=, @namespace_class)

    # Manually ensure @namespace and @prefix are new string objects
    if dup_rule.namespace
      dup_rule.send(:namespace=, dup_rule.namespace.dup)
    end
    if dup_rule.prefix
      dup_rule.send(: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



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

def form_set?
  !form.nil?
end

#mixed_content?Boolean

Returns:

  • (Boolean)


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

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



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

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



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lutaml/xml/mapping_rule.rb', line 154

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



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

def prefixed_name
  @cached_prefixed_name
end

#qualified?Boolean

Check if this mapping specifies qualified form

Returns:

  • (Boolean)

    true if form is :qualified



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

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 }



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/lutaml/xml/mapping_rule.rb', line 241

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

#unqualified?Boolean

Check if this mapping specifies unqualified form

Returns:

  • (Boolean)

    true if form is :unqualified



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

def unqualified?
  form == :unqualified
end