Skip to content
Browse files

Fixing generation arrays representation in xml.

  • Loading branch information...
1 parent f7b9efd commit b9909fed73a5e0a33d412fd093d1c15829e3e17c @nuxlli nuxlli committed Feb 16, 2011
Showing with 33 additions and 6 deletions.
  1. +9 −6 lib/tokamak/builder/xml.rb
  2. +24 −0 test/tokamak/builder/xml_test.rb
View
15 lib/tokamak/builder/xml.rb
@@ -50,12 +50,15 @@ def link(relationship, uri, options = {})
end
def insert_value(name, prefix, *args, &block)
- node = create_element(name.to_s, prefix, *args)
- node.parent = @parent
- if block_given?
- @parent = node
- block.call
- @parent = node.parent
+ # Protected if empty array
+ unless args.size == 1 and args.first == []
+ node = create_element(name.to_s, prefix, *args)
+ node.parent = @parent
+ if block_given?
+ @parent = node
+ block.call
+ @parent = node.parent
+ end
end
end
View
24 test/tokamak/builder/xml_test.rb
@@ -108,6 +108,30 @@ def test_root_set_on_members
assert_equal "bar" , xml.css("root > foos > id").first.text
assert_equal 2 , xml.css("root > foos > id").size
end
+
+ def test_values_that_should_be_an_array
+ obj = [{ :foo => ["bar", "zoom"] }]
+ xml = Tokamak::Builder::Xml.build(obj) do |collection|
+ collection.values do |values|
+ values.id "an_id"
+ end
+
+ collection.members do |member, some_foos|
+ member.values do |values|
+ values.ids []
+ some_foos[:foo].each do |id|
+ values.ids id
+ end
+ end
+ end
+ end
+
+ xml = Nokogiri::XML::Document.parse(xml)
+
+ assert_equal "root" , xml.root.name
+ assert_equal "an_id", xml.css("root id").first.text
+ assert_equal ["bar", "zoom"], xml.css("root > member > ids").map { |id| id.text }
+ end
def test_nested_crazy_values
obj = [{ :foo => "bar" }, { :foo => "zue" }]

0 comments on commit b9909fe

Please sign in to comment.
Something went wrong with that request. Please try again.