Sha256: 379bb34e77d983e98fbb5f88f7fa884d33049c30ebc57f09e77d44b5e28a5605

Contents?: true

Size: 639 Bytes

Versions: 15

Compression:

Stored size: 639 Bytes

Contents

class Hashify
  ##
  # Return a Hash from a Nokogiri::XML::Document.
  #
  # +node+: An Nokogiri::XML::Document.

  def self.convert node
    children    = {}
    child_nodes = node.children

    if child_nodes.first.nil?
      children = nil
    elsif child_nodes.first.text?
      children = child_nodes.first.text
    else
      child_nodes.each do |child|
        convert(child).each_pair do |k, v|
          children[k] = if children.key? k
            children[k].is_a?(Array) ? (children[k] << v) : ([children[k], v])
          else
            v
          end
        end
      end
    end

    { node.name => children }
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
nephophobia-0.3.6 lib/hashify.rb
nephophobia-0.3.0 lib/hashify.rb
nephophobia-0.2.0 lib/hashify.rb
nephophobia-0.1.2 lib/hashify.rb
nephophobia-0.1.1 lib/hashify.rb
nephophobia-0.1.0 lib/hashify.rb
nephophobia-0.0.9 lib/hashify.rb
nephophobia-0.0.8 lib/hashify.rb
nephophobia-0.0.7 lib/hashify.rb
nephophobia-0.0.6 lib/hashify.rb
nephophobia-0.0.5 lib/hashify.rb
nephophobia-0.0.4 lib/hashify.rb
nephophobia-0.0.3 lib/hashify.rb
nephophobia-0.0.2 lib/hashify.rb
nephophobia-0.0.1 lib/hashify.rb