nokogiri-happymapper 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/happymapper.rb CHANGED
@@ -121,10 +121,10 @@ module HappyMapper
121
121
  #
122
122
  # @param [Symbol] name the name of the accessor that is created
123
123
  # @param [String,Class] type the class name of the name of the class whcih
124
- # the object will be converted upon parsing
124
+ # the object will be converted upon parsing. By Default String class will be taken.
125
125
  # @param [Hash] options additional parameters to send to the relationship
126
126
  #
127
- def content(name, type, options={})
127
+ def content(name, type=String, options={})
128
128
  @content = TextNode.new(name, type, options)
129
129
  attr_accessor @content.method_name.intern
130
130
  end
@@ -309,8 +309,12 @@ module HappyMapper
309
309
 
310
310
  nodes.each_slice(limit) do |slice|
311
311
 
312
- part = slice.map do |n|
313
- obj = new
312
+ part = slice.map do |n|
313
+
314
+ # If an existing HappyMapper object is provided, update it with the
315
+ # values from the xml being parsed. Otherwise, create a new object
316
+
317
+ obj = options[:update] ? options[:update] : new
314
318
 
315
319
  attributes.each do |attr|
316
320
  obj.send("#{attr.method_name}=",attr.from_xml_node(n, namespace, namespaces))
@@ -612,7 +616,15 @@ module HappyMapper
612
616
  write_out_to_xml ? builder.to_xml : builder
613
617
 
614
618
  end
615
-
619
+
620
+ # Parse the xml and update this instance. This does not update instances
621
+ # of HappyMappers that are children of this object. New instances will be
622
+ # created for any HappyMapper children of this object.
623
+ #
624
+ # Params and return are the same as the class parse() method above.
625
+ def parse(xml, options = {})
626
+ self.class.parse(xml, options.merge!(:update => self))
627
+ end
616
628
 
617
629
  end
618
630
 
@@ -5,4 +5,5 @@
5
5
  <postcode>26131</postcode>
6
6
  <city>Oldenburg</city>
7
7
  <country code="de">Germany</country>
8
+ <state>Lower Saxony</state>
8
9
  </address>
@@ -13,5 +13,6 @@
13
13
  <city>Oldenburg</city>
14
14
  <country code="de">Germany</country>
15
15
  </address>
16
+ <rate>120.25</rate>
16
17
  </product>
17
18
  </products>
@@ -119,6 +119,10 @@ class Product
119
119
  has_one :address, Address
120
120
  end
121
121
 
122
+ class Rate
123
+ include HappyMapper
124
+ end
125
+
122
126
  module FamilySearch
123
127
  class AlternateIds
124
128
  include HappyMapper
@@ -307,6 +311,11 @@ class Country
307
311
  content :name, String
308
312
  end
309
313
 
314
+
315
+ class State
316
+ include HappyMapper
317
+ end
318
+
310
319
  class Address
311
320
  include HappyMapper
312
321
 
@@ -316,6 +325,7 @@ class Address
316
325
  element :housenumber, String
317
326
  element :city, String
318
327
  has_one :country, Country
328
+ has_one :state, State
319
329
  end
320
330
 
321
331
  # for type coercion
@@ -663,6 +673,23 @@ describe HappyMapper do
663
673
  end
664
674
  end
665
675
 
676
+ describe "#content" do
677
+ it "should take String as default argument for type" do
678
+ State.content :name
679
+ address = Address.parse(fixture_file('address.xml'))
680
+ address.state.name.should == "Lower Saxony"
681
+ address.state.name.class == String
682
+ end
683
+
684
+ it "should work when specific type is provided" do
685
+ Rate.content :value, Float
686
+ Product.has_one :rate, Rate
687
+ product = Product.parse(fixture_file('product_default_namespace.xml'), :single => true)
688
+ product.rate.value.should == 120.25
689
+ product.rate.class == Float
690
+ end
691
+ end
692
+
666
693
  it "should parse xml attributes into ruby objects" do
667
694
  posts = Post.parse(fixture_file('posts.xml'))
668
695
  posts.size.should == 20
