Permalink
Browse files

Allow optional attributes with types other than String

This commit adds support for optional attributes (nil is an allowed
value). Without it, HappyMapper will fail with an error similar to:

  TypeError in '#to_xml Address before(:all)'
  wrong argument type Float (expected String)

if an attribute is defined with a type other than String, but
is actually nil, when to_xml is called.

The fix is pretty simple - always calling to_s on the value is the
right thing to do for an attribute, because attributes are always
text in XML.
  • Loading branch information...
1 parent d2351e5 commit e6c2c6b03ef8cbb957caff1fbe0d4dd477191e69 @nerab nerab committed Feb 20, 2014
Showing with 43 additions and 8 deletions.
  1. +1 −1 lib/happymapper.rb
  2. +42 −7 spec/happymapper_to_xml_spec.rb
View
@@ -214,7 +214,7 @@ def to_xml(parent_node = nil, default_namespace = nil)
end
end
- current_node[ "#{attribute_namespace ? "#{attribute_namespace.prefix}:" : ""}#{attribute.tag}" ] = value
+ current_node[ "#{attribute_namespace ? "#{attribute_namespace.prefix}:" : ""}#{attribute.tag}" ] = value.to_s
end
#
@@ -8,6 +8,12 @@ class Address
tag 'address'
attribute :location, String
+ attribute :precipitation, Float
+ attribute :last_update, Time
+ attribute :mayor_elected, Date
+ attribute :last_earthquake, DateTime
+ attribute :revision, Integer
+ attribute :domestic, Boolean
element :street, String
element :postcode, String
@@ -82,7 +88,13 @@ def initialize(parameters)
'postcode' => '98103',
'city' => 'Seattle',
'country' => Country.new(:name => 'USA', :code => 'us'),
- 'date_created' => '2011-01-01 15:00:00')
+ 'date_created' => '2011-01-01 15:00:00',
+ 'precipitation' => 58.3,
+ 'last_update' => Time.new(1993, 02, 24, 12, 0, 0, "+09:00"),
+ 'mayor_elected' => Date.new(2001, 2, 3) ,
+ 'last_earthquake' => DateTime.new(2001, -11, -26, -20, -55, -54, '+7'),
+ 'revision' => 42,
+ 'domestic' => true)
address.dates_updated = ["2011-01-01 16:01:00","2011-01-02 11:30:01"]
@@ -103,10 +115,6 @@ def initialize(parameters)
@address_xml.find("housenumber").first.child.to_s.should == "[1313]"
end
- it "should have the attribute 'location' with the value 'Home'" do
- @address_xml.find('@location').first.child.to_s.should == "Home"
- end
-
it "should add an empty description element" do
@address_xml.find('description').first.child.to_s.should == ""
end
@@ -130,9 +138,36 @@ def initialize(parameters)
@address_xml.find('country/countryName').first.child.to_s.should == "USA"
end
- end
+ it "should have the attribute 'location' with the value 'Home'" do
+ @address_xml.find('@location').first.child.to_s.should == 'Home'
+ end
+
+ it "should have the attribute 'precipitation' with the value '58.3'" do
+ @address_xml.find('@precipitation').first.child.to_s.should == '58.3'
+ end
+
+ it "should have the attribute 'last_update' with the value '1993-02-24 12:00:00 +0900'" do
+ @address_xml.find('@last_update').first.child.to_s.should == "1993-02-24 12:00:00 +0900"
+ end
+ it "should have the attribute 'mayor_elected' with the value '2001-02-03'" do
+ @address_xml.find('@mayor_elected').first.child.to_s.should == '2001-02-03'
+ end
+
+ it "should have the attribute 'last_earthquake' with the value ''" do
+ @address_xml.find('@last_earthquake').first.child.to_s.should == '2001-02-03T04:05:06+07:00'
+ end
+
+ it "should have the attribute 'revision' with the value '42'" do
+ @address_xml.find('@revision').first.child.to_s.should == '42'
+ end
+
+ it "should have the attribute 'domestic' with the value 'true'" do
+ @address_xml.find('@domestic').first.child.to_s.should == 'true'
+ end
+
+ end
end
-end
+end

0 comments on commit e6c2c6b

Please sign in to comment.