class DynportTools::XmlFile

Attributes

content[RW]
path[RW]

Public Class Methods

new(path_or_options = nil) click to toggle source
# File lib/dynport_tools/xml_file.rb, line 6
def initialize(path_or_options = nil)
  if path_or_options.is_a?(Hash)
    self.path = path_or_options[:path].to_s if path_or_options[:path].to_s
    self.content = path_or_options[:content]
  elsif !path_or_options.nil?
    self.path = path_or_options.to_s
  end
end

Public Instance Methods

doc() click to toggle source
# File lib/dynport_tools/xml_file.rb, line 19
def doc
  @doc ||= Nokogiri::XML(content || File.open(path))
end
flatten_hash(in_hash) click to toggle source
# File lib/dynport_tools/xml_file.rb, line 49
def flatten_hash(in_hash)
  in_hash.inject({}) do |hash, (key, arr_of_value)|
    if arr_of_value.is_a?(Array)
      if arr_of_value.length == 0
        hash[key] = nil
      elsif arr_of_value.length == 1
        hash[key] = arr_of_value.first
      else
        hash[key] = arr_of_value
      end
    else
      hash[key] = arr_of_value
    end
    hash
  end
end
key_for_node(node) click to toggle source
# File lib/dynport_tools/xml_file.rb, line 39
def key_for_node(node)
  if node.attributes.any?
    node.attributes.inject({ :name => node.name }) do |hash, (key, value)|
      hash.merge!(key => value.value)
    end
  else
    node.name
  end
end
nodes_hash() click to toggle source
# File lib/dynport_tools/xml_file.rb, line 15
def nodes_hash
  { key_for_node(doc.root) => parse_node(doc.root) }
end
parse_node(node) click to toggle source
# File lib/dynport_tools/xml_file.rb, line 23
def parse_node(node)
  child_elements = node.children.select { |n| n.is_a?(Nokogiri::XML::Element) }
  value = if child_elements.any?
    flatten_hash(
      child_elements.inject({}) do |hash, el|
        hash[key_for_node(el)] ||= Array.new
        hash[key_for_node(el)] << parse_node(el)
        hash
      end
    )
  else
    txt = node.inner_text.strip
    txt.length == 0 ? nil : txt
  end
end