Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #8 from outright/master

More consistent behavior WRT single objects vs arrays
  • Loading branch information...
commit a98e55654112f65b47f1f7cd0266c232e8830fd1 2 parents e814ab8 + 4fb2929
@burtlo burtlo authored
Showing with 23 additions and 3 deletions.
  1. +4 −1 lib/happymapper.rb
  2. +19 −2 spec/happymapper_spec.rb
View
5 lib/happymapper.rb
@@ -211,7 +211,7 @@ def parse(xml, options = {})
namespace = @namespace
# If the XML specified is an Node then we have what we need.
- if xml.is_a?(Nokogiri::XML::Node)
+ if xml.is_a?(Nokogiri::XML::Node) && !xml.is_a?(Nokogiri::XML::Document)
node = xml
else
@@ -289,6 +289,9 @@ def parse(xml, options = {})
nodes
end
+ # Nothing matching found, we can go ahead and return
+ return ( ( options[:single] || root ) ? nil : [] ) if nodes.size == 0
+
# If the :limit option has been specified then we are going to slice
# our node results by that amount to allow us the ability to deal with
# a large result set of data.
View
21 spec/happymapper_spec.rb
@@ -601,7 +601,7 @@ class Boo; include HappyMapper end
element = @klass.elements.first
element.name.should == 'user'
element.type.should == User
- element.options[:single] = true
+ element.options[:single].should == true
end
it "should allow has many association" do
@@ -609,7 +609,7 @@ class Boo; include HappyMapper end
element = @klass.elements.first
element.name.should == 'users'
element.type.should == User
- element.options[:single] = false
+ element.options[:single].should == false
end
it "should default tag name to lowercase class" do
@@ -700,6 +700,12 @@ module Bar; class Baz; include HappyMapper; end; end
address.country.code.should == 'de'
end
+ it "should treat Nokogiri::XML::Document as root" do
+ doc = Nokogiri::XML(fixture_file('address.xml'))
+ address = Address.parse(doc)
+ address.class.should == Address
+ end
+
it "should parse xml with default namespace (amazon)" do
file_contents = fixture_file('pita.xml')
items = PITA::Items.parse(file_contents, :single => true)
@@ -732,6 +738,17 @@ module Bar; class Baz; include HappyMapper; end; end
feed.link.last.href.should == 'http://www.example.com/tv_shows.atom'
end
+ it "returns nil rather than empty array for absent values when :single => true" do
+ address = Address.parse('<?xml version="1.0" encoding="UTF-8"?><foo/>', :single => true)
+ address.should be_nil
+ end
+
+ it "should return same result for absent values when :single => true, regardless of :in_groups_of" do
+ addr1 = Address.parse('<?xml version="1.0" encoding="UTF-8"?><foo/>', :single => true)
+ addr2 = Address.parse('<?xml version="1.0" encoding="UTF-8"?><foo/>', :single => true, :in_groups_of => 10)
+ addr1.should == addr2
+ end
+
it "should parse xml with nested elements" do
radars = Radar.parse(fixture_file('radar.xml'))
first = radars[0]
Please sign in to comment.
Something went wrong with that request. Please try again.