Browse files

Tests & Fix for cascading namespace

  Found that when you have an object that is the same
  structure but a differing namespace for two classes
  that the namespace designation that you provide for
  that class did not cascade down and become the default
  • Loading branch information...
1 parent 249fa8d commit fd1e898c70f7289d2d2618d629b56f2f6623785c @burtlo committed Jan 20, 2011
Showing with 101 additions and 1 deletion.
  1. +6 −1 lib/happymapper.rb
  2. +28 −0 spec/fixtures/subclass_namespace.xml
  3. +67 −0 spec/happymapper_spec.rb
View
7 lib/happymapper.rb
@@ -101,7 +101,9 @@ def parse(xml, options = {})
namespaces = namespaces.merge(xml.collect_namespaces) if xml.respond_to?(:collect_namespaces)
namespaces = namespaces.merge(@registered_namespaces)
- if namespaces.has_key?("xmlns")
+ if options[:namespace]
+ namespace = options[:namespace]
+ elsif namespaces.has_key?("xmlns")
namespace ||= DEFAULT_NS
namespaces[namespace] = namespaces.delete("xmlns")
elsif namespaces.has_key?(DEFAULT_NS)
@@ -120,6 +122,8 @@ def parse(xml, options = {})
# 1. specified tag
# 2. name of element
# 3. tag_name (derived from class name by default)
+
+
[options[:tag], options[:name], tag_name].compact.each do |xpath_ext|
nodes = node.xpath(xpath + xpath_ext.to_s, namespaces)
break if nodes && !nodes.empty?
@@ -128,6 +132,7 @@ def parse(xml, options = {})
nodes
end
+
collection = nodes.collect do |n|
obj = new
View
28 spec/fixtures/subclass_namespace.xml
@@ -0,0 +1,28 @@
+<article:Article xmlns:article="http://www.wetpaint.com/alfresco/article">
+ <article:title>article title</article:title>
+ <article:text>article text</article:text>
+ <article:publishOptions>
+ <article:author>Nathan</article:author>
+ <article:draft>false</article:draft>
+ <article:scheduledDay>2011-01-14</article:scheduledDay>
+ <article:scheduledTime>11:31:45.0</article:scheduledTime>
+ <article:publishDisplayDay>2011-01-14</article:publishDisplayDay>
+ <article:publishDisplayTime>11:31:45.0</article:publishDisplayTime>
+ <article:createdDay>2011-01-14</article:createdDay>
+ <article:createdTime>11:52:24.0</article:createdTime>
+ </article:publishOptions>
+
+ <photo:Photo xmlns:photo="http://www.wetpaint.com/alfresco/photo">
+ <photo:title>photo title</photo:title>
+ <photo:publishOptions>
+ <photo:author>Stephanie</photo:author>
+ <photo:draft>false</photo:draft>
+ <photo:scheduledDay>2011-01-13</photo:scheduledDay>
+ <photo:scheduledTime>15:47:30.0</photo:scheduledTime>
+ <photo:publishDisplayDay>2011-01-13</photo:publishDisplayDay>
+ <photo:publishDisplayTime>15:47:30.0</photo:publishDisplayTime>
+ <photo:createdDay>2011-01-13</photo:createdDay>
+ <photo:createdTime>15:51:47.0</photo:createdTime>
+ </photo:publishOptions>
+ </photo:Photo>
+</article:Article>
View
67 spec/happymapper_spec.rb
@@ -423,6 +423,49 @@ class Item
end
end
+class PublishOptions
+ include HappyMapper
+
+ tag 'publishOptions'
+
+ element :author, String, :tag => 'author'
+
+ element :draft, Boolean, :tag => 'draft'
+ element :scheduled_day, String, :tag => 'scheduledDay'
+ element :scheduled_time, String, :tag => 'scheduledTime'
+ element :published_day, String, :tag => 'publishDisplayDay'
+ element :published_time, String, :tag => 'publishDisplayTime'
+ element :created_day, String, :tag => 'publishDisplayDay'
+ element :created_time, String, :tag => 'publishDisplayTime'
+
+end
+
+class Article
+ include HappyMapper
+
+ tag 'Article'
+ namespace 'article'
+
+ element :title, String
+ element :text, String
+ has_many :photos, 'Photo', :tag => 'Photo', :namespace => 'photo'
+
+ element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'article'
+
+end
+
+
+class Photo
+ include HappyMapper
+
+ tag 'Photo'
+ namespace 'photo'
+
+ element :title, String
+ element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'photo'
+
+end
+
describe HappyMapper do
describe "being included into another class" do
@@ -793,4 +836,28 @@ class Thing
:xpath => '/ambigous/my-items')
items.map(&:name).should == %w(first second third).map{|s| "My #{s} item" }
end
+
+
+ context Article do
+ it "should parse the publish options for Article and Photo" do
+ @article.title.should_not be_nil
+ @article.text.should_not be_nil
+ @article.photos.should_not be_nil
+ @article.photos.first.title.should_not be_nil
+ end
+
+ it "should parse the publish options for Article" do
+ @article.publish_options.should_not be_nil
+ end
+
+ it "should parse the publish options for Photo" do
+ @article.photos.first.publish_options.should_not be_nil
+ end
+
+ before(:all) do
+ @article = Article.parse(fixture_file('subclass_namespace.xml'))
+ end
+
+ end
+
end

0 comments on commit fd1e898

Please sign in to comment.