Module: XML::Mapping::ClassMethods

Defined in:
lib/xml/mapping_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#root_element_namesHash[Symbol, String] (readonly)

Gets the configured root element names for this object.

Returns:

  • (Hash[Symbol, String])

    the root element names for this object, by mapping.



99
100
101
# File 'lib/xml/mapping_extensions.rb', line 99

def root_element_names
  @root_element_names
end

Instance Method Details

#fallback_mapping(mapping, fallback)

Configures one mapping as a fallback for another, allowing mappings that differ e.g. from the default only in some small detail. Creates mapping nodes based on the fallback for all attributes for which a mapping node is present in the fallback but not in the primary, and likewise sets the root element name for the primary to the root element name for the fallback if it has not already been set.

Parameters:

  • mapping (Symbol)

    the primary mapping

  • fallback (Symbol)

    the fallback mapping to be used if the primary mapping lacks a mapping node or root element name



128
129
130
131
132
133
134
135
# File 'lib/xml/mapping_extensions.rb', line 128

def fallback_mapping(mapping, fallback)
  mapping_nodes = add_fallback_nodes(nodes_by_attrname(mapping), mapping, fallback)
  xml_mapping_nodes_hash[mapping] = mapping_nodes.values

  return if root_element_names[mapping]
  fallback_name = root_element_names[fallback]
  root_element_name(fallback_name, mapping: mapping)
end

#parse_xml(xml, options = { mapping: :_default }) ⇒ Object

Create a new instance of this class from the XML contained in xml, which can be a string, REXML document, or REXML element

Parameters:

  • xml (String, REXML::Document, REXML::Element)

Returns:

  • (Object)

    an instance of this class.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/xml/mapping_extensions.rb', line 105

def parse_xml(xml, options = { mapping: :_default })
  element = case xml
            when REXML::Document
              xml.root
            when REXML::Element
              xml
            else
              raise ArgumentError, "Unexpected argument type; expected XML document, String, or IO source, was #{xml.class}" unless can_parse(xml)
              REXML::Document.new(xml).root
            end
  load_from_xml(element, options)
end