Module: ActiveSupport::XmlMini_Nokogiri::Conversions::Node

Defined in:
lib/active_support/xml_mini/nokogiri.rb

Constant Summary collapse

CONTENT_ROOT =
'__content__'

Instance Method Summary collapse

Instance Method Details

#attributes_as_hashObject



66
67
68
69
70
# File 'lib/active_support/xml_mini/nokogiri.rb', line 66

def attributes_as_hash
  Hash[*(attribute_nodes.map { |node|
    [node.node_name, node.value]
  }.flatten)]
end

#to_hash(hash = {}) ⇒ Object

Convert XML document to hash

hash

Hash to merge the converted element into.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_support/xml_mini/nokogiri.rb', line 35

def to_hash(hash = {})
  hash[name] ||= attributes_as_hash

  walker = lambda { |memo, parent, child, callback|
    next if child.blank? && 'file' != parent['type']

    if child.text?
      (memo[CONTENT_ROOT] ||= '') << child.content
      next
    end

    name = child.name

    child_hash = child.attributes_as_hash
    if memo[name]
      memo[name] = [memo[name]].flatten
      memo[name] << child_hash
    else
      memo[name] = child_hash
    end

    # Recusively walk children
    child.children.each { |c|
      callback.call(child_hash, child, c, callback)
    }
  }

  children.each { |c| walker.call(hash[name], self, c, walker) }
  hash
end