Skip to content

Commit

Permalink
Merge branch 'jnunemaker/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
knaveofdiamonds committed May 28, 2009
2 parents 440fc5a + effc7d7 commit 6324310
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 42 deletions.
8 changes: 8 additions & 0 deletions History
@@ -1,3 +1,11 @@
== 0.2.5
* 1 minor tweak
* Classes can now be strings instead of constants so you don't have to worry about class definition order (this was all for technicalpickles, enjoy!)

== 0.2.4
* 1 minor tweak
* Added a patch that allows even crazy namespaces to work

== 0.2.3
* 1 minor tweak
* bumped the version of libxml-ruby to 1.1.3
Expand Down
6 changes: 3 additions & 3 deletions happymapper.gemspec
Expand Up @@ -2,15 +2,15 @@

Gem::Specification.new do |s|
s.name = %q{happymapper}
s.version = "0.2.4"
s.version = "0.2.5"

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["John Nunemaker"]
s.date = %q{2009-05-17}
s.date = %q{2009-05-27}
s.description = %q{object to xml mapping library}
s.email = %q{nunemaker@gmail.com}
s.extra_rdoc_files = ["lib/happymapper/attribute.rb", "lib/happymapper/element.rb", "lib/happymapper/item.rb", "lib/happymapper/version.rb", "lib/happymapper.rb", "README", "TODO"]
s.files = ["examples/amazon.rb", "examples/current_weather.rb", "examples/dashed_elements.rb", "examples/post.rb", "examples/twitter.rb", "happymapper.gemspec", "History", "lib/happymapper/attribute.rb", "lib/happymapper/element.rb", "lib/happymapper/item.rb", "lib/happymapper/version.rb", "lib/happymapper.rb", "License", "Manifest", "Rakefile", "README", "spec/fixtures/address.xml", "spec/fixtures/commit.xml", "spec/fixtures/current_weather.xml", "spec/fixtures/family_tree.xml", "spec/fixtures/multiple_namespaces.xml", "spec/fixtures/pita.xml", "spec/fixtures/posts.xml", "spec/fixtures/product_default_namespace.xml", "spec/fixtures/product_no_namespace.xml", "spec/fixtures/product_single_namespace.xml", "spec/fixtures/radar.xml", "spec/fixtures/statuses.xml", "spec/happymapper_attribute_spec.rb", "spec/happymapper_element_spec.rb", "spec/happymapper_item_spec.rb", "spec/happymapper_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "TODO", "website/css/common.css", "website/index.html"]
s.files = ["examples/amazon.rb", "examples/current_weather.rb", "examples/dashed_elements.rb", "examples/post.rb", "examples/twitter.rb", "happymapper.gemspec", "History", "lib/happymapper/attribute.rb", "lib/happymapper/element.rb", "lib/happymapper/item.rb", "lib/happymapper/version.rb", "lib/happymapper.rb", "License", "Manifest", "Rakefile", "README", "spec/fixtures/address.xml", "spec/fixtures/analytics.xml", "spec/fixtures/commit.xml", "spec/fixtures/current_weather.xml", "spec/fixtures/family_tree.xml", "spec/fixtures/multiple_namespaces.xml", "spec/fixtures/pita.xml", "spec/fixtures/posts.xml", "spec/fixtures/product_default_namespace.xml", "spec/fixtures/product_no_namespace.xml", "spec/fixtures/product_single_namespace.xml", "spec/fixtures/radar.xml", "spec/fixtures/statuses.xml", "spec/happymapper_attribute_spec.rb", "spec/happymapper_element_spec.rb", "spec/happymapper_item_spec.rb", "spec/happymapper_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "TODO", "website/css/common.css", "website/index.html"]
s.has_rdoc = true
s.homepage = %q{http://happymapper.rubyforge.org}
s.post_install_message = %q{May you have many happy mappings!}
Expand Down
44 changes: 33 additions & 11 deletions lib/happymapper/item.rb
Expand Up @@ -21,6 +21,10 @@ def initialize(name, type, o={})

@xml_type = self.class.to_s.split('::').last.downcase
end

def constant
@constant ||= constantize(type)
end

def from_xml_node(node, namespace, xpath_options)
if primitive?
Expand All @@ -41,13 +45,13 @@ def from_xml_node(node, namespace, xpath_options)
end

begin
type.send(options[:parser].to_sym, value)
constant.send(options[:parser].to_sym, value)
rescue
nil
end
end
else
type.parse(node, options.merge(:namespaces => xpath_options))
constant.parse(node, options.merge(:namespaces => xpath_options))
end
end
end
Expand All @@ -62,7 +66,7 @@ def xpath(namespace = self.namespace)
end

def primitive?
Types.include?(type)
Types.include?(constant)
end

def element?
Expand All @@ -78,15 +82,15 @@ def method_name
end

def typecast(value)
return value if value.kind_of?(type) || value.nil?
return value if value.kind_of?(constant) || value.nil?
begin
if type == String then value.to_s
elsif type == Float then value.to_f
elsif type == Time then Time.parse(value.to_s)
elsif type == Date then Date.parse(value.to_s)
elsif type == DateTime then DateTime.parse(value.to_s)
elsif type == Boolean then ['true', 't', '1'].include?(value.to_s.downcase)
elsif type == Integer
if constant == String then value.to_s
elsif constant == Float then value.to_f
elsif constant == Time then Time.parse(value.to_s)
elsif constant == Date then Date.parse(value.to_s)
elsif constant == DateTime then DateTime.parse(value.to_s)
elsif constant == Boolean then ['true', 't', '1'].include?(value.to_s.downcase)
elsif constant == Integer
# ganked from datamapper
value_to_i = value.to_i
if value_to_i == 0 && value != '0'
Expand All @@ -108,8 +112,26 @@ def typecast(value)
end

private

def constantize(type)
if type.is_a?(String)
names = type.split('::')
constant = Object
names.each do |name|
constant = constant.const_defined?(name) ?
constant.const_get(name) :
constant.const_missing(name)
end
constant
else
type
end
end


def find(node, namespace, xpath_options, &block)
if self.namespace && xpath_options["xmlns:#{self.namespace}"]
# from the class definition
namespace = self.namespace
elsif options[:namespace] && xpath_options["xmlns:#{options[:namespace]}"]
namespace = options[:namespace]
Expand Down
2 changes: 1 addition & 1 deletion lib/happymapper/version.rb
@@ -1,3 +1,3 @@
module HappyMapper
Version = '0.2.4'
Version = '0.2.5'
end
21 changes: 21 additions & 0 deletions spec/happymapper_item_spec.rb
@@ -1,5 +1,9 @@
require File.dirname(__FILE__) + '/spec_helper.rb'

module Foo
class Bar; end
end

describe HappyMapper::Item do

describe "new instance" do
Expand All @@ -24,6 +28,23 @@
end
end

describe "#constant" do
it "should just use type if constant" do
item = HappyMapper::Item.new(:foo, String)
item.constant.should == String
end

it "should convert string type to constant" do
item = HappyMapper::Item.new(:foo, 'String')
item.constant.should == String
end

it "should convert string with :: to constant" do
item = HappyMapper::Item.new(:foo, 'Foo::Bar')
item.constant.should == Foo::Bar
end
end

describe "#method_name" do
it "should convert dashes to underscores" do
item = HappyMapper::Item.new(:'foo-bar', String, :tag => 'foobar')
Expand Down
71 changes: 44 additions & 27 deletions spec/happymapper_spec.rb
Expand Up @@ -283,72 +283,76 @@ class Artist

describe "being included into another class" do
before do
Foo.instance_variable_set("@attributes", {})
Foo.instance_variable_set("@elements", {})
@klass = Class.new do
include HappyMapper

def self.to_s
'Foo'
end
end
end
class Foo; include HappyMapper end

it "should set attributes to an array" do
Foo.attributes.should == []
@klass.attributes.should == []
end

it "should set @elements to a hash" do
Foo.elements.should == []
@klass.elements.should == []
end

it "should allow adding an attribute" do
lambda {
Foo.attribute :name, String
}.should change(Foo, :attributes)
@klass.attribute :name, String
}.should change(@klass, :attributes)
end

it "should allow adding an attribute containing a dash" do
lambda {
Foo.attribute :'bar-baz', String
}.should change(Foo, :attributes)
@klass.attribute :'bar-baz', String
}.should change(@klass, :attributes)
end

it "should be able to get all attributes in array" do
Foo.attribute :name, String
Foo.attributes.size.should == 1
@klass.attribute :name, String
@klass.attributes.size.should == 1
end

it "should allow adding an element" do
lambda {
Foo.element :name, String
}.should change(Foo, :elements)
@klass.element :name, String
}.should change(@klass, :elements)
end

