class DarwinCore::XmlReader::Node

Node is a helper class to parse xml into hash

Public Class Methods

new(node) click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 16
def initialize(node)
  @node = node
  @val = {}
end

Public Instance Methods

value() click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 21
def value
  if @node.element?
    prepare_node_element
  else
    prepare(@node.content.to_s)
  end
end

Private Instance Methods

add_attribute(attributes, attribute) click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 50
def add_attribute(attributes, attribute)
  attributes[attribute.name.to_sym] = prepare(attribute.value)
end
add_attributes() click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 41
def add_attributes
  return if @node.attributes.empty?

  @val[:attributes] = {}
  @node.attributes.each_key do |key|
    add_attribute(@val[:attributes], @node.attributes[key])
  end
end
add_child_to_value(child, value) click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 69
def add_child_to_value(child, value)
  if @val[child.name.to_sym]
    handle_child_node(child.name.to_sym, value)
  else
    @val[child.name.to_sym] = prepare(value)
  end
end
add_children() click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 54
def add_children
  @node.children.each do |child|
    process_child(child)
  end
end
handle_child_node(child, val) click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 77
def handle_child_node(child, val)
  if @val[child].is_a?(Object::Array)
    @val[child] << prepare(val)
  else
    @val[child] = [@val[child], prepare(val)]
  end
end
handle_text(child, val) click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 85
def handle_text(child, val)
  @val = prepare(val) unless child.next_sibling || child.previous_sibling
end
prepare(data) click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 37
def prepare(data)
  data.instance_of?(String) && data.to_i.to_s == data ? data.to_i : data
end
prepare_node_element() click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 31
def prepare_node_element
  add_attributes
  add_children if @node.children.size.positive?
  @val
end
process_child(child) click to toggle source
# File lib/dwc_archive/xml_reader.rb, line 60
def process_child(child)
  value = DarwinCore::XmlReader::Node.new(child).value
  if child.name == "text"
    handle_text(child, value)
  else
    add_child_to_value(child, value)
  end
end