Sha256: b644eb4e30538dc43132a4f8cd1ed1d41693ba5dbe1a2d64ac7d6180eb2d7328

Contents?: true

Size: 1.89 KB

Versions: 125

Compression:

Stored size: 1.89 KB

Contents

module MultiXml
  module Parsers
    module Libxml2Parser #:nodoc:
      # Convert XML document to hash
      #
      # node::
      #   The XML node object to convert to a hash.
      #
      # hash::
      #   Hash to merge the converted element into.
      def node_to_hash(node, hash = {}) # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, PerceivedComplexity
        node_hash = {MultiXml::CONTENT_ROOT => ''}

        name = node_name(node)

        # Insert node hash into parent hash correctly.
        case hash[name]
        when Array
          hash[name] << node_hash
        when Hash
          hash[name] = [hash[name], node_hash]
        when NilClass
          hash[name] = node_hash
        end

        # Handle child elements
        each_child(node) do |c|
          if c.element?
            node_to_hash(c, node_hash)
          elsif c.text? || c.cdata?
            node_hash[MultiXml::CONTENT_ROOT] << c.content
          end
        end

        # Remove content node if it is empty
        if node_hash[MultiXml::CONTENT_ROOT].strip.empty?
          node_hash.delete(MultiXml::CONTENT_ROOT)
        end

        # Handle attributes
        each_attr(node) do |a|
          key = node_name(a)
          v = node_hash[key]
          node_hash[key] = (v ? [a.value, v] : a.value)
        end

        hash
      end

      # Parse an XML Document IO into a simple hash.
      # xml::
      #   XML Document IO to parse
      def parse(_)
        raise(NotImplementedError.new("inheritor should define #{__method__}"))
      end

    private

      def each_child(*)
        raise(NotImplementedError.new("inheritor should define #{__method__}"))
      end

      def each_attr(*)
        raise(NotImplementedError.new("inheritor should define #{__method__}"))
      end

      def node_name(*)
        raise(NotImplementedError.new("inheritor should define #{__method__}"))
      end
    end
  end
end

Version data entries

125 entries across 99 versions & 6 rubygems

Version Path
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
alloy_sdk-0.1.0 vendor/bundle/ruby/2.6.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/paynow_sdk-1.0.9/vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/paynow_sdk-1.0.9/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.1.0 vendor/bundle/ruby/2.7.0/gems/paynow_sdk-1.0.9/vendor/bundle/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.1.0 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.1.0 vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.9 vendor/bundle/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.9 vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.9 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.8 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.8 vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.8 vendor/bundle/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.7 vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.7 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.6 vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.6 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.5 vendor/cache/ruby/2.7.0/gems/paynow_sdk-1.0.0/vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb
paynow_sdk-1.0.5 vendor/cache/ruby/2.7.0/gems/multi_xml-0.6.0/lib/multi_xml/parsers/libxml2_parser.rb