Class: Lutaml::Xml::Mapping
- Inherits:
-
Model::Mapping
- Object
- Model::Mapping
- Lutaml::Xml::Mapping
- Defined in:
- lib/lutaml/xml/mapping.rb
Constant Summary collapse
- TYPES =
{ attribute: :map_attribute, element: :map_element, content: :map_content, all_content: :map_all, processing_instruction: :map_processing_instruction, }.freeze
Instance Attribute Summary collapse
-
#consolidation_maps ⇒ Object
readonly
Returns the value of attribute consolidation_maps.
-
#documentation_text ⇒ Object
readonly
Returns the value of attribute documentation_text.
-
#element_name ⇒ Object
readonly
Returns the value of attribute element_name.
-
#element_sequence ⇒ Object
readonly
Returns the value of attribute element_sequence.
-
#mapper_class ⇒ Object
readonly
Returns the value of attribute mapper_class.
-
#mappings_imported ⇒ Object
readonly
Returns the value of attribute mappings_imported.
-
#namespace_class ⇒ Object
readonly
Returns the value of attribute namespace_class.
-
#namespace_param ⇒ Object
readonly
Returns the value of attribute namespace_param.
-
#namespace_prefix ⇒ Object
readonly
Returns the value of attribute namespace_prefix.
-
#namespace_scope(namespaces = nil) ⇒ Array<Class>
readonly
Set the namespace scope for this mapping.
-
#namespace_scope_config ⇒ Object
Returns the value of attribute namespace_scope_config.
-
#namespace_uri ⇒ Object
readonly
Returns the value of attribute namespace_uri.
-
#processing_instruction_mappings ⇒ Object
readonly
Returns the value of attribute processing_instruction_mappings.
-
#root_element ⇒ Object
readonly
Returns the value of attribute root_element.
-
#type_name_value ⇒ Object
readonly
Returns the value of attribute type_name_value.
-
#xml_space(value = nil) ⇒ Symbol?
readonly
Control the xml:space attribute for this element.
Class Method Summary collapse
-
.xml(&block) ⇒ Lutaml::Xml::Mapping
Class-level XML DSL for reusable mapping classes.
-
.xml_mapping_instance ⇒ Lutaml::Xml::Mapping?
Get the shared XML mapping instance for this mapping class.
Instance Method Summary collapse
- #attributes(register_id = nil) ⇒ Object
- #attributes_hash ⇒ Object
- #attributes_to_dup ⇒ Object
-
#consolidate_map(by:, to:, group_class: nil) { ... } ⇒ ConsolidationMap
Declare consolidation rules for this mapping.
- #content_mapping ⇒ Object
- #decode_html_entities? ⇒ Boolean
- #deep_dup ⇒ Object
-
#documentation(text) ⇒ String
Set documentation text for this mapping.
- #dup_mappings(mappings) ⇒ Object
-
#element(name) ⇒ String
Set the XML element name for this mapping.
- #elements(register_id = nil) ⇒ Object
-
#elements_hash ⇒ Object
Raw hash access for inheritance and deep_dup operations.
-
#enable_mixed_content ⇒ Boolean
(also: #mixed_content)
Enable mixed content for this element.
- #ensure_mappings_imported!(register_id = nil) ⇒ Object
- #finalize(mapper_class) ⇒ Object
- #finalized? ⇒ Boolean
-
#find_attribute(name) ⇒ MappingRule?
Find attribute mapping rule by attribute name.
- #find_by_name(name, type: "Text", node_type: nil, namespace_uri: nil) ⇒ Object
- #find_by_to(to) ⇒ Object
- #find_by_to!(to) ⇒ Object
-
#find_element(name, register_id = nil) ⇒ MappingRule?
Find element mapping rule by attribute name.
-
#html_entities(enabled: true) ⇒ Object
lutaml-model#154: opt-in decoding of HTML named/numeric entities (e.g. ©, —) in mapped text and attribute values.
-
#import_model_mappings(model, register_id = nil) ⇒ Object
Import mappings from another model.
- #importable_mappings ⇒ Object
-
#initialize ⇒ Mapping
constructor
A new instance of Mapping.
- #map_all(to:, render_nil: false, render_default: false, delegate: nil, with: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), render_empty: false) ⇒ Object (also: #map_all_content)
- #map_attribute(name, to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, polymorphic_map: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), transform: {}, value_map: {}, as_list: nil, delimiter: nil, form: nil, documentation: nil, xsd_type: (xsd_type_provided = false nil)) ⇒ Object
- #map_content(to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, mixed: false, cdata: false, transform: {}, value_map: {}) ⇒ Object
- #map_element(name, to: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, delegate: nil, cdata: false, polymorphic: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), transform: {}, value_map: {}, when_attribute: {}, unmatched: :drop, form: nil, documentation: nil, xsd_type: (xsd_type_provided = false nil), raw: nil) ⇒ Object
- #map_instances(to:, polymorphic: {}) ⇒ Object
-
#map_processing_instruction(target, to:) ⇒ Object
Map XML processing instructions with a given target to a model attribute.
- #mapping_attributes_hash(register_id = nil) ⇒ Object
- #mapping_elements_hash(register_id = nil) ⇒ Object
- #mappings(register_id = nil) ⇒ Object
- #merge_elements_sequence(mapping) ⇒ Object
-
#mixed_content? ⇒ Boolean
Check if mixed content is enabled.
-
#namespace(ns_class_or_symbol, _deprecated_prefix = nil) ⇒ void
Set the XML namespace for this mapping.
-
#namespace_class? ⇒ Boolean
Whether namespace_class was explicitly set (not nil).
-
#namespace_set? ⇒ Boolean
Whether namespace was explicitly set via DSL.
-
#no_element? ⇒ Boolean
Check if this mapping has no element declaration.
-
#no_root ⇒ Object
deprecated
Deprecated.
Use absence of element() declaration and type_name() instead
-
#no_root? ⇒ Boolean
Check if this mapping has no root element.
-
#on_attribute(name, id: nil, &block) ⇒ void
Add a complex listener for an XML attribute with a custom handler block.
-
#on_element(name, id: nil, &block) ⇒ void
Add a complex listener for an XML element with a custom handler block.
-
#ordered ⇒ Boolean
Enable ordered content for this element.
-
#ordered? ⇒ Boolean?
Return whether this mapping uses ordered content.
-
#plain_element_rules_by_attr(register_id = nil) ⇒ Hash{Symbol, String => Array<MappingRule>}
lutaml-model#765: plain element rules grouped by target attribute name, for spelling-tolerance mapping (several element names feeding one attribute).
- #polymorphic_mapping ⇒ Object
- #prefixed_root ⇒ Object
-
#preserve_whitespace? ⇒ Boolean
Whether this mapping should auto-add xml:space="preserve".
- #raw_mapping ⇒ Object
-
#root(name, mixed: false, ordered: false) ⇒ String
Set the root element name with optional configuration.
- #root? ⇒ Boolean
-
#rule_records_version ⇒ Object
Bumped at finalize; ModelTransform's compiled rule records are keyed on it so late mapping changes rebuild instead of going stale.
- #sequence(&block) ⇒ Object
- #sequence_dup(sequence) ⇒ Object
- #sequence_importable_mappings ⇒ Object
- #set_mappings_imported(value) ⇒ Object
-
#sole_local_attribute_claimant?(rule, register_id = nil) ⇒ Boolean
lutaml-model#790/#791: whether
ruleis the only attribute rule claiming its exact name spelling. -
#type_name(name = nil) ⇒ String?
(also: #xsd_type)
Set explicit type name for XSD generation.
- #validate!(key, to, with, render_nil, render_empty, type: nil) ⇒ Object
-
#validate_mapped_attribute_types!(mapper_class) ⇒ Object
Fire InvalidAttributeTypeError at definition time for mapped rules whose target attribute type is neither a Type::Value nor Serializable (lutaml-model#296).
-
#validate_mixed_content_collection!(mapper_class) ⇒ Object
Validate that mixed content models have a collection attribute for their content mapping.
-
#validate_namespace_prefix!(prefix) ⇒ void
Validate namespace prefix parameter.
-
#validate_namespace_scope!(namespaces) ⇒ void
Validate namespace_scope parameter.
- #validate_ordered_content_mapping!(mapper_class) ⇒ Object
- #validate_raw_mappings!(type) ⇒ Object
- #validate_to_and_with_arguments!(key, to, with) ⇒ Object
- #validate_with_options!(key, to, with) ⇒ Object
-
#w3c_attributes(*attrs) ⇒ Object
Convenience method for standard W3C XML attributes.
Methods inherited from Model::Mapping
#add_listener, #all_listeners, #duplicate_mappings, #inherit_from, #listeners_for, #omit_element, #omit_listener, #parent_mapping
Constructor Details
#initialize ⇒ Mapping
Returns a new instance of Mapping.
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 |
# File 'lib/lutaml/xml/mapping.rb', line 57 def initialize super @elements = {} @attributes = {} @element_sequence = [] @content_mapping = nil @raw_mapping = nil @mixed_content = false @xml_space = nil @format = :xml @register_elements = ::Hash.new { |h, k| h[k] = {} } @register_attributes = ::Hash.new { |h, k| h[k] = {} } @register_element_sequences = ::Hash.new { |h, k| h[k] = [] } @consolidation_maps = [] @element_name = nil @namespace_class = nil @documentation_text = nil @type_name_value = nil @namespace_scope = [] @namespace_scope_config = [] @mapper_class = nil @importing_mappings = false @attributes_with_methods_defined = Set.new @processing_instruction_mappings = [] # Performance: Caches for finalized mapping queries @cached_elements = {} @cached_attributes = {} @cached_mappings = {} @cached_attribute_name_counts = {} @finalized = false end |
Instance Attribute Details
#consolidation_maps ⇒ Object (readonly)
Returns the value of attribute consolidation_maps.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def consolidation_maps @consolidation_maps end |
#documentation_text ⇒ Object (readonly)
Returns the value of attribute documentation_text.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def documentation_text @documentation_text end |
#element_name ⇒ Object (readonly)
Returns the value of attribute element_name.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def element_name @element_name end |
#element_sequence ⇒ Object (readonly)
Returns the value of attribute element_sequence.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def element_sequence @element_sequence end |
#mapper_class ⇒ Object (readonly)
Returns the value of attribute mapper_class.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def mapper_class @mapper_class end |
#mappings_imported ⇒ Object (readonly)
Returns the value of attribute mappings_imported.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def mappings_imported @mappings_imported end |
#namespace_class ⇒ Object (readonly)
Returns the value of attribute namespace_class.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def namespace_class @namespace_class end |
#namespace_param ⇒ Object (readonly)
Returns the value of attribute namespace_param.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def namespace_param @namespace_param end |
#namespace_prefix ⇒ Object (readonly)
Returns the value of attribute namespace_prefix.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def namespace_prefix @namespace_prefix end |
#namespace_scope(namespaces = nil) ⇒ Array<Class> (readonly)
Set the namespace scope for this mapping
Controls which namespaces are declared at the root element level versus being declared locally on each element that uses them.
Namespaces listed in namespace_scope will be declared once on the root element. Namespaces not listed will be declared locally on elements where they are used.
463 464 465 |
# File 'lib/lutaml/xml/mapping.rb', line 463 def namespace_scope @namespace_scope end |
#namespace_scope_config ⇒ Object
Returns the value of attribute namespace_scope_config.
1416 1417 1418 |
# File 'lib/lutaml/xml/mapping.rb', line 1416 def namespace_scope_config @namespace_scope_config end |
#namespace_uri ⇒ Object (readonly)
Returns the value of attribute namespace_uri.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def namespace_uri @namespace_uri end |
#processing_instruction_mappings ⇒ Object (readonly)
Returns the value of attribute processing_instruction_mappings.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def processing_instruction_mappings @processing_instruction_mappings end |
#root_element ⇒ Object (readonly)
Returns the value of attribute root_element.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def root_element @root_element end |
#type_name_value ⇒ Object (readonly)
Returns the value of attribute type_name_value.
41 42 43 |
# File 'lib/lutaml/xml/mapping.rb', line 41 def type_name_value @type_name_value end |
#xml_space(value = nil) ⇒ Symbol? (readonly)
Control the xml:space attribute for this element
When set to :preserve, automatically adds xml:space="preserve"
during serialization. When set to :default, adds xml:space="default".
When false or nil, no xml:space attribute is added.
261 262 263 |
# File 'lib/lutaml/xml/mapping.rb', line 261 def xml_space @xml_space end |
Class Method Details
.xml(&block) ⇒ Lutaml::Xml::Mapping
Class-level XML DSL for reusable mapping classes.
When a subclass of Lutaml::Xml::Mapping uses xml do...end in its
class body, this method creates an instance and evaluates the block.
27 28 29 30 31 |
# File 'lib/lutaml/xml/mapping.rb', line 27 def self.xml(&block) @xml_instance ||= new @xml_instance.instance_eval(&block) if block @xml_instance end |
.xml_mapping_instance ⇒ Lutaml::Xml::Mapping?
Get the shared XML mapping instance for this mapping class. Created lazily via the xml() class method.
37 38 39 |
# File 'lib/lutaml/xml/mapping.rb', line 37 def self.xml_mapping_instance @xml_instance end |
Instance Method Details
#attributes(register_id = nil) ⇒ Object
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 |
# File 'lib/lutaml/xml/mapping.rb', line 1137 def attributes(register_id = nil) reg_key = register_id || :default cached = @cached_attributes[reg_key] return cached if cached result = mapping_attributes_hash(register_id).values.flat_map do |v| v.is_a?(Array) ? v : [v] end @cached_attributes[reg_key] = result.freeze if @finalized result end |
#attributes_hash ⇒ Object
1390 1391 1392 |
# File 'lib/lutaml/xml/mapping.rb', line 1390 def attributes_hash @attributes end |
#attributes_to_dup ⇒ Object
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 |
# File 'lib/lutaml/xml/mapping.rb', line 1477 def attributes_to_dup @attributes_to_dup ||= %i[ @content_mapping @raw_mapping @element_sequence @attributes @elements @processing_instruction_mappings @register_elements @register_attributes @register_element_sequences @mappings_imported @sequence_importable_mappings @consolidation_maps ] end |
#consolidate_map(by:, to:, group_class: nil) { ... } ⇒ ConsolidationMap
Declare consolidation rules for this mapping.
Creates a ConsolidationMap that describes how sibling elements are grouped into structured model instances.
890 891 892 893 894 895 |
# File 'lib/lutaml/xml/mapping.rb', line 890 def consolidate_map(by:, to:, group_class: nil, &block) builder = ::Lutaml::Model::ConsolidationMap::Builder.new(by, to, group_class) builder.instance_eval(&block) @consolidation_maps << builder.build end |
#content_mapping ⇒ Object
1149 1150 1151 |
# File 'lib/lutaml/xml/mapping.rb', line 1149 def content_mapping @content_mapping end |
#decode_html_entities? ⇒ Boolean
1407 1408 1409 |
# File 'lib/lutaml/xml/mapping.rb', line 1407 def decode_html_entities? !!@decode_html_entities end |
#deep_dup ⇒ Object
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 |
# File 'lib/lutaml/xml/mapping.rb', line 1432 def deep_dup self.class.new.tap do |xml_mapping| if @root_element xml_mapping.root(@root_element.dup, mixed: @mixed_content, ordered: @ordered) end if @namespace_class xml_mapping.namespace(@namespace_class) elsif @namespace_param == :inherit xml_mapping.namespace(:inherit) elsif @namespace_param == :blank xml_mapping.namespace(:blank) end attributes_to_dup.each do |var_name| value = instance_variable_get(var_name) # Skip nil values (e.g., @register_elements not yet initialized on fresh copies) # Accessor methods will lazily initialize when needed next if value.nil? xml_mapping.instance_variable_set(var_name, ::Lutaml::Model::Utils.deep_dup(value)) end xml_mapping.instance_variable_set(:@mappings_imported, ::Hash.new { |h, k| h[k] = false }) xml_mapping.instance_variable_set(:@register_elements, ::Hash.new { |h, k| h[k] = {} }) xml_mapping.instance_variable_set(:@register_attributes, ::Hash.new { |h, k| h[k] = {} }) xml_mapping.instance_variable_set(:@register_element_sequences, ::Hash.new { |h, k| h[k] = [] }) xml_mapping.instance_variable_set(:@cached_elements, {}) xml_mapping.instance_variable_set(:@cached_attributes, {}) xml_mapping.instance_variable_set(:@cached_mappings, {}) xml_mapping.instance_variable_set(:@finalized, true) # CRITICAL: Do NOT copy @mapper_class to the duplicate # The duplicate may be used in a different class context # and should not carry over the original's mapper_class reference xml_mapping.instance_variable_set(:@mapper_class, nil) end end |
#documentation(text) ⇒ String
Set documentation text for this mapping
Used for XSD annotation generation.
482 483 484 |
# File 'lib/lutaml/xml/mapping.rb', line 482 def documentation(text) @documentation_text = text end |
#dup_mappings(mappings) ⇒ Object
1494 1495 1496 1497 1498 1499 1500 1501 1502 |
# File 'lib/lutaml/xml/mapping.rb', line 1494 def dup_mappings(mappings) new_mappings = {} mappings.each do |key, mapping_rule| new_mappings[key] = mapping_rule.deep_dup end new_mappings end |
#element(name) ⇒ String
Set the XML element name for this mapping
This is the primary method for declaring the element name. Use this for the new clean API.
205 206 207 208 |
# File 'lib/lutaml/xml/mapping.rb', line 205 def element(name) @element_name = name @root_element = name # Maintain backward compatibility end |
#elements(register_id = nil) ⇒ Object
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 |
# File 'lib/lutaml/xml/mapping.rb', line 1125 def elements(register_id = nil) reg_key = register_id || :default cached = @cached_elements[reg_key] return cached if cached result = mapping_elements_hash(register_id).values.flat_map do |v| v.is_a?(Array) ? v : [v] end @cached_elements[reg_key] = result.freeze if @finalized result end |
#elements_hash ⇒ Object
Raw hash access for inheritance and deep_dup operations. Callers may mutate the returned hash (e.g., setting keys).
1386 1387 1388 |
# File 'lib/lutaml/xml/mapping.rb', line 1386 def elements_hash @elements end |
#enable_mixed_content ⇒ Boolean Also known as: mixed_content
Enable mixed content for this element
Mixed content means the element can contain both text nodes and child elements interspersed.
231 232 233 234 |
# File 'lib/lutaml/xml/mapping.rb', line 231 def enable_mixed_content @mixed_content = true @ordered = true # mixed content implies ordered end |
#ensure_mappings_imported!(register_id = nil) ⇒ Object
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 |
# File 'lib/lutaml/xml/mapping.rb', line 1217 def ensure_mappings_imported!(register_id = nil) # CRITICAL: Return immediately if already imported to prevent redundant processing # This prevents the exponential explosion of recursive ensure_imports! calls # in complex schemas with hundreds of interdependent classes register_id ||= Lutaml::Model::Config.default_register # Use per-register tracking imported_flag = register_id || :default return if @mappings_imported[imported_flag] # CRITICAL: Prevent re-entrant calls during import processing # This prevents infinite loops when importing models that themselves have imports # Architecture: Import resolution should be atomic and non-recursive return if @importing_mappings # Check if there's any work to do - either regular imports OR sequence imports return if importable_mappings.empty? && sequence_importable_mappings.empty? # Mark as currently importing to prevent re-entrance @importing_mappings = true # Track if all imports were successfully resolved all_resolved = true # Process each deferred mapping import importable_mappings.dup.each do |model_sym| begin model_class = Lutaml::Model::GlobalContext.resolve_type(model_sym, register_id) rescue Lutaml::Model::UnknownTypeError # Model not registered yet - skip for now, will retry later all_resolved = false next end next if model_class.nil? # Skip if not registered yet # Recursively ensure the imported model's imports are resolved # Use the child class's own register if it has one if model_class.is_a?(Class) && model_class.include?(Lutaml::Model::Serialize) child_register = Lutaml::Model::Register.resolve_for_child( model_class, register_id ) model_class.ensure_imports!(child_register) end # Now import the mappings import_model_mappings(model_class, register_id) end # CRITICAL FIX: Process sequence importable mappings # Sequence blocks can have their own deferred imports via import_model_mappings # These need to be resolved separately because they add attributes to sequences unless sequence_importable_mappings.empty? sequence_importable_mappings.each do |sequence, model_syms| model_syms.dup.each do |model_sym| begin model_class = Lutaml::Model::GlobalContext.resolve_type( model_sym, register_id ) rescue Lutaml::Model::UnknownTypeError # Model not registered yet - skip for now all_resolved = false next end next if model_class.nil? # Recursively ensure the imported model's imports are resolved # Use the child class's own register if it has one if model_class.is_a?(Class) && model_class.include?(Lutaml::Model::Serialize) child_register = Lutaml::Model::Register.resolve_for_child( model_class, register_id ) model_class.ensure_imports!(child_register) end # Now import into the sequence # This will call Sequence#import_model_mappings which adds to sequence.attributes # and also calls @model.import_model_mappings to add to main mapping sequence.import_model_mappings(model_class, register_id) # Remove from queue after successful import model_syms.delete(model_sym) end end # Clean up empty sequence entries sequence_importable_mappings.reject! { |_, models| models.empty? } end # Mark as fully imported only if both queues are empty @mappings_imported[imported_flag] = all_resolved && importable_mappings.empty? && sequence_importable_mappings.empty? ensure # CRITICAL: Always reset the importing flag to prevent deadlock # Even if an exception occurs, we must allow future import attempts @importing_mappings = false end |
#finalize(mapper_class) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lutaml/xml/mapping.rb', line 91 def finalize(mapper_class) # Store mapper class for later use in deferred imports @mapper_class = mapper_class # DO NOT auto-generate root element # Models should explicitly declare root in their xml block if needed # Type-only models (used as nested types) don't need a root element # Resolve any deferred mapping imports before finalizing ensure_mappings_imported! # lutaml-model#687: nested sequence choices tag their target # model attributes now that the mapper class is known. @element_sequence.each do |seq| seq.bind_choice_compositors!(mapper_class) end # lutaml-model#296: a mapped rule whose attribute type can never # accept the serialization shape fails here, with the attribute # and type named, instead of surfacing mid-parse as a cast oddity. validate_mapped_attribute_types!(mapper_class) # Validate mixed content requires collection attribute for content mapping validate_mixed_content_collection!(mapper_class) # Validate element-only content models do not use map_content validate_ordered_content_mapping!(mapper_class) # Performance: Clear caches and mark finalized @cached_elements.clear @cached_attributes.clear @cached_mappings.clear @cached_attribute_name_counts.clear @rule_records_version = (@rule_records_version || 0) + 1 @finalized = true end |
#finalized? ⇒ Boolean
134 135 136 |
# File 'lib/lutaml/xml/mapping.rb', line 134 def finalized? @finalized end |
#find_attribute(name) ⇒ MappingRule?
Find attribute mapping rule by attribute name
1333 1334 1335 |
# File 'lib/lutaml/xml/mapping.rb', line 1333 def find_attribute(name) attributes.detect { |rule| name == rule.to } end |
#find_by_name(name, type: "Text", node_type: nil, namespace_uri: nil) ⇒ Object
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 |
# File 'lib/lutaml/xml/mapping.rb', line 1337 def find_by_name(name, type: "Text", node_type: nil, namespace_uri: nil) # If node_type is provided, use it for type detection (preferred) if node_type && %i[text cdata].include?(node_type) content_mapping # Backward compatibility: still check name for old code that doesn't pass node_type elsif ["text", "#cdata-section"].include?(name.to_s) && type == "Text" content_mapping else candidates = mappings.select do |rule| rule.name == name.to_s || rule.name == name.to_sym end return candidates.first if namespace_uri.nil? || candidates.one? candidates.find do |r| r.namespace_class&.uri == namespace_uri end || candidates.first end end |
#find_by_to(to) ⇒ Object
1356 1357 1358 |
# File 'lib/lutaml/xml/mapping.rb', line 1356 def find_by_to(to) mappings.detect { |rule| rule.to.to_s == to.to_s } end |
#find_by_to!(to) ⇒ Object
1360 1361 1362 1363 1364 1365 1366 |
# File 'lib/lutaml/xml/mapping.rb', line 1360 def find_by_to!(to) mapping = find_by_to(to) return mapping if !!mapping raise Lutaml::Model::NoMappingFoundError.new(to.to_s) end |
#find_element(name, register_id = nil) ⇒ MappingRule?
Find element mapping rule by attribute name
1325 1326 1327 |
# File 'lib/lutaml/xml/mapping.rb', line 1325 def find_element(name, register_id = nil) elements(register_id).detect { |rule| name == rule.to } end |
#html_entities(enabled: true) ⇒ Object
lutaml-model#154: opt-in decoding of HTML named/numeric entities (e.g. ©, —) in mapped text and attribute values. XML documents are otherwise kept byte-faithful: HTML entities are not defined in XML without a DTD.
1403 1404 1405 |
# File 'lib/lutaml/xml/mapping.rb', line 1403 def html_entities(enabled: true) @decode_html_entities = enabled end |
#import_model_mappings(model, register_id = nil) ⇒ Object
Import mappings from another model. For default register: imports BOTH attributes (into @mapper_class) AND mappings. For non-default register: stores mappings in register-specific storage (@register_elements, @register_attributes).
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
# File 'lib/lutaml/xml/mapping.rb', line 904 def import_model_mappings(model, register_id = nil) register_id ||= Lutaml::Model::Config.default_register reg_id = register_id return import_mappings_later(model, reg_id) if model_importable?(model) raise Lutaml::Model::ImportModelWithRootError.new(model) if model.root?(reg_id) if register_id != :default register_only_import_model_mappings(model, register_id) return end # CRITICAL: Access raw mapping structure directly without triggering import resolution # Calling model.mappings_for() can trigger ensure_imports! on that model # which creates circular chains: A imports B, B's mappings_for imports C, C imports A # Architecture: Access data structure directly, don't call methods that trigger resolutions mappings = model.instance_variable_get(:@mappings)&.dig(:xml) return unless mappings # Skip if no XML mappings defined # ATOMIC IMPORT: Both object model (attributes) AND serialization mappings # This mimics XSD complexType composition where importing a type means # getting both its structure (attributes) and serialization rules (mappings) # 1. Import object model (attributes with accessors) # This defines the data structure of the model # CRITICAL: Access attributes directly to avoid triggering ensure_imports! imported_attributes = ::Lutaml::Model::Utils.deep_dup(model.instance_variable_get(:@attributes)&.values || []) if @mapper_class imported_attributes.each do |attr| # CRITICAL: Check LOCAL set to avoid calling @mapper_class.attributes() # which can trigger ensure_imports! and create circular calls unless @attributes_with_methods_defined.include?(attr.name) # Define accessor methods on the model class @mapper_class.define_attribute_methods(attr, reg_id) @attributes_with_methods_defined.add(attr.name) end end # Merge attributes data - use direct access to avoid triggering imports attrs_hash = imported_attributes.to_h { |attr| [attr.name, attr] } existing_attrs = @mapper_class.instance_variable_get(:@attributes) || {} attrs_hash.each do |name, attr| if existing_attrs.key?(name) existing_attrs[name]..merge!(attr.) else existing_attrs[name] = attr end end @mapper_class.instance_variable_set(:@attributes, existing_attrs) end # 2. Import serialization mappings (XML element/attribute names → model attributes) # This defines how the data structure maps to/from XML # CRITICAL: Deep-copy mapping rules to prevent shared state # When multiple classes import the same model, each must have independent MappingRule instances # Otherwise, any state mutation during serialization affects ALL importing classes @elements.merge!(dup_mappings(mappings.instance_variable_get(:@elements))) @attributes.merge!(dup_mappings(mappings.instance_variable_get(:@attributes))) # CRITICAL: Deep-copy sequences to prevent shared state # Each importing class must have its own Sequence objects imported_sequences = mappings.element_sequence.map do |seq| seq.deep_dup(self) end (@element_sequence << imported_sequences).flatten! end |
#importable_mappings ⇒ Object
1213 1214 1215 |
# File 'lib/lutaml/xml/mapping.rb', line 1213 def importable_mappings @importable_mappings ||= [] end |
#map_all(to:, render_nil: false, render_default: false, delegate: nil, with: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), render_empty: false) ⇒ Object Also known as: map_all_content
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/lutaml/xml/mapping.rb', line 795 def map_all( to:, render_nil: false, render_default: false, delegate: nil, with: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), render_empty: false ) validate!( ::Lutaml::Model::Constants::RAW_MAPPING_KEY, to, with, render_nil, render_empty, type: TYPES[:all_content], ) # Warn if prefix parameter is provided if prefix_provided != false warn "[DEPRECATED] The prefix parameter on map_all is deprecated. " \ "Define prefix_default in your XmlNamespace class instead. " \ "Prefix '#{prefix}' will be ignored." end # Raise error if namespace parameter is provided if namespace_set != false raise Lutaml::Model::IncorrectMappingArgumentsError, "namespace is not allowed at map_all level. " \ "Namespaces must be declared on the MODEL CLASS itself using 'namespace' at the xml block level." end rule = MappingRule.new( ::Lutaml::Model::Constants::RAW_MAPPING_KEY, to: to, render_nil: render_nil, render_default: render_default, with: with, delegate: delegate, namespace: namespace, default_namespace: namespace_uri, namespace_set: namespace_set != false, ) @raw_mapping = rule end |
#map_attribute(name, to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, polymorphic_map: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), transform: {}, value_map: {}, as_list: nil, delimiter: nil, form: nil, documentation: nil, xsd_type: (xsd_type_provided = false nil)) ⇒ Object
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 |
# File 'lib/lutaml/xml/mapping.rb', line 659 def map_attribute( name, to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, polymorphic_map: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), transform: {}, value_map: {}, as_list: nil, delimiter: nil, form: nil, documentation: nil, xsd_type: (xsd_type_provided = false nil) ) validate!( name, to, with, render_nil, render_empty, type: TYPES[:attribute] ) # Warn if prefix parameter is provided if prefix_provided != false warn "[DEPRECATED] The prefix parameter on map_attribute is deprecated. " \ "Define prefix_default in your XmlNamespace class instead. " \ "Prefix '#{prefix}' will be ignored." end # Raise error if xsd_type parameter is provided if xsd_type_provided != false raise Lutaml::Model::IncorrectMappingArgumentsError, "xsd_type is not allowed at mapping level. " \ "XSD type must be declared in Type::Value classes using the xsd_type directive. " \ "See docs/migration-guides/xsd-type-migration.adoc" end # Raise error if namespace parameter is a non-nil value at attribute level # This is NOT allowed - namespaces should be declared at model level # However, namespace: nil is allowed to explicitly opt out of namespace # Note: namespace_set is false when default is used, nil when explicit arg passed if namespace_set != false && !namespace.nil? raise Lutaml::Model::IncorrectMappingArgumentsError, "namespace is not allowed at attribute mapping level. " \ "Namespaces should be declared on the MODEL CLASS itself using 'namespace' at the xml block level." end if name == "schemaLocation" location = caller_locations(1, 1)[0] caller_file = if defined?(File) File.basename(location.path) else location.path.to_s.split("/").last end ::Lutaml::Model::Logger.warn_auto_handling( name: name, caller_file: caller_file, caller_line: location.lineno, ) end rule = MappingRule.new( name, to: to, render_nil: render_nil, render_default: render_default, render_empty: render_empty, with: with, delegate: delegate, namespace: namespace, attribute: true, polymorphic_map: polymorphic_map, default_namespace: namespace_uri, namespace_set: namespace_set != false, transform: transform, value_map: value_map, as_list: as_list, delimiter: delimiter, form: form, documentation: documentation, ) # Use eql? to detect and skip exact duplicates (prevents accumulation) key = rule.namespaced_name existing = @attributes[key] if existing.nil? # New mapping - store directly @attributes[key] = rule elsif existing.is_a?(Array) # Array exists - check for duplicate or add duplicate_index = existing.find_index { |r| r.eql?(rule) } # Only add if not a duplicate existing << rule unless duplicate_index elsif existing.eql?(rule) # Exact duplicate - already stored, no action needed else # Different mapping (polymorphic) - convert to array @attributes[key] = [existing, rule] end end |
#map_content(to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, mixed: false, cdata: false, transform: {}, value_map: {}) ⇒ Object
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/lutaml/xml/mapping.rb', line 764 def map_content( to: nil, render_nil: false, render_default: false, render_empty: false, with: {}, delegate: nil, mixed: false, cdata: false, transform: {}, value_map: {} ) validate!( "content", to, with, render_nil, render_empty, type: TYPES[:content] ) @content_mapping = MappingRule.new( nil, to: to, render_nil: render_nil, render_default: render_default, render_empty: render_empty, with: with, delegate: delegate, mixed_content: mixed, cdata: cdata, transform: transform, value_map: value_map, ) end |
#map_element(name, to: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, delegate: nil, cdata: false, polymorphic: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), transform: {}, value_map: {}, when_attribute: {}, unmatched: :drop, form: nil, documentation: nil, xsd_type: (xsd_type_provided = false nil), raw: nil) ⇒ Object
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/lutaml/xml/mapping.rb', line 511 def map_element( name, to: nil, render_nil: false, render_default: false, render_empty: false, treat_nil: :nil, treat_empty: :empty, treat_omitted: :nil, with: {}, delegate: nil, cdata: false, polymorphic: {}, namespace: (namespace_set = false nil), prefix: (prefix_provided = false nil), transform: {}, value_map: {}, when_attribute: {}, unmatched: :drop, form: nil, documentation: nil, xsd_type: (xsd_type_provided = false nil), raw: nil ) validate!( name, to, with, render_nil, render_empty, type: TYPES[:element] ) # lutaml-model#88: grouped form — `when_attribute: "type"` plus # `to: { value => attribute }` expands to one rule per value at # DSL time, so the compiled rules are identical to the per-rule # form and nothing downstream changes. if when_attribute.is_a?(::String) || when_attribute.is_a?(::Symbol) # These locals are `false` when their kwarg default ran and # nil when the caller passed the kwarg explicitly (planned # locals read as nil, so defined? is useless here). if namespace_set.nil? || prefix_provided.nil? || xsd_type_provided.nil? raise Lutaml::Model::IncorrectMappingArgumentsError, "namespace/prefix/xsd_type are not supported in the " \ "grouped when_attribute form; declare rules individually" end when_attribute_group(when_attribute, to).each do |value, target| map_element( name, to: target, 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, cdata: cdata, polymorphic: polymorphic, transform: transform, value_map: value_map, when_attribute: { when_attribute.to_s => value.to_s }, unmatched: unmatched, form: form, documentation: documentation, raw: raw, ) end return end reject_ambiguous_when_attribute!(when_attribute, to) validate_when_attribute!(when_attribute) unless when_attribute.empty? validate_unmatched!(unmatched, when_attribute) # Warn if prefix parameter is provided if prefix_provided != false warn "[DEPRECATED] The prefix parameter on map_element is deprecated. " \ "Define prefix_default in your XmlNamespace class instead. " \ "Prefix '#{prefix}' will be ignored." end # Raise error if xsd_type parameter is provided if xsd_type_provided != false raise Lutaml::Model::IncorrectMappingArgumentsError, "xsd_type is not allowed at mapping level. " \ "XSD type must be declared in Type::Value classes using the xsd_type directive. " \ "See docs/migration-guides/xsd-type-migration.adoc" end # Raise error if namespace parameter is a non-nil value at element level # This is NOT allowed - namespaces should be declared at model level # However, namespace: nil is allowed to explicitly opt out of namespace # Note: namespace_set is false when default is used, nil when explicit arg passed if namespace_set != false && !namespace.nil? raise Lutaml::Model::IncorrectMappingArgumentsError, "namespace is not allowed at element mapping level. " \ "Namespaces must be declared on the MODEL CLASS itself using 'namespace' at the xml block level. " \ "Each model class should declare its own namespace, not individual elements." end rule = MappingRule.new( 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, cdata: cdata, namespace: namespace, default_namespace: namespace_uri, polymorphic: polymorphic, namespace_set: namespace_set != false || namespace == :inherit, transform: transform, value_map: value_map, form: form, documentation: documentation, raw: raw, when_attribute: when_attribute, unmatched: unmatched, ) # Store rules with the same element name in an array to support # multiple mapping rules for the same element name with different target types # Use eql? to detect and skip exact duplicates (prevents accumulation) key = rule.namespaced_name existing = @elements[key] if existing.nil? # New mapping - store directly @elements[key] = rule elsif existing.is_a?(Array) # Array exists - check for duplicate or add duplicate_index = existing.find_index { |r| r.eql?(rule) } # Only add if not a duplicate existing << rule unless duplicate_index elsif existing.eql?(rule) # Exact duplicate - already stored, no action needed else # Different mapping (polymorphic) - convert to array @elements[key] = [existing, rule] end end |
#map_instances(to:, polymorphic: {}) ⇒ Object
507 508 509 |
# File 'lib/lutaml/xml/mapping.rb', line 507 def map_instances(to:, polymorphic: {}) map_element(to, to: to, polymorphic: polymorphic) end |
#map_processing_instruction(target, to:) ⇒ Object
Map XML processing instructions with a given target to a model attribute.
During serialization, the attribute value (Hash) is expanded into
individual PIs: each key-value pair generates <?target key="value"?>.
During deserialization, PIs with matching target are parsed into the hash.
867 868 869 870 871 |
# File 'lib/lutaml/xml/mapping.rb', line 867 def map_processing_instruction(target, to:) @processing_instruction_mappings << ProcessingInstructionMapping.new( target.to_s, to ) end |
#mapping_attributes_hash(register_id = nil) ⇒ Object
1368 1369 1370 1371 1372 1373 1374 |
# File 'lib/lutaml/xml/mapping.rb', line 1368 def mapping_attributes_hash(register_id = nil) if register_id.nil? || register_id == :default @attributes else @attributes.merge(@register_attributes[register_id]) end end |
#mapping_elements_hash(register_id = nil) ⇒ Object
1376 1377 1378 1379 1380 1381 1382 |
# File 'lib/lutaml/xml/mapping.rb', line 1376 def mapping_elements_hash(register_id = nil) if register_id.nil? || register_id == :default @elements else @elements.merge(@register_elements[register_id]) end end |
#mappings(register_id = nil) ⇒ Object
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 |
# File 'lib/lutaml/xml/mapping.rb', line 1177 def mappings(register_id = nil) reg_key = register_id || :default cached = @cached_mappings[reg_key] return cached if cached result = elements(register_id) + attributes(register_id) + [content_mapping, raw_mapping].compact @cached_mappings[reg_key] = result.freeze if @finalized result end |
#merge_elements_sequence(mapping) ⇒ Object
1426 1427 1428 1429 1430 |
# File 'lib/lutaml/xml/mapping.rb', line 1426 def merge_elements_sequence(mapping) mapping.element_sequence.each do |sequence| element_sequence << sequence_dup(sequence) end end |
#mixed_content? ⇒ Boolean
Check if mixed content is enabled
188 189 190 |
# File 'lib/lutaml/xml/mapping.rb', line 188 def mixed_content? @mixed_content || false end |
#namespace(ns_class_or_symbol, _deprecated_prefix = nil) ⇒ void
This method returns an undefined value.
Set the XML namespace for this mapping
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/lutaml/xml/mapping.rb', line 374 def namespace(ns_class_or_symbol, _deprecated_prefix = nil) # Only raise error for explicitly marked no_root (using deprecated method) # Type-only models (no element declared) CAN have namespaces raise Lutaml::Model::TypeOnlyNamespaceError if @no_root # Warn if prefix parameter is provided if _deprecated_prefix warn "[DEPRECATED] The prefix parameter on namespace() is deprecated. " \ "Define prefix_default in your XmlNamespace class instead. " \ "Prefix '#{_deprecated_prefix}' will be ignored." end # Handle :blank symbol - explicit blank namespace if ns_class_or_symbol == :blank @namespace_class = nil @namespace_uri = nil @namespace_prefix = nil @namespace_set = true # Mark as explicitly set @namespace_param = :blank # Store original value return end # Handle :inherit symbol if ns_class_or_symbol == :inherit @namespace_set = true @namespace_param = :inherit return end # nil means "not set" - DON'T set @namespace_set if ns_class_or_symbol.nil? @namespace_class = nil @namespace_uri = nil @namespace_prefix = nil @namespace_set = false # Explicitly NOT set @namespace_param = nil return end # Accept both Lutaml::Xml::Namespace and Lutaml::Model::Xml::Namespace for compatibility is_namespace_class = ns_class_or_symbol.is_a?(Class) && ( (defined?(::Lutaml::Xml::Namespace) && ns_class_or_symbol < ::Lutaml::Xml::Namespace) || (defined?(::Lutaml::Model::Xml::Namespace) && ns_class_or_symbol < ::Lutaml::Model::Xml::Namespace) ) if is_namespace_class # XmlNamespace class passed - register and use @namespace_class = NamespaceClassRegistry.instance.register_named(ns_class_or_symbol) @namespace_uri = ns_class_or_symbol.uri @namespace_prefix = ns_class_or_symbol.prefix_default elsif ns_class_or_symbol.is_a?(String) # String URI - NOT SUPPORTED raise Lutaml::Xml::Error::InvalidNamespaceError.new( expected: "XmlNamespace class", got: ns_class_or_symbol, message: "String namespace URIs are not supported. " \ "Define an XmlNamespace class instead. " \ "See docs/_guides/xml-namespaces.adoc for migration guide.", ) else raise Lutaml::Xml::Error::InvalidNamespaceError.new( expected: "XmlNamespace class, :inherit, or :blank", got: ns_class_or_symbol, ) end end |
#namespace_class? ⇒ Boolean
Whether namespace_class was explicitly set (not nil)
1395 1396 1397 |
# File 'lib/lutaml/xml/mapping.rb', line 1395 def namespace_class? !@namespace_class.nil? end |
#namespace_set? ⇒ Boolean
Whether namespace was explicitly set via DSL
1412 1413 1414 |
# File 'lib/lutaml/xml/mapping.rb', line 1412 def namespace_set? !!@namespace_set end |
#no_element? ⇒ Boolean
Check if this mapping has no element declaration
345 346 347 |
# File 'lib/lutaml/xml/mapping.rb', line 345 def no_element? !element_name end |
#no_root ⇒ Object
Use absence of element() declaration and type_name() instead
Mark this model as having no root element (type-only)
324 325 326 327 328 329 |
# File 'lib/lutaml/xml/mapping.rb', line 324 def no_root warn "[Lutaml::Model] DEPRECATED: no_root is deprecated. " \ "Simply omit the element declaration for type-only models. " \ "Use type_name() to set the XSD type name." @no_root = true end |
#no_root? ⇒ Boolean
Check if this mapping has no root element
Returns true if:
- The deprecated @no_root flag is explicitly set, OR
- No element is declared (modern pattern - just omit element() call)
338 339 340 |
# File 'lib/lutaml/xml/mapping.rb', line 338 def no_root? !!@no_root || no_element? end |
#on_attribute(name, id: nil, &block) ⇒ void
This method returns an undefined value.
Add a complex listener for an XML attribute with a custom handler block.
1556 1557 1558 1559 1560 1561 1562 |
# File 'lib/lutaml/xml/mapping.rb', line 1556 def on_attribute(name, id: nil, &block) add_listener(Lutaml::Xml::Listener.new( target: name, id: id, handler: block, )) end |
#on_element(name, id: nil, &block) ⇒ void
This method returns an undefined value.
Add a complex listener for an XML element with a custom handler block.
Unlike map_element which creates a simple listener (framework handles deserialization), on_element allows custom deserialization logic.
Multiple listeners for the same element are allowed — all matching listeners are invoked during parsing.
1535 1536 1537 1538 1539 1540 1541 |
# File 'lib/lutaml/xml/mapping.rb', line 1535 def on_element(name, id: nil, &block) add_listener(Lutaml::Xml::Listener.new( target: name, id: id, handler: block, )) end |
#ordered ⇒ Boolean
Enable ordered content for this element
Ordered content means element order is preserved during
round-trip serialization without validation.
This is different from sequence which enforces and validates order.
Use this when:
- Element order matters for your application
- You need to preserve input order exactly
- You DON'T want to validate/enforce specific order
Use sequence when you need strict order validation.
313 314 315 |
# File 'lib/lutaml/xml/mapping.rb', line 313 def ordered @ordered = true end |
#ordered? ⇒ Boolean?
Return whether this mapping uses ordered content
194 195 196 |
# File 'lib/lutaml/xml/mapping.rb', line 194 def ordered? @ordered end |
#plain_element_rules_by_attr(register_id = nil) ⇒ Hash{Symbol, String => Array<MappingRule>}
lutaml-model#765: plain element rules grouped by target attribute name, for spelling-tolerance mapping (several element names feeding one attribute). Cached per register so the parse hot path only pays a hash lookup. Grouping uses rule-level properties only — the transform applies the collection-attribute filter.
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
# File 'lib/lutaml/xml/mapping.rb', line 1195 def plain_element_rules_by_attr(register_id = nil) reg_key = register_id || :default @plain_element_rules_by_attr ||= {} @plain_element_rules_by_attr[reg_key] ||= begin grouped = {} mappings(reg_key).each do |r| next if r.attribute? || r.raw_mapping? || r.content_mapping? || r.cdata || r.when_attribute? || r.has_custom_method_for_deserialization? || (r.transform.is_a?(Hash) && !r.transform.empty?) || r.transform.is_a?(Class) (grouped[r.to] ||= []) << r end grouped end end |
#polymorphic_mapping ⇒ Object
1473 1474 1475 |
# File 'lib/lutaml/xml/mapping.rb', line 1473 def polymorphic_mapping mappings.find(&:polymorphic_mapping?) end |
#prefixed_root ⇒ Object
349 350 351 352 353 354 355 |
# File 'lib/lutaml/xml/mapping.rb', line 349 def prefixed_root if namespace_uri && namespace_prefix "#{namespace_prefix}:#{root_element}" else root_element end end |
#preserve_whitespace? ⇒ Boolean
Whether this mapping should auto-add xml:space="preserve"
269 270 271 |
# File 'lib/lutaml/xml/mapping.rb', line 269 def preserve_whitespace? @xml_space == :preserve end |
#raw_mapping ⇒ Object
1153 1154 1155 |
# File 'lib/lutaml/xml/mapping.rb', line 1153 def raw_mapping @raw_mapping end |
#root(name, mixed: false, ordered: false) ⇒ String
Set the root element name with optional configuration
This is kept as an alias to element() for backward compatibility, but also supports the mixed: and ordered: options.
219 220 221 222 223 |
# File 'lib/lutaml/xml/mapping.rb', line 219 def root(name, mixed: false, ordered: false) element(name) @mixed_content = mixed @ordered = ordered || mixed # mixed content is always ordered end |
#root? ⇒ Boolean
317 318 319 |
# File 'lib/lutaml/xml/mapping.rb', line 317 def root? !!root_element end |
#rule_records_version ⇒ Object
Bumped at finalize; ModelTransform's compiled rule records are keyed on it so late mapping changes rebuild instead of going stale.
130 131 132 |
# File 'lib/lutaml/xml/mapping.rb', line 130 def rule_records_version @rule_records_version ||= 0 end |
#sequence(&block) ⇒ Object
873 874 875 876 877 878 |
# File 'lib/lutaml/xml/mapping.rb', line 873 def sequence(&block) @element_sequence << ::Lutaml::Model::Sequence.new(self, format: :xml).tap do |s| s.instance_eval(&block) end end |
#sequence_dup(sequence) ⇒ Object
1418 1419 1420 1421 1422 1423 1424 |
# File 'lib/lutaml/xml/mapping.rb', line 1418 def sequence_dup(sequence) Lutaml::Model::Sequence.new(self, format: :xml).tap do |instance| sequence.attributes.each do |attr| instance.attributes << attr.deep_dup end end end |
#sequence_importable_mappings ⇒ Object
1317 1318 1319 |
# File 'lib/lutaml/xml/mapping.rb', line 1317 def sequence_importable_mappings @sequence_importable_mappings ||= ::Hash.new { |h, k| h[k] = [] } end |
#set_mappings_imported(value) ⇒ Object
985 986 987 988 989 990 991 |
# File 'lib/lutaml/xml/mapping.rb', line 985 def set_mappings_imported(value) if @mappings_imported.is_a?(Hash) @mappings_imported.each_key { |k| @mappings_imported[k] = value } else @mappings_imported = value end end |
#sole_local_attribute_claimant?(rule, register_id = nil) ⇒ Boolean
lutaml-model#790/#791: whether rule is the only attribute rule
claiming its exact name spelling. The answer is a property of the
mapping, not the document, so it is counted once per register
(rules sharing a spelling make each other non-sole).
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 |
# File 'lib/lutaml/xml/mapping.rb', line 1161 def sole_local_attribute_claimant?(rule, register_id = nil) reg_key = register_id || :default counts = @cached_attribute_name_counts[reg_key] unless counts counts = {} attributes(register_id).each do |r| key = r.name_strings counts[key] = (counts[key] || 0) + 1 end counts.freeze if @finalized @cached_attribute_name_counts[reg_key] = counts end (counts[rule.name_strings] || 1) == 1 end |
#type_name(name = nil) ⇒ String? Also known as: xsd_type
Set explicit type name for XSD generation
By default, type name is inferred as "ClassNameType". Use this to override.
493 494 495 496 |
# File 'lib/lutaml/xml/mapping.rb', line 493 def type_name(name = nil) @type_name_value = name if name @type_name_value end |
#validate!(key, to, with, render_nil, render_empty, type: nil) ⇒ Object
993 994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/lutaml/xml/mapping.rb', line 993 def validate!(key, to, with, render_nil, render_empty, type: nil) validate_raw_mappings!(type) validate_to_and_with_arguments!(key, to, with) if render_nil == :as_empty || render_empty == :as_empty raise ::Lutaml::Model::IncorrectMappingArgumentsError.new( ":as_empty is not supported for XML mappings. " \ "Use :as_blank instead to create blank XML elements.", ) end end |
#validate_mapped_attribute_types!(mapper_class) ⇒ Object
Fire InvalidAttributeTypeError at definition time for mapped rules whose target attribute type is neither a Type::Value nor Serializable (lutaml-model#296). Undeclared types are left to deferred-import resolution.
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 |
# File 'lib/lutaml/xml/mapping.rb', line 1109 def validate_mapped_attribute_types!(mapper_class) (elements + attributes).each do |rule| target = mapper_class.attributes[rule.to] next unless target begin resolved = target.type(Lutaml::Model::Config.default_register) rescue Lutaml::Model::UnknownTypeError next end next unless resolved.is_a?(Class) target.validate_attr_type!(resolved) end end |
#validate_mixed_content_collection!(mapper_class) ⇒ Object
Validate that mixed content models have a collection attribute for their content mapping. When mixed_content is enabled, the XML element can contain interleaved text and child elements, resulting in multiple text nodes. The content attribute must be a string collection to hold all of them; otherwise only the first text node is preserved.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/lutaml/xml/mapping.rb', line 143 def validate_mixed_content_collection!(mapper_class) return unless @mixed_content && @content_mapping attr_name = @content_mapping.to return unless attr_name # Follow delegate chain to find the actual attribute definition delegate = @content_mapping.delegate attr_def = if delegate delegate_obj = mapper_class.attributes[delegate] delegate_obj&.type&.attributes&.dig(attr_name) if delegate_obj&.type.is_a?(Class) && delegate_obj&.type.include?(Lutaml::Model::Serialize) else mapper_class.attributes[attr_name] end return unless attr_def return if attr_def.collection? raise Lutaml::Model::MixedContentCollectionError.new(attr_name, mapper_class) end |
#validate_namespace_prefix!(prefix) ⇒ void
This method returns an undefined value.
Validate namespace prefix parameter
Raises ArgumentError if prefix is Hash or Array, which indicates the common mistake of passing options as prefix.
1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/lutaml/xml/mapping.rb', line 1050 def validate_namespace_prefix!(prefix) if prefix.is_a?(Hash) || prefix.is_a?(Array) raise ArgumentError, "namespace prefix must be a String or Symbol, not #{prefix.class}. " \ "Did you mean to use 'root' with mixed: true?" end end |
#validate_namespace_scope!(namespaces) ⇒ void
This method returns an undefined value.
Validate namespace_scope parameter
Ensures all items are XmlNamespace classes or valid Hash entries
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 |
# File 'lib/lutaml/xml/mapping.rb', line 1065 def validate_namespace_scope!(namespaces) unless namespaces.is_a?(Array) raise ArgumentError, "namespace_scope must be an Array of XmlNamespace classes, " \ "got #{namespaces.class}" end namespaces.each do |ns| if ns.is_a?(Class) unless ns < Lutaml::Xml::Namespace raise ArgumentError, "namespace_scope must contain only XmlNamespace classes, " \ "got #{ns}" end elsif ns.is_a?(::Hash) ns_class = ns[:namespace] unless ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace raise ArgumentError, "namespace_scope Hash entry must have :namespace key " \ "with XmlNamespace class, got #{ns_class.class}" end # Validate :declare option if present if ns.key?(:declare) declare_value = ns[:declare] valid_modes = %i[auto always never] unless valid_modes.include?(declare_value) raise ArgumentError, "namespace_scope Hash entry :declare must be one of " \ "#{valid_modes.inspect}, got #{declare_value.inspect}" end end else raise ArgumentError, "namespace_scope must contain only XmlNamespace classes or Hashes, " \ "got #{ns.class}" end end end |
#validate_ordered_content_mapping!(mapper_class) ⇒ Object
164 165 166 167 168 |
# File 'lib/lutaml/xml/mapping.rb', line 164 def validate_ordered_content_mapping!(mapper_class) return unless @ordered && !@mixed_content && @content_mapping raise Lutaml::Model::OrderedContentMappingError.new(mapper_class) end |
#validate_raw_mappings!(type) ⇒ Object
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/lutaml/xml/mapping.rb', line 1025 def validate_raw_mappings!(type) if !@raw_mapping.nil? && type != TYPES[:attribute] raise StandardError, "#{type} is not allowed, only #{TYPES[:attribute]} " \ "is allowed with #{TYPES[:all_content]}" end if type == TYPES[:all_content] && !(elements.empty? && content_mapping.nil?) # lutaml-model#223: map_all may coexist with mapped siblings. # The raw capture receives only the UNCLAIMED content — the # verbatim subtree minus children matched by element rules. # The historical exclusivity raised here; the coexistence # contract is enforced by scoped capture on parse and # remainder-after-mapped emission on serialize. end end |
#validate_to_and_with_arguments!(key, to, with) ⇒ Object
1005 1006 1007 1008 1009 1010 1011 1012 1013 |
# File 'lib/lutaml/xml/mapping.rb', line 1005 def validate_to_and_with_arguments!(key, to, with) if to.nil? && with.empty? raise ::Lutaml::Model::IncorrectMappingArgumentsError.new( ":to or :with argument is required for mapping '#{key}'", ) end (key, to, with) end |
#validate_with_options!(key, to, with) ⇒ Object
1015 1016 1017 1018 1019 1020 1021 1022 1023 |
# File 'lib/lutaml/xml/mapping.rb', line 1015 def (key, to, with) return true if to if !with.empty? && (with[:from].nil? || with[:to].nil?) raise ::Lutaml::Model::IncorrectMappingArgumentsError.new( ":with argument for mapping '#{key}' requires :to and :from keys", ) end end |
#w3c_attributes(*attrs) ⇒ Object
Convenience method for standard W3C XML attributes
Automatically creates attribute mappings for attributes that use W3C types. The attribute must already be declared with a W3C type.
Available options: :lang, :space, :base, :id
293 294 295 296 297 |
# File 'lib/lutaml/xml/mapping.rb', line 293 def w3c_attributes(*attrs) attrs.each do |attr| map_attribute attr.to_s, to: attr end end |