Sha256: c07f5486b4c33b5455b25fbaa6a8c1fb7b98cf49745230682bed5dbda6ff6e10

Contents?: true

Size: 1.22 KB

Versions: 20

Compression:

Stored size: 1.22 KB

Contents

module LolSoap
  # Turns an XML node into a hash data structure. Works out which elements
  # are supposed to be collections based on the type information.
  class HashBuilder
    attr_reader :node, :type

    def initialize(node, type)
      @node = node
      @type = type
    end

    def output
      if node.first_element_child
        children_hash
      else
        content
      end
    end

    private

    # @private
    def children_hash
      hash = {}
      node.element_children.each do |child|
        element = type.element(child.name)
        output  = self.class.new(child, element.type).output
        val     = hash[child.name]

        if output
          if val
            if val.is_a?(Array)
              val << output
            else
              hash[child.name] = [val, output]
            end
          else
            hash[child.name] = element.singular? ? output : [output]
          end
        else
          hash[child.name] = element.singular? ? nil : []
        end
      end
      hash
    end

    # @private
    def content
      node.text.to_s unless nil_value?
    end

    # @private
    def nil_value?
      node.attribute_with_ns('nil', 'http://www.w3.org/2001/XMLSchema-instance')
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
lolsoap-0.11.0 lib/lolsoap/hash_builder.rb
lolsoap-0.10.0 lib/lolsoap/hash_builder.rb
lolsoap-0.9.0 lib/lolsoap/hash_builder.rb
lolsoap-0.8.3 lib/lolsoap/hash_builder.rb
lolsoap-0.8.2 lib/lolsoap/hash_builder.rb
lolsoap-0.8.1 lib/lolsoap/hash_builder.rb
lolsoap-0.8.0 lib/lolsoap/hash_builder.rb
lolsoap-0.7.0 lib/lolsoap/hash_builder.rb
lolsoap-0.6.1 lib/lolsoap/hash_builder.rb
lolsoap-0.6.0 lib/lolsoap/hash_builder.rb
lolsoap-0.5.1 lib/lolsoap/hash_builder.rb
lolsoap-0.5.0 lib/lolsoap/hash_builder.rb
lolsoap-0.4.2 lib/lolsoap/hash_builder.rb
lolsoap-0.4.1 lib/lolsoap/hash_builder.rb
lolsoap-0.4.0 lib/lolsoap/hash_builder.rb
lolsoap-0.3.2 lib/lolsoap/hash_builder.rb
lolsoap-0.3.1 lib/lolsoap/hash_builder.rb
lolsoap-0.3.0 lib/lolsoap/hash_builder.rb
lolsoap-0.2.0 lib/lolsoap/hash_builder.rb
lolsoap-0.1.4 lib/lolsoap/hash_builder.rb