Sha256: 8f98152a8f48da56c4743254d42458378f255ee66d354e0b53531df7e4bba337

Contents?: true

Size: 783 Bytes

Versions: 1

Compression:

Stored size: 783 Bytes

Contents

# frozen_string_literal: true

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xmlhasher-1.0.5 lib/xmlhasher/node.rb