Skip to content

Commit

Permalink
making nokogiri to hash less clever, more fast O_o
Browse files Browse the repository at this point in the history
[#2243 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
tenderlove authored and jeremy committed Sep 17, 2009
1 parent b5dd1b6 commit 636624f
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions activesupport/lib/active_support/xml_mini/nokogiri.rb
Expand Up @@ -18,7 +18,7 @@ def parse(data)
{}
else
data.ungetc(char)
doc = Nokogiri::XML(data)
doc = Nokogiri::XML(data) { |cfg| cfg.noblanks }
raise doc.errors.first if doc.errors.length > 0
doc.to_hash
end
Expand All @@ -39,33 +39,25 @@ module Node #:nodoc:
# hash::
# Hash to merge the converted element into.
def to_hash(hash = {})
hash[name] ||= attributes_as_hash
attributes = attributes_as_hash
if hash[name]
hash[name] = [hash[name]].flatten
hash[name] << attributes
else
hash[name] ||= attributes
end

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

if child.text? || child.cdata?
(memo[CONTENT_ROOT] ||= '') << child.content
(attributes[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

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

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

Expand Down

0 comments on commit 636624f

Please sign in to comment.