Sha256: 7398ba84c658f07e74679b864b83dceacc24d32348f156f6c26a58e8c9a9f85c

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 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 children.any?
        children_hash
      else
        node.text.to_s
      end
    end

    def children
      @children ||= node.children.select(&:element?)
    end

    private

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

        if Array === hash[child.name] || !element.singular?
          hash[child.name] ||= []
          hash[child.name] << output
        else
          if hash.include?(child.name)
            hash[child.name] = [hash[child.name]]
            hash[child.name] << output
          else
            hash[child.name] = output
          end
        end
      end
      hash
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lolsoap-0.1.1 lib/lolsoap/hash_builder.rb
lolsoap-0.1.0 lib/lolsoap/hash_builder.rb