it "should allow adding an element containing a dash" do
lambda {
Foo.element :'bar-baz', String
}.should change(Foo, :elements)
@klass.element :'bar-baz', String
}.should change(@klass, :elements)

end

it "should be able to get all elements in array" do
Foo.element(:name, String)
Foo.elements.size.should == 1
@klass.element(:name, String)
@klass.elements.size.should == 1
end

it "should allow has one association" do
Foo.has_one(:user, User)
element = Foo.elements.first
@klass.has_one(:user, User)
element = @klass.elements.first
element.name.should == 'user'
element.type.should == User
element.options[:single] = true
end

it "should allow has many association" do
Foo.has_many(:users, User)
element = Foo.elements.first
@klass.has_many(:users, User)
element = @klass.elements.first
element.name.should == 'users'
element.type.should == User
element.options[:single] = false
end

it "should default tag name to lowercase class" do
Foo.tag_name.should == 'foo'
@klass.tag_name.should == 'foo'
end

it "should default tag name of class in modules to the last constant lowercase" do
Expand All @@ -357,17 +361,17 @@ module Bar; class Baz; include HappyMapper; end; end
end

it "should allow setting tag name" do
Foo.tag('FooBar')
Foo.tag_name.should == 'FooBar'
@klass.tag('FooBar')
@klass.tag_name.should == 'FooBar'
end

it "should allow setting a namespace" do
Foo.namespace(namespace = "foo")
Foo.namespace.should == namespace
@klass.namespace(namespace = "foo")
@klass.namespace.should == namespace
end

it "should provide #parse" do
Foo.should respond_to(:parse)
@klass.should respond_to(:parse)
end
end

Expand Down Expand Up @@ -560,6 +564,19 @@ module Bar; class Baz; include HappyMapper; end; end
property.value.should == '85301'
end

it "should allow instantiating with a string" do
module StringFoo
class Bar
include HappyMapper
has_many :things, 'StringFoo::Thing'
end

class Thing
include HappyMapper
end
end
end

xit "should parse family search xml" do
tree = FamilySearch::FamilyTree.parse(fixture_file('family_tree.xml'))
tree.version.should == '1.0.20071213.942'
Expand Down

0 comments on commit 6324310

Please sign in to comment.