Sha256: 9c08c46863ed5fbb981d47d5788c6c0052b1ce4d760dbdcf5453d34d783775a0

Contents?: true

Size: 1020 Bytes

Versions: 2

Compression:

Stored size: 1020 Bytes

Contents

module XmlHasher
  class Node
    attr_accessor :name, :attributes, :children, :text

    def initialize(name)
      @name = name
      @attributes = {}
      @children = []
    end

    def to_hash
      h = {}
      if text
        if clean_attributes.empty?
          h[name] = text
        else
          h[name] = clean_attributes.merge(value: text)
        end
      else
        h[name] = clean_attributes
        if children.size == 1
          child = children.first
          h[name].merge!(child.to_hash)
        else
          h[name].merge!(children.group_by { |c| c.name }.inject({}) { |r, (k, v)| v.length == 1 ? r.merge!(v.first.to_hash) : r[k] = v.map { |c| c.to_hash[c.name] }; r })
        end
      end
      h[name] = nil if h[name].empty?
      h
    end

    private

    def clean_attributes
      return @clean_attributes if defined? @clean_attributes
      @clean_attributes = attributes.inject({}) { |r, (key, value)| r[key] = value if !value.nil? && !value.to_s.empty?; r }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xmlhasher_with_attributes-1.0.1 lib/xmlhasher/node.rb
xmlhasher_with_attributes-1.0.0 lib/xmlhasher/node.rb