Module: Lutaml::Xml::Adapter::AdapterHelpers
- Included in:
- BaseAdapter
- Defined in:
- lib/lutaml/xml/adapter/adapter_helpers.rb
Overview
Shared helper methods for XML adapters.
This module provides common functionality used by both OgaAdapter and RexmlAdapter to reduce code duplication.
Constant Summary collapse
- TEXT_CLASSES =
Text node classes that should be handled specially
[Moxml::Text, Moxml::Cdata].freeze
Instance Method Summary collapse
-
#name_of(element) ⇒ String?
Get the name of an XML element.
-
#namespaced_attr_name(attribute) ⇒ String
Get the namespaced attribute name.
-
#namespaced_name_of(node) ⇒ String
Get the namespaced name of an XML node.
-
#node_type_of(element) ⇒ Symbol?
Get the node type of an XML element.
-
#prefixed_name_of(node) ⇒ String
Get the prefixed name of an XML node.
-
#text_node?(node) ⇒ Boolean
Check if a node is a text node.
Instance Method Details
#name_of(element) ⇒ String?
Get the name of an XML element
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 23 def name_of(element) return nil if element.nil? case element when Moxml::Text "text" when Moxml::Cdata "#cdata-section" when Moxml::ProcessingInstruction element.target when Moxml::Comment "comment" else element.name end end |
#namespaced_attr_name(attribute) ⇒ String
Get the namespaced attribute name
78 79 80 81 82 83 84 85 86 |
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 78 def namespaced_attr_name(attribute) attr_ns = attribute.namespace attr_name = attribute.name return attr_name unless attr_ns # Special handling for xml:lang - use prefix instead of URI prefix = attr_name == "lang" ? attr_ns.prefix : attr_ns.uri [prefix, attr_name].compact.join(":") end |
#namespaced_name_of(node) ⇒ String
Get the namespaced name of an XML node
92 93 94 95 96 |
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 92 def namespaced_name_of(node) return name_of(node) unless node.respond_to?(:namespace) [node&.namespace&.uri, node.name].compact.join(":") end |
#node_type_of(element) ⇒ Symbol?
Get the node type of an XML element
This returns the actual node type based on the underlying library’s classification, not inferred from the element name.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 47 def node_type_of(element) return nil if element.nil? case element when Moxml::Text :text when Moxml::Cdata :cdata when Moxml::ProcessingInstruction :processing_instruction when Moxml::Comment :comment else :element end end |
#prefixed_name_of(node) ⇒ String
Get the prefixed name of an XML node
68 69 70 71 72 |
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 68 def prefixed_name_of(node) return name_of(node) if TEXT_CLASSES.include?(node.class) [node&.namespace&.prefix, node.name].compact.join(":") end |
#text_node?(node) ⇒ Boolean
Check if a node is a text node
102 103 104 |
# File 'lib/lutaml/xml/adapter/adapter_helpers.rb', line 102 def text_node?(node) TEXT_CLASSES.include?(node.class) end |