Skip to content

Commit

Permalink
Have parsers refer to toplevel CONTENT_ROOT instead of defining it.
Browse files Browse the repository at this point in the history
  • Loading branch information
phiggins committed Jun 6, 2011
1 parent a5dac06 commit 94e6fa4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
10 changes: 4 additions & 6 deletions lib/multi_xml/parsers/libxml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ def to_hash
end

module Node #:nodoc:
CONTENT_ROOT = '__content__'.freeze unless defined?(CONTENT_ROOT)

# Convert XML document to hash
#
# hash::
# Hash to merge the converted element into.
def to_hash(hash={})
node_hash = {CONTENT_ROOT => ''}
node_hash = {MultiXml::CONTENT_ROOT => ''}

# Insert node hash into parent hash correctly.
case hash[name]
Expand All @@ -56,13 +54,13 @@ def to_hash(hash={})
if c.element?
c.to_hash(node_hash)
elsif c.text? || c.cdata?
node_hash[CONTENT_ROOT] << c.content
node_hash[MultiXml::CONTENT_ROOT] << c.content
end
end

# Remove content node if it is empty
if node_hash[CONTENT_ROOT].strip.empty?
node_hash.delete(CONTENT_ROOT)
if node_hash[MultiXml::CONTENT_ROOT].strip.empty?
node_hash.delete(MultiXml::CONTENT_ROOT)
end

# Handle attributes
Expand Down
10 changes: 4 additions & 6 deletions lib/multi_xml/parsers/nokogiri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ def to_hash
end

module Node #:nodoc:
CONTENT_ROOT = '__content__'.freeze unless defined?(CONTENT_ROOT)

# Convert XML document to hash
#
# hash::
# Hash to merge the converted element into.
def to_hash(hash={})
node_hash = {CONTENT_ROOT => ''}
node_hash = {MultiXml::CONTENT_ROOT => ''}

# Insert node hash into parent hash correctly.
case hash[name]
Expand All @@ -54,13 +52,13 @@ def to_hash(hash={})
if c.element?
c.to_hash(node_hash)
elsif c.text? || c.cdata?
node_hash[CONTENT_ROOT] << c.content
node_hash[MultiXml::CONTENT_ROOT] << c.content
end
end

# Remove content node if it is empty
if node_hash[CONTENT_ROOT].strip.empty?
node_hash.delete(CONTENT_ROOT)
if node_hash[MultiXml::CONTENT_ROOT].strip.empty?
node_hash.delete(MultiXml::CONTENT_ROOT)
end

# Handle attributes
Expand Down
4 changes: 1 addition & 3 deletions lib/multi_xml/parsers/rexml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module Rexml #:nodoc:
extend self
def parse_error; ::REXML::ParseException; end

CONTENT_ROOT = '__content__'.freeze unless defined?(CONTENT_ROOT)

# Parse an XML Document string or IO into a simple hash using REXML
#
# xml::
Expand Down Expand Up @@ -72,7 +70,7 @@ def merge_texts!(hash, element)
# must use value to prevent double-escaping
texts = ''
element.texts.each { |t| texts << t.value }
merge!(hash, CONTENT_ROOT, texts)
merge!(hash, MultiXml::CONTENT_ROOT, texts)
end
end

Expand Down

0 comments on commit 94e6fa4

Please sign in to comment.