Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow optional attributes with types other than String #31

Merged
merged 2 commits into from

2 participants

@nerab

This PR adds support for optional attributes with a type other than String.

Before, if an attribute was defined with a type other than String and was nil, to_xml failed with an error similar to:

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

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.

There is an additional change that sets the Gemfile source to https://rubygems.org.

nerab added some commits
@nerab nerab Change source to the recommended https://rubygems.org value d2351e5
@nerab nerab 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.
e6c2c6b
@jnunemaker jnunemaker merged commit ab86093 into jnunemaker:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 20, 2014
  1. @nerab
  2. @nerab

    Allow optional attributes with types other than String

    nerab authored
    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.
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 10 deletions.
  1. +2 −2 Gemfile
  2. +1 −1  lib/happymapper.rb
  3. +42 −7 spec/happymapper_to_xml_spec.rb
View
4 Gemfile
@@ -1,8 +1,8 @@
-source :rubygems
+source 'https://rubygems.org'
gemspec
group :development do
gem 'rake'
gem "rspec", "~> 1.3"
-end
+end
View
2  lib/happymapper.rb
@@ -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
#
View
49 spec/happymapper_to_xml_spec.rb
@@ -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
Something went wrong with that request. Please try again.