Permalink
Browse files

Rename configuration callback for nokogiri to be more explicit

  • Loading branch information...
1 parent 9f3f013 commit def71a1e5d635fe430a68ef59c2e1bf28a0d318f @dam5s dam5s committed Oct 30, 2012
Showing with 71 additions and 68 deletions.
  1. +10 −7 lib/happymapper.rb
  2. +61 −61 spec/happymapper_spec.rb
View
@@ -232,21 +232,21 @@ def #{item.method_name}=(value)
has_one name, wrapper
end
- # The callback defined through {.on_config}.
+ # The callback defined through {.with_nokogiri_config}.
#
# @return [Proc] the proc to pass to Nokogiri to setup parse options. nil if empty.
#
- def config_callback
- @config_callback
+ def nokogiri_config_callback
+ @nokogiri_config_callback
end
# Register a config callback according to the block Nokogori expects when calling Nokogiri::XML::Document.parse().
# See http://nokogiri.org/Nokogiri/XML/Document.html#method-c-parse
#
# @param [Proc] the proc to pass to Nokogiri to setup parse options
#
- def on_config(&blk)
- @config_callback = blk
+ def with_nokogiri_config(&blk)
+ @nokogiri_config_callback = blk
end
#
@@ -274,8 +274,11 @@ def parse(xml, options = {})
# Attempt to parse the xml value with Nokogiri XML as a document
# and select the root element
-
- xml = Nokogiri::XML(xml, nil, nil, Nokogiri::XML::ParseOptions::STRICT, &config_callback)
+ xml = Nokogiri::XML(
+ xml, nil, nil,
+ Nokogiri::XML::ParseOptions::STRICT,
+ &nokogiri_config_callback
+ )
node = xml.root
end
View
@@ -120,7 +120,7 @@ class Product
end
class Rate
- include HappyMapper
+ include HappyMapper
end
module FamilySearch
@@ -313,7 +313,7 @@ class Country
class State
- include HappyMapper
+ include HappyMapper
end
class Address
@@ -453,9 +453,9 @@ class Item
class PublishOptions
include HappyMapper
-
+
tag 'publishOptions'
-
+
element :author, String, :tag => 'author'
element :draft, Boolean, :tag => 'draft'
@@ -465,87 +465,87 @@ class PublishOptions
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'
-
+
attr_writer :xml_value
-
+
element :title, String
element :text, String
has_many :photos, 'Photo', :tag => 'Photo', :namespace => 'photo', :xpath => '/article:Article'
has_many :galleries, 'Gallery', :tag => 'Gallery', :namespace => 'gallery'
-
+
element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'article'
-
+
end
class PartiallyBadArticle
include HappyMapper
-
+
attr_writer :xml_value
-
+
tag 'Article'
namespace 'article'
-
+
element :title, String
element :text, String
has_many :photos, 'Photo', :tag => 'Photo', :namespace => 'photo', :xpath => '/article:Article'
has_many :videos, 'Video', :tag => 'Video', :namespace => 'video'
-
+
element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'article'
-
+
end
class Photo
include HappyMapper
tag 'Photo'
namespace 'photo'
-
+
attr_writer :xml_value
-
+
element :title, String
element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'photo'
-
+
end
class Gallery
include HappyMapper
-
+
tag 'Gallery'
namespace 'gallery'
attr_writer :xml_value
element :title, String
-
+
end
class Video
include HappyMapper
-
+
tag 'Video'
namespace 'video'
attr_writer :xml_value
-
+
element :title, String
element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'video'
-
+
end
class OptionalAttribute
include HappyMapper
tag 'address'
-
+
attribute :street, String
-end
+end
class DefaultNamespaceCombi
include HappyMapper
@@ -673,23 +673,23 @@ module Bar; class Baz; include HappyMapper; end; end
end
end
- describe "#content" do
- it "should take String as default argument for type" do
+ describe "#content" do
+ it "should take String as default argument for type" do
State.content :name
address = Address.parse(fixture_file('address.xml'))
address.state.name.should == "Lower Saxony"
address.state.name.class == String
end
-
- it "should work when specific type is provided" do
+
+ it "should work when specific type is provided" do
Rate.content :value, Float
Product.has_one :rate, Rate
product = Product.parse(fixture_file('product_default_namespace.xml'), :single => true)
product.rate.value.should == 120.25
product.rate.class == Float
- end
+ end
end
-
+
it "should parse xml attributes into ruby objects" do
posts = Post.parse(fixture_file('posts.xml'))
posts.size.should == 20
@@ -946,26 +946,26 @@ class Thing
l = Location.parse(fixture_file('lastfm.xml'))
l.first.latitude.should == "51.53469"
end
-
+
describe "Parse optional attributes" do
-
+
it "should parse an empty String as empty" do
a = OptionalAttribute.parse(fixture_file('optional_attributes.xml'))
a[0].street.should == ""
end
-
+
it "should parse a String with value" do
a = OptionalAttribute.parse(fixture_file('optional_attributes.xml'))
a[1].street.should == "Milchstrasse"
end
-
+
it "should parse a String with value" do
a = OptionalAttribute.parse(fixture_file('optional_attributes.xml'))
a[2].street.should be_nil
end
-
+
end
-
+
describe "Default namespace combi" do
before(:each) do
file_contents = fixture_file('default_namespace_combi.xml')
@@ -1005,16 +1005,16 @@ class Thing
items = AmbigousItems::Item.parse(fixture_file('ambigous_items.xml'), :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
@@ -1026,13 +1026,13 @@ class Thing
it "should only find only items at the parent level" do
@article.photos.length.should == 1
end
-
+
before(:all) do
@article = Article.parse(fixture_file('subclass_namespace.xml'))
end
-
+
end
-
+
context "Namespace is missing because an optional element that uses it is not present" do
it "should parse successfully" do
@article = PartiallyBadArticle.parse(fixture_file('subclass_namespace.xml'))
@@ -1043,8 +1043,8 @@ class Thing
@article.photos.first.title.should_not be_nil
end
end
-
-
+
+
describe "with limit option" do
it "should return results with limited size: 6" do
sizes = []
@@ -1062,7 +1062,7 @@ class Thing
sizes.should == [10, 10]
end
end
-
+
context "when letting user set Nokogiri::XML::ParseOptions" do
let(:default) {
Class.new do
@@ -1074,32 +1074,32 @@ class Thing
Class.new do
include HappyMapper
element :item, String
- on_config do |config|
+ with_nokogiri_config do |config|
config.default_xml
end
- end
+ end
}
-
- it 'initializes @config_callback to nil' do
- default.config_callback.should be_nil
+
+ it 'initializes @nokogiri_config_callback to nil' do
+ default.nokogiri_config_callback.should be_nil
end
-
+
it 'defaults to Nokogiri::XML::ParseOptions::STRICT' do
expect { default.parse(fixture_file('set_config_options.xml')) }.to raise_error(Nokogiri::XML::SyntaxError)
end
-
+
it 'accepts .on_config callback' do
- custom.config_callback.should_not be_nil
+ custom.nokogiri_config_callback.should_not be_nil
end
-
- it 'parses according to @config_callback' do
+
+ it 'parses according to @nokogiri_config_callback' do
expect { custom.parse(fixture_file('set_config_options.xml')) }.to_not raise_error(Nokogiri::XML::SyntaxError)
end
-
- it 'can clear @config_callback' do
- custom.on_config {}
+
+ it 'can clear @nokogiri_config_callback' do
+ custom.with_nokogiri_config {}
expect { custom.parse(fixture_file('set_config_options.xml')) }.to raise_error(Nokogiri::XML::SyntaxError)
- end
+ end
end
-
+
end

0 comments on commit def71a1

Please sign in to comment.