class XmlHasher::Node

Attributes

attributes[RW]
children[RW]
name[RW]
text[RW]

Public Class Methods

new(name) click to toggle source
# File lib/xmlhasher/node.rb, line 7
def initialize(name)
  @name = name
  @attributes = {}
  @children = []
end

Public Instance Methods

to_hash() click to toggle source
# File lib/xmlhasher/node.rb, line 13
def to_hash
  node_content = content
  { name => node_content.empty? ? nil : node_content }
end

Private Instance Methods

attributes_to_hash() click to toggle source
# File lib/xmlhasher/node.rb, line 26
def attributes_to_hash
  attributes.each_with_object({}) do |(key, value), data|
    next if value.nil? || value.to_s.empty?

    data[key] = value
  end
end
children_to_hash() click to toggle source
# File lib/xmlhasher/node.rb, line 34
def children_to_hash
  return children.first.to_hash if children.size == 1

  children.group_by(&:name).each_with_object({}) do |(key, nodes), data|
    next data.merge!(nodes.first.to_hash) if nodes.length == 1

    data[key] = nodes.map do |node|
      node.to_hash[node.name]
    end
  end
end
content() click to toggle source
# File lib/xmlhasher/node.rb, line 20
def content
  return text if text && !text.empty?

  attributes_to_hash.merge(children_to_hash)
end