happymapper 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History +8 -0
- data/happymapper.gemspec +2 -2
- data/lib/happymapper/item.rb +30 -11
- data/lib/happymapper/version.rb +1 -1
- data/spec/happymapper_item_spec.rb +21 -0
- data/spec/happymapper_spec.rb +44 -27
- metadata +2 -2
data/History
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
== 0.2.5
|
2
|
+
* 1 minor tweak
|
3
|
+
* 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!)
|
4
|
+
|
5
|
+
== 0.2.4
|
6
|
+
* 1 minor tweak
|
7
|
+
* Added a patch that allows even crazy namespaces to work
|
8
|
+
|
1
9
|
== 0.2.3
|
2
10
|
* 1 minor tweak
|
3
11
|
* bumped the version of libxml-ruby to 1.1.3
|
data/happymapper.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{happymapper}
|
5
|
-
s.version = "0.2.
|
5
|
+
s.version = "0.2.5"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["John Nunemaker"]
|
9
|
-
s.date = %q{2009-05-
|
9
|
+
s.date = %q{2009-05-27}
|
10
10
|
s.description = %q{object to xml mapping library}
|
11
11
|
s.email = %q{nunemaker@gmail.com}
|
12
12
|
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"]
|
data/lib/happymapper/item.rb
CHANGED
@@ -21,6 +21,10 @@ module HappyMapper
|
|
21
21
|
|
22
22
|
@xml_type = self.class.to_s.split('::').last.downcase
|
23
23
|
end
|
24
|
+
|
25
|
+
def constant
|
26
|
+
@constant ||= constantize(type)
|
27
|
+
end
|
24
28
|
|
25
29
|
def from_xml_node(node, namespace)
|
26
30
|
if primitive?
|
@@ -41,13 +45,13 @@ module HappyMapper
|
|
41
45
|
end
|
42
46
|
|
43
47
|
begin
|
44
|
-
|
48
|
+
constant.send(options[:parser].to_sym, value)
|
45
49
|
rescue
|
46
50
|
nil
|
47
51
|
end
|
48
52
|
end
|
49
53
|
else
|
50
|
-
|
54
|
+
constant.parse(node, options)
|
51
55
|
end
|
52
56
|
end
|
53
57
|
end
|
@@ -62,7 +66,7 @@ module HappyMapper
|
|
62
66
|
end
|
63
67
|
|
64
68
|
def primitive?
|
65
|
-
Types.include?(
|
69
|
+
Types.include?(constant)
|
66
70
|
end
|
67
71
|
|
68
72
|
def element?
|
@@ -78,15 +82,15 @@ module HappyMapper
|
|
78
82
|
end
|
79
83
|
|
80
84
|
def typecast(value)
|
81
|
-
return value if value.kind_of?(
|
85
|
+
return value if value.kind_of?(constant) || value.nil?
|
82
86
|
begin
|
83
|
-
if
|
84
|
-
elsif
|
85
|
-
elsif
|
86
|
-
elsif
|
87
|
-
elsif
|
88
|
-
elsif
|
89
|
-
elsif
|
87
|
+
if constant == String then value.to_s
|
88
|
+
elsif constant == Float then value.to_f
|
89
|
+
elsif constant == Time then Time.parse(value.to_s)
|
90
|
+
elsif constant == Date then Date.parse(value.to_s)
|
91
|
+
elsif constant == DateTime then DateTime.parse(value.to_s)
|
92
|
+
elsif constant == Boolean then ['true', 't', '1'].include?(value.to_s.downcase)
|
93
|
+
elsif constant == Integer
|
90
94
|
# ganked from datamapper
|
91
95
|
value_to_i = value.to_i
|
92
96
|
if value_to_i == 0 && value != '0'
|
@@ -108,6 +112,21 @@ module HappyMapper
|
|
108
112
|
end
|
109
113
|
|
110
114
|
private
|
115
|
+
def constantize(type)
|
116
|
+
if type.is_a?(String)
|
117
|
+
names = type.split('::')
|
118
|
+
constant = Object
|
119
|
+
names.each do |name|
|
120
|
+
constant = constant.const_defined?(name) ?
|
121
|
+
constant.const_get(name) :
|
122
|
+
constant.const_missing(name)
|
123
|
+
end
|
124
|
+
constant
|
125
|
+
else
|
126
|
+
type
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
111
130
|
def find(node, namespace, &block)
|
112
131
|
# this node has a custom namespace (that is present in the doc)
|
113
132
|
if self.namespace && node.namespaces.find_by_prefix(self.namespace)
|
data/lib/happymapper/version.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
2
|
|
3
|
+
module Foo
|
4
|
+
class Bar; end
|
5
|
+
end
|
6
|
+
|
3
7
|
describe HappyMapper::Item do
|
4
8
|
|
5
9
|
describe "new instance" do
|
@@ -24,6 +28,23 @@ describe HappyMapper::Item do
|
|
24
28
|
end
|
25
29
|
end
|
26
30
|
|
31
|
+
describe "#constant" do
|
32
|
+
it "should just use type if constant" do
|
33
|
+
item = HappyMapper::Item.new(:foo, String)
|
34
|
+
item.constant.should == String
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should convert string type to constant" do
|
38
|
+
item = HappyMapper::Item.new(:foo, 'String')
|
39
|
+
item.constant.should == String
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should convert string with :: to constant" do
|
43
|
+
item = HappyMapper::Item.new(:foo, 'Foo::Bar')
|
44
|
+
item.constant.should == Foo::Bar
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
27
48
|
describe "#method_name" do
|
28
49
|
it "should convert dashes to underscores" do
|
29
50
|
item = HappyMapper::Item.new(:'foo-bar', String, :tag => 'foobar')
|
data/spec/happymapper_spec.rb
CHANGED
@@ -274,72 +274,76 @@ describe HappyMapper do
|
|
274
274
|
|
275
275
|
describe "being included into another class" do
|
276
276
|
before do
|
277
|
-
|
278
|
-
|
277
|
+
@klass = Class.new do
|
278
|
+
include HappyMapper
|
279
|
+
|
280
|
+
def self.to_s
|
281
|
+
'Foo'
|
282
|
+
end
|
283
|
+
end
|
279
284
|
end
|
280
|
-
class Foo; include HappyMapper end
|
281
285
|
|
282
286
|
it "should set attributes to an array" do
|
283
|
-
|
287
|
+
@klass.attributes.should == []
|
284
288
|
end
|
285
289
|
|
286
290
|
it "should set @elements to a hash" do
|
287
|
-
|
291
|
+
@klass.elements.should == []
|
288
292
|
end
|
289
293
|
|
290
294
|
it "should allow adding an attribute" do
|
291
295
|
lambda {
|
292
|
-
|
293
|
-
}.should change(
|
296
|
+
@klass.attribute :name, String
|
297
|
+
}.should change(@klass, :attributes)
|
294
298
|
end
|
295
299
|
|
296
300
|
it "should allow adding an attribute containing a dash" do
|
297
301
|
lambda {
|
298
|
-
|
299
|
-
}.should change(
|
302
|
+
@klass.attribute :'bar-baz', String
|
303
|
+
}.should change(@klass, :attributes)
|
300
304
|
end
|
301
305
|
|
302
306
|
it "should be able to get all attributes in array" do
|
303
|
-
|
304
|
-
|
307
|
+
@klass.attribute :name, String
|
308
|
+
@klass.attributes.size.should == 1
|
305
309
|
end
|
306
310
|
|
307
311
|
it "should allow adding an element" do
|
308
312
|
lambda {
|
309
|
-
|
310
|
-
}.should change(
|
313
|
+
@klass.element :name, String
|
314
|
+
}.should change(@klass, :elements)
|
311
315
|
end
|
312
316
|
|
313
317
|
it "should allow adding an element containing a dash" do
|
314
318
|
lambda {
|
315
|
-
|
316
|
-
}.should change(
|
319
|
+
@klass.element :'bar-baz', String
|
320
|
+
}.should change(@klass, :elements)
|
317
321
|
|
318
322
|
end
|
319
323
|
|
320
324
|
it "should be able to get all elements in array" do
|
321
|
-
|
322
|
-
|
325
|
+
@klass.element(:name, String)
|
326
|
+
@klass.elements.size.should == 1
|
323
327
|
end
|
324
328
|
|
325
329
|
it "should allow has one association" do
|
326
|
-
|
327
|
-
element =
|
330
|
+
@klass.has_one(:user, User)
|
331
|
+
element = @klass.elements.first
|
328
332
|
element.name.should == 'user'
|
329
333
|
element.type.should == User
|
330
334
|
element.options[:single] = true
|
331
335
|
end
|
332
336
|
|
333
337
|
it "should allow has many association" do
|
334
|
-
|
335
|
-
element =
|
338
|
+
@klass.has_many(:users, User)
|
339
|
+
element = @klass.elements.first
|
336
340
|
element.name.should == 'users'
|
337
341
|
element.type.should == User
|
338
342
|
element.options[:single] = false
|
339
343
|
end
|
340
344
|
|
341
345
|
it "should default tag name to lowercase class" do
|
342
|
-
|
346
|
+
@klass.tag_name.should == 'foo'
|
343
347
|
end
|
344
348
|
|
345
349
|
it "should default tag name of class in modules to the last constant lowercase" do
|
@@ -348,17 +352,17 @@ describe HappyMapper do
|
|
348
352
|
end
|
349
353
|
|
350
354
|
it "should allow setting tag name" do
|
351
|
-
|
352
|
-
|
355
|
+
@klass.tag('FooBar')
|
356
|
+
@klass.tag_name.should == 'FooBar'
|
353
357
|
end
|
354
358
|
|
355
359
|
it "should allow setting a namespace" do
|
356
|
-
|
357
|
-
|
360
|
+
@klass.namespace(namespace = "foo")
|
361
|
+
@klass.namespace.should == namespace
|
358
362
|
end
|
359
363
|
|
360
364
|
it "should provide #parse" do
|
361
|
-
|
365
|
+
@klass.should respond_to(:parse)
|
362
366
|
end
|
363
367
|
end
|
364
368
|
|
@@ -551,6 +555,19 @@ describe HappyMapper do
|
|
551
555
|
property.value.should == '85301'
|
552
556
|
end
|
553
557
|
|
558
|
+
it "should allow instantiating with a string" do
|
559
|
+
module StringFoo
|
560
|
+
class Bar
|
561
|
+
include HappyMapper
|
562
|
+
has_many :things, 'StringFoo::Thing'
|
563
|
+
end
|
564
|
+
|
565
|
+
class Thing
|
566
|
+
include HappyMapper
|
567
|
+
end
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
554
571
|
xit "should parse family search xml" do
|
555
572
|
tree = FamilySearch::FamilyTree.parse(fixture_file('family_tree.xml'))
|
556
573
|
tree.version.should == '1.0.20071213.942'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: happymapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-27 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|