Permalink
Browse files

Implemented nested to_xml and named elements for array xmling [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3830 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent d872281 commit 0c6d1785529ab2f9c86908f03a10c871512311e5 @dhh dhh committed Mar 9, 2006
@@ -29,13 +29,16 @@ def to_param
def to_xml(options = {})
raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
- options[:root] ||= all? { |e| e.is_a? first.class } ? first.class.to_s.underscore.pluralize : "records"
- options[:indent] ||= 2
- options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
+ options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
+ options[:children] ||= options[:root].singularize
+ options[:indent] ||= 2
+ options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
+
+ root = options.delete(:root)
+ children = options.delete(:children)
options[:builder].instruct! unless options.delete(:skip_instruct)
- root = options.delete(:root)
- options[:builder].__send__(root) { each { |e| e.to_xml(options.merge({ :skip_instruct => true })) } }
+ options[:builder].__send__(root) { each { |e| e.to_xml(options.merge({ :skip_instruct => true, :root => children })) } }
end
end
end
@@ -24,15 +24,18 @@ def to_xml(options = {})
for key in keys
value = self[key]
- if value.is_a?(self.class)
- value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
- else
- type_name = XML_TYPE_NAMES[value.class.to_s]
+ case value.class.to_s # TODO: Figure out why I have to to_s the class to do comparisons in order for tests to run
+ when "Hash"
+ value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
+ when "Array"
+ value.to_xml(options.merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true}))
+ else
+ type_name = XML_TYPE_NAMES[value.class.to_s]
- options[:builder].__send__(key.to_s.dasherize,
- XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
- options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name }
- )
+ options[:builder].__send__(key.to_s.dasherize,
+ XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
+ options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name }
+ )
end
end
end
@@ -75,19 +75,27 @@ def test_to_xml
{ :name => "David", :age => 26 }, { :name => "Jason", :age => 31 }
].to_xml(:skip_instruct => true, :indent => 0)
- assert_equal "<hashes><hash>", xml.first(14)
+ assert_equal "<records><record>", xml.first(17)
assert xml.include?(%(<age type="integer">26</age>))
assert xml.include?(%(<name>David</name>))
assert xml.include?(%(<age type="integer">31</age>))
assert xml.include?(%(<name>Jason</name>))
end
+
+ def test_to_xml_with_dedicated_name
+ xml = [
+ { :name => "David", :age => 26 }, { :name => "Jason", :age => 31 }
+ ].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
+
+ assert_equal "<people><person>", xml.first(16)
+ end
def test_to_xml_with_options
xml = [
{ :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0)
- assert_equal "<hashes><hash>", xml.first(14)
+ assert_equal "<records><record>", xml.first(17)
assert xml.include?(%(<street-address>Paulina</street-address>))
assert xml.include?(%(<name>David</name>))
assert xml.include?(%(<street-address>Evergreen</street-address>))
@@ -209,4 +209,19 @@ def test_two_levels
assert xml.include?(%(<address><street>Paulina</street></address>))
assert xml.include?(%(<name>David</name>))
end
+
+ def test_two_levels_with_array
+ xml = { :name => "David", :addresses => [{ :street => "Paulina" }, { :street => "Evergreen" }] }.to_xml(@xml_options)
+ assert_equal "<person>", xml.first(8)
+ assert xml.include?(%(<addresses><address>))
+ assert xml.include?(%(<address><street>Paulina</street></address>))
+ assert xml.include?(%(<address><street>Evergreen</street></address>))
+ assert xml.include?(%(<name>David</name>))
+ end
+
+
+ def test_three_levels_with_array
+ xml = { :name => "David", :addresses => [{ :streets => [ { :name => "Paulina" }, { :name => "Paulina" } ] } ] }.to_xml(@xml_options)
+ assert xml.include?(%(<addresses><address><streets><street><name>))
+ end
end

0 comments on commit 0c6d178

Please sign in to comment.