@@ -0,0 +1,129 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ parse_instance_initial_xml = %{
4
+ <root attr1="initial">
5
+ <item attr1="initial">
6
+ <description>initial</description>
7
+ <subitem attr1="initial">
8
+ <name>initial</name>
9
+ </subitem>
10
+ <subitem attr1="initial">
11
+ <name>initial</name>
12
+ </subitem>
13
+ </item>
14
+ <item attr1="initial">
15
+ <description>initial</description>
16
+ <subitem attr1="initial">
17
+ <name>initial</name>
18
+ </subitem>
19
+ <subitem attr1="initial">
20
+ <name>initial</name>
21
+ </subitem>
22
+ </item>
23
+ </root>
24
+ }
25
+
26
+ parse_instance_updated_xml = %{
27
+ <root attr1="updated">
28
+ <item attr1="updated">
29
+ <description>updated</description>
30
+ <subitem attr1="updated">
31
+ <name>updated</name>
32
+ </subitem>
33
+ <subitem attr1="updated">
34
+ <name>updated</name>
35
+ </subitem>
36
+ </item>
37
+ <item attr1="updated">
38
+ <description>updated</description>
39
+ <subitem attr1="updated">
40
+ <name>updated</name>
41
+ </subitem>
42
+ <subitem attr1="updated">
43
+ <name>updated</name>
44
+ </subitem>
45
+ </item>
46
+ </root>
47
+ }
48
+
49
+ module ParseInstanceSpec
50
+ class SubItem
51
+ include HappyMapper
52
+ tag 'subitem'
53
+ attribute :attr1, String
54
+ element :name, String
55
+ end
56
+ class Item
57
+ include HappyMapper
58
+ tag 'item'
59
+ attribute :attr1, String
60
+ element :description, String
61
+ has_many :sub_items, SubItem
62
+ end
63
+ class Root
64
+ include HappyMapper
65
+ tag 'root'
66
+ attribute :attr1, String
67
+ has_many :items, Item
68
+ end
69
+ end
70
+
71
+ describe HappyMapper do
72
+ describe "update existing instance by parsing new xml" do
73
+
74
+ it 'should have initial values' do
75
+ @initial.attr1.should == 'initial'
76
+ @initial.items[0].attr1.should == 'initial'
77
+ @initial.items[0].description.should == 'initial'
78
+ @initial.items[0].sub_items[0].attr1.should == 'initial'
79
+ @initial.items[0].sub_items[0].name.should == 'initial'
80
+ @initial.items[0].sub_items[1].attr1.should == 'initial'
81
+ @initial.items[0].sub_items[1].name.should == 'initial'
82
+ @initial.items[1].attr1.should == 'initial'
83
+ @initial.items[1].description.should == 'initial'
84
+ @initial.items[1].sub_items[0].attr1.should == 'initial'
85
+ @initial.items[1].sub_items[0].name.should == 'initial'
86
+ @initial.items[1].sub_items[1].attr1.should == 'initial'
87
+ @initial.items[1].sub_items[1].name.should == 'initial'
88
+ end
89
+
90
+ it 'should have updated values' do
91
+ ParseInstanceSpec::Root.parse(parse_instance_updated_xml, :update => @initial)
92
+ @initial.attr1.should == 'updated'
93
+ @initial.items[0].attr1.should == 'updated'
94
+ @initial.items[0].description.should == 'updated'
95
+ @initial.items[0].sub_items[0].attr1.should == 'updated'
96
+ @initial.items[0].sub_items[0].name.should == 'updated'
97
+ @initial.items[0].sub_items[1].attr1.should == 'updated'
98
+ @initial.items[0].sub_items[1].name.should == 'updated'
99
+ @initial.items[1].attr1.should == 'updated'
100
+ @initial.items[1].description.should == 'updated'
101
+ @initial.items[1].sub_items[0].attr1.should == 'updated'
102
+ @initial.items[1].sub_items[0].name.should == 'updated'
103
+ @initial.items[1].sub_items[1].attr1.should == 'updated'
104
+ @initial.items[1].sub_items[1].name.should == 'updated'
105
+ end
106
+
107
+ it "should be able to update instance from 'parse()' instance method" do
108
+ @initial.parse(parse_instance_updated_xml)
109
+ @initial.attr1.should == 'updated'
110
+ @initial.items[0].attr1.should == 'updated'
111
+ @initial.items[0].description.should == 'updated'
112
+ @initial.items[0].sub_items[0].attr1.should == 'updated'
113
+ @initial.items[0].sub_items[0].name.should == 'updated'
114
+ @initial.items[0].sub_items[1].attr1.should == 'updated'
115
+ @initial.items[0].sub_items[1].name.should == 'updated'
116
+ @initial.items[1].attr1.should == 'updated'
117
+ @initial.items[1].description.should == 'updated'
118
+ @initial.items[1].sub_items[0].attr1.should == 'updated'
119
+ @initial.items[1].sub_items[0].name.should == 'updated'
120
+ @initial.items[1].sub_items[1].attr1.should == 'updated'
121
+ @initial.items[1].sub_items[1].name.should == 'updated'
122
+ end
123
+
124
+ before(:each) do
125
+ @initial = ParseInstanceSpec::Root.parse(parse_instance_initial_xml)
126
+ end
127
+ end
128
+ end
129
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri-happymapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2011-08-22 00:00:00.000000000 Z
17
+ date: 2012-09-23 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: nokogiri
@@ -50,7 +50,7 @@ dependencies:
50
50
  version: '2.8'
51
51
  description: Object to XML Mapping Library, using Nokogiri (fork from John Nunemaker's
52
52
  Happymapper)
53
- email: franklin.webber@gmail.com
53
+ email:
54
54
  executables: []
55
55
  extensions: []
56
56
  extra_rdoc_files:
@@ -96,9 +96,10 @@ files:
96
96
  - spec/happymapper_to_xml_namespaces_spec.rb
97
97
  - spec/happymapper_to_xml_spec.rb
98
98
  - spec/ignay_spec.rb
99
+ - spec/parse_instance_spec.rb
99
100
  - spec/spec_helper.rb
100
101
  - spec/xpath_spec.rb
101
- homepage: http://github.com/burtlo/happymapper
102
+ homepage: http://github.com/dam5s/happymapper
102
103
  licenses: []
103
104
  post_install_message:
104
105
  rdoc_options: []
@@ -155,5 +156,6 @@ test_files:
155
156
  - spec/happymapper_to_xml_namespaces_spec.rb
156
157
  - spec/happymapper_to_xml_spec.rb
157
158
  - spec/ignay_spec.rb
159
+ - spec/parse_instance_spec.rb
158
160
  - spec/spec_helper.rb
159
161
  - spec/xpath_spec.rb