Permalink
Browse files

Fixed parsing when the object is the root node.

Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information...
1 parent 36b5d4b commit 03d44d496c6a91a23613078e8f3325488ff38902 @galfert galfert committed with Dec 10, 2008
Showing with 34 additions and 1 deletion.
  1. +1 −1 lib/happymapper.rb
  2. +8 −0 spec/fixtures/address.xml
  3. +25 −0 spec/happymapper_spec.rb
View
@@ -73,7 +73,7 @@ def parse(xml, o={})
node.register_default_namespace(namespace.chop)
node.find("#{namespace}#{get_tag_name}")
else
- doc.find(get_tag_name)
+ doc.find("//#{get_tag_name}")
end
collection = create_collection(nodes, namespace)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<address>
+ <street>Milchstrasse</street>
+ <housenumber>23</housenumber>
+ <postcode>26131</postcode>
+ <city>Oldenburg</city>
+ <country code="de">Germany</country>
+</address>
View
@@ -69,6 +69,16 @@ class Items
end
end
+class Address
+ include HappyMapper
+
+ element :street, String
+ element :postcode, String
+ element :housenumber, String
+ element :city, String
+ element :country, String
+end
+
describe HappyMapper do
describe "being included into another class" do
@@ -202,6 +212,21 @@ class Foo; include HappyMapper end
first.user.followers_count.should == 486
end
end
+
+ describe "#parse (with xml containing the desired element as root node)" do
+ before do
+ file_contents = File.read(File.dirname(__FILE__) + '/fixtures/address.xml')
+ @address = Address.parse(file_contents, :single => true)
+ end
+
+ it "should properly create objects" do
+ @address.street.should == 'Milchstrasse'
+ @address.postcode.should == '26131'
+ @address.housenumber.should == '23'
+ @address.city.should == 'Oldenburg'
+ @address.country.should == 'Germany'
+ end
+ end
# TODO: someone please get xml with namespaces working, kthxbai
describe "#parse (with xml that has namespace)" do

0 comments on commit 03d44d4

Please sign in to comment.