Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

making nokogiri to hash less clever, more fast O_o

[#2243 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit e18752868aec20c74f87dcb4ee64575a405c0f78 1 parent 1488c6c
@tenderlove tenderlove authored jeremy committed
Showing with 12 additions and 20 deletions.
  1. +12 −20 activesupport/lib/active_support/xml_mini/nokogiri.rb
View
32 activesupport/lib/active_support/xml_mini/nokogiri.rb
@@ -12,7 +12,7 @@ def parse(string)
if string.blank?
{}
else
- doc = Nokogiri::XML(string)
+ doc = Nokogiri::XML(data) { |cfg| cfg.noblanks }
raise doc.errors.first if doc.errors.length > 0
doc.to_hash
end
@@ -33,33 +33,25 @@ module Node
# 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
-
- # Recusively 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
Please sign in to comment.
Something went wrong with that request. Please try again.