xml-mapping 0.9.1 → 0.10.0

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +10 -53
  3. data/README.md +57 -0
  4. data/Rakefile +75 -85
  5. data/TODO.txt +12 -2
  6. data/examples/README +4 -4
  7. data/examples/cleanup.rb +11 -0
  8. data/examples/company_usage.intout +7 -7
  9. data/examples/documents_folders_usage.intout +2 -2
  10. data/examples/order_signature_enhanced_usage.intout +3 -3
  11. data/examples/order_usage.intout +26 -26
  12. data/examples/person.intout +3 -3
  13. data/examples/person_mm.intout +2 -2
  14. data/examples/publication.intout +2 -2
  15. data/examples/reader.intout +1 -1
  16. data/examples/stringarray_usage.intout +1 -1
  17. data/examples/time_augm.intout +6 -6
  18. data/examples/xpath_create_new.intout +13 -13
  19. data/examples/xpath_ensure_created.intout +2 -2
  20. data/examples/xpath_usage.intout +1 -1
  21. data/lib/xml/mapping.rb +0 -2
  22. data/lib/xml/mapping/base.rb +24 -9
  23. data/lib/xml/mapping/version.rb +1 -1
  24. data/test/company.rb +43 -0
  25. data/test/documents_folders.rb +7 -0
  26. data/test/multiple_mappings_test.rb +3 -1
  27. data/test/xml_mapping_adv_test.rb +20 -20
  28. data/test/xml_mapping_test.rb +31 -2
  29. data/{README → user_manual.md} +916 -154
  30. data/user_manual_xxpath.md +677 -0
  31. metadata +100 -112
  32. data/ChangeLog +0 -189
  33. data/README_XPATH +0 -202
  34. data/examples/company_usage.intin.rb +0 -19
  35. data/examples/documents_folders_usage.intin.rb +0 -18
  36. data/examples/order_signature_enhanced_usage.intin.rb +0 -12
  37. data/examples/order_usage.intin.rb +0 -120
  38. data/examples/person.intin.rb +0 -44
  39. data/examples/person_mm.intin.rb +0 -119
  40. data/examples/publication.intin.rb +0 -44
  41. data/examples/reader.intin.rb +0 -33
  42. data/examples/stringarray_usage.intin.rb +0 -11
  43. data/examples/time_augm.intin.rb +0 -19
  44. data/examples/time_augm_loading.intin.rb +0 -44
  45. data/examples/time_node.intin.rb +0 -79
  46. data/examples/time_node_w_marshallers.intin.rb +0 -48
  47. data/examples/xpath_create_new.intin.rb +0 -85
  48. data/examples/xpath_docvsroot.intin.rb +0 -30
  49. data/examples/xpath_ensure_created.intin.rb +0 -62
  50. data/examples/xpath_pathological.intin.rb +0 -42
  51. data/examples/xpath_usage.intin.rb +0 -51
  52. data/install.rb +0 -41
  53. data/test/xxpath_benchmark.result1.txt +0 -17
@@ -1,10 +1,10 @@
1
1
 
2
2
  root = XML::Mapping.load_object_from_file "documents_folders.xml"
3
- => #<Folder:0x7f48766041e8 @name="home", @entries=[#<Document:0x7f4876600f70 @name="plan", @contents=" inhale, exhale">, #<Folder:0x7f48765febf8 @name="work", @entries=[#<Folder:0x7f48765fc1a0 @name="xml-mapping", @entries=[#<Document:0x7f48765fa6c0 @name="README", @contents="foo bar baz">]>]>]>
3
+ => #<Folder:0x007fb015437098 @entries=[#<Document:0x007fb015436148 @name="plan", @contents=" inhale, exhale">, #<Folder:0x007fb0154352c0 @entries=[#<Folder:0x007fb015434550 @entries=[#<Document:0x007fb015432e30 @name="README", @contents="foo bar baz">], @name="xml-mapping">], @name="work">], @name="home">
4
4
  root.name
5
5
  => "home"
6
6
  root.entries
7
- => [#<Document:0x7f4876600f70 @name="plan", @contents=" inhale, exhale">, #<Folder:0x7f48765febf8 @name="work", @entries=[#<Folder:0x7f48765fc1a0 @name="xml-mapping", @entries=[#<Document:0x7f48765fa6c0 @name="README", @contents="foo bar baz">]>]>]
7
+ => [#<Document:0x007fb015436148 @name="plan", @contents=" inhale, exhale">, #<Folder:0x007fb0154352c0 @entries=[#<Folder:0x007fb015434550 @entries=[#<Document:0x007fb015432e30 @name="README", @contents="foo bar baz">], @name="xml-mapping">], @name="work">]
8
8
 
9
9
  root.append "etc", Folder.new
10
10
  root["etc"].append "passwd", Document.new
@@ -1,9 +1,9 @@
1
1
  s=Signature.load_from_file("order_signature_enhanced.xml")
2
- => #<Signature:0x7f487642e850 @signed_on=Sun Feb 13 00:00:00 +0100 2005, @name="John Doe", @position="product manager">
2
+ => #<Signature:0x007fb015310840 @position="product manager", @signed_on=2005-02-13 00:00:00 +0100, @name="John Doe">
3
3
  s.signed_on
4
- => Sun Feb 13 00:00:00 +0100 2005
4
+ => 2005-02-13 00:00:00 +0100
5
5
  s.signed_on=Time.local(1976,12,18)
6
- => Sat Dec 18 00:00:00 +0100 1976
6
+ => 1976-12-18 00:00:00 +0100
7
7
  s.save_to_xml.write($stdout,2)
8
8
  <signature>
9
9
  <Name>
@@ -1,18 +1,18 @@
1
1
  ####read access
2
2
  o=Order.load_from_file("order.xml")
3
- => #<Order:0x7f4877650510 @reference="12343-AHSHE-314159", @signatures=[#<Signature:0x7f48776282b8 @name="John Doe", @position="product manager">, #<Signature:0x7f4877626d28 @name="Jill Smith", @position="clerk">, #<Signature:0x7f4877625798 @name="Miles O'Brien", @position="Some Employee">], @items={"RF-3341"=>#<Item:0x7f487762d290 @quantity=30, @descr="Cookie", @unit_price=0.85>, "RF-0034"=>#<Item:0x7f4877632808 @quantity=5, @descr="Chocolate", @unit_price=28.5>, "RF-0001"=>#<Item:0x7f487763a558 @quantity=10, @descr="Stuffed Penguin", @unit_price=8.95>}, @client=#<Client:0x7f487764d9c8 @name="Jean Smith", @work_address=#<Address:0x7f4877641588 @zip=94102, @street="98765, Fulton Street", @city="San Francisco", @state="CA">, @home_address=#<Address:0x7f4877645d40 @zip=94403, @street="2000, Alameda de las Pulgas", @city="San Mateo", @state="CA">>>
3
+ => #<Order:0x007fb01549d438 @signatures=[#<Signature:0x007fb015493b68 @position="product manager", @name="John Doe">, #<Signature:0x007fb015492c68 @position="clerk", @name="Jill Smith">, #<Signature:0x007fb015491a20 @position="Some Employee", @name="Miles O'Brien">], @reference="12343-AHSHE-314159", @client=#<Client:0x007fb01549c998 @work_address=#<Address:0x007fb01549a238 @city="San Francisco", @state="CA", @zip=94102, @street="98765, Fulton Street">, @name="Jean Smith", @home_address=#<Address:0x007fb01549bbb0 @city="San Mateo", @state="CA", @zip=94403, @street="2000, Alameda de las Pulgas">>, @items={"RF-0001"=>#<Item:0x007fb0154989b0 @descr="Stuffed Penguin", @quantity=10, @unit_price=8.95>, "RF-0034"=>#<Item:0x007fb015496638 @descr="Chocolate", @quantity=5, @unit_price=28.5>, "RF-3341"=>#<Item:0x007fb015495300 @descr="Cookie", @quantity=30, @unit_price=0.85>}>
4
4
  o.reference
5
5
  => "12343-AHSHE-314159"
6
6
  o.client
7
- => #<Client:0x7f487764d9c8 @name="Jean Smith", @work_address=#<Address:0x7f4877641588 @zip=94102, @street="98765, Fulton Street", @city="San Francisco", @state="CA">, @home_address=#<Address:0x7f4877645d40 @zip=94403, @street="2000, Alameda de las Pulgas", @city="San Mateo", @state="CA">>
7
+ => #<Client:0x007fb01549c998 @work_address=#<Address:0x007fb01549a238 @city="San Francisco", @state="CA", @zip=94102, @street="98765, Fulton Street">, @name="Jean Smith", @home_address=#<Address:0x007fb01549bbb0 @city="San Mateo", @state="CA", @zip=94403, @street="2000, Alameda de las Pulgas">>
8
8
  o.items.keys
9
- => ["RF-3341", "RF-0034", "RF-0001"]
9
+ => ["RF-0001", "RF-0034", "RF-3341"]
10
10
  o.items["RF-0034"].descr
11
11
  => "Chocolate"
12
12
  o.items["RF-0034"].total_price
13
13
  => 142.5
14
14
  o.signatures
15
- => [#<Signature:0x7f48776282b8 @name="John Doe", @position="product manager">, #<Signature:0x7f4877626d28 @name="Jill Smith", @position="clerk">, #<Signature:0x7f4877625798 @name="Miles O'Brien", @position="Some Employee">]
15
+ => [#<Signature:0x007fb015493b68 @position="product manager", @name="John Doe">, #<Signature:0x007fb015492c68 @position="clerk", @name="Jill Smith">, #<Signature:0x007fb015491a20 @position="Some Employee", @name="Miles O'Brien">]
16
16
  o.signatures[2].name
17
17
  => "Miles O'Brien"
18
18
  o.signatures[2].position
@@ -70,48 +70,48 @@ xml.write($stdout,2)
70
70
  </Street>
71
71
  </Address>
72
72
  </Client>
73
- <Item reference='RF-3341'>
73
+ <Item reference='RF-0001'>
74
74
  <Description>
75
- Cookie
75
+ Stuffed Penguin
76
76
  </Description>
77
77
  <Quantity>
78
- 30
78
+ 10
79
79
  </Quantity>
80
80
  <UnitPrice>
81
- 0.85
81
+ 8.95
82
82
  </UnitPrice>
83
83
  </Item>
84
- <Item reference='RF-4711'>
84
+ <Item reference='RF-0034'>
85
85
  <Description>
86
- power transfer grid
86
+ Chocolate
87
87
  </Description>
88
88
  <Quantity>
89
- 2
89
+ 5
90
90
  </Quantity>
91
91
  <UnitPrice>
92
- 29.95
92
+ 28.5
93
93
  </UnitPrice>
94
94
  </Item>
95
- <Item reference='RF-0034'>
95
+ <Item reference='RF-3341'>
96
96
  <Description>
97
- Chocolate
97
+ Cookie
98
98
  </Description>
99
99
  <Quantity>
100
- 5
100
+ 30
101
101
  </Quantity>
102
102
  <UnitPrice>
103
- 28.5
103
+ 0.85
104
104
  </UnitPrice>
105
105
  </Item>
106
- <Item reference='RF-0001'>
106
+ <Item reference='RF-4711'>
107
107
  <Description>
108
- Stuffed Penguin
108
+ power transfer grid
109
109
  </Description>
110
110
  <Quantity>
111
- 10
111
+ 2
112
112
  </Quantity>
113
113
  <UnitPrice>
114
- 8.95
114
+ 29.95
115
115
  </UnitPrice>
116
116
  </Item>
117
117
  <Signed-By>
@@ -150,17 +150,17 @@ xml.write($stdout,2)
150
150
 
151
151
  ####Starting a new order from scratch
152
152
  o = Order.new
153
- => #<Order:0x7f48766fc118 @signatures=[]>
153
+ => #<Order:0x007fb0153bfef8 @signatures=[]>
154
154
  ## attributes with default values (here: signatures) are set
155
155
  ## automatically
156
156
 
157
157
  xml=o.save_to_xml
158
158
  XML::MappingError: no value, and no default value, for attribute: reference
159
- from ../lib/xml/../xml/mapping/base.rb:697:in `obj_to_xml'
160
- from ../lib/xml/../xml/mapping/base.rb:218:in `fill_into_xml'
161
- from ../lib/xml/../xml/mapping/base.rb:217:in `each'
162
- from ../lib/xml/../xml/mapping/base.rb:217:in `fill_into_xml'
163
- from ../lib/xml/../xml/mapping/base.rb:229:in `save_to_xml'
159
+ from /home/olaf/xml-mapping/lib/xml/mapping/base.rb:712:in `obj_to_xml'
160
+ from /home/olaf/xml-mapping/lib/xml/mapping/base.rb:218:in `block in fill_into_xml'
161
+ from /home/olaf/xml-mapping/lib/xml/mapping/base.rb:217:in `each'
162
+ from /home/olaf/xml-mapping/lib/xml/mapping/base.rb:217:in `fill_into_xml'
163
+ from /home/olaf/xml-mapping/lib/xml/mapping/base.rb:229:in `save_to_xml'
164
164
  ## can't save as long as there are still unset attributes without
165
165
  ## default values
166
166
 
@@ -10,13 +10,13 @@ end
10
10
  ### usage
11
11
 
12
12
  p1 = Person.load_from_xml(REXML::Document.new('<person name="Jim"/>').root)
13
- => #<Person:0x7f48763d3360 @name="Jim">
13
+ => #<Person:0x007fb014e95298 @name="Jim">
14
14
 
15
15
  p2 = Person.load_from_xml(REXML::Document.new('<person><name>James</name></person>').root)
16
- => #<Person:0x7f48763ce900 @name="James">
16
+ => #<Person:0x007fb014e847b8 @name="James">
17
17
 
18
18
  p3 = Person.load_from_xml(REXML::Document.new('<person>Suzy</person>').root)
19
- => #<Person:0x7f48763c99f0 @name="Suzy">
19
+ => #<Person:0x007fb014e6c550 @name="Suzy">
20
20
 
21
21
 
22
22
  p1.save_to_xml.write($stdout)
@@ -66,7 +66,7 @@ xml = REXML::Document.new('
66
66
 
67
67
  ## load using the default mapping
68
68
  p = Person.load_from_xml xml
69
- => #<Person:0x7f487663af90 @address=#<Address:0x7f4876637250 @number=72, @zip=18827, @city="London", @street="Abbey Road">, @name="Suzy", @age=28>
69
+ => #<Person:0x007fb015506640 @name="Suzy", @age=28, @address=#<Address:0x007fb015505a38 @street="Abbey Road", @number=72, @city="London", @zip=18827>>
70
70
 
71
71
  ## save using the default mapping
72
72
  xml2 = p.save_to_xml
@@ -109,6 +109,6 @@ other_xml.write $stdout,2
109
109
  </individual>
110
110
  ## load it again using the :other mapping
111
111
  p2 = Person.load_from_xml other_xml, :mapping=>:other
112
- => #<Person:0x7f4876600958 @address=#<Address:0x7f48765fdfc8 @number=72, @zip=18827, @city="London", @street="Abbey Road">, @name="Suzy", @age=28>
112
+ => #<Person:0x007fb0154bad30 @name="Suzy", @age=28, @address=#<Address:0x007fb0154b98e0 @street="Abbey Road", @number=72, @city="London", @zip=18827>>
113
113
 
114
114
  ## p2 identical to p
@@ -9,7 +9,7 @@ end
9
9
  ### usage
10
10
 
11
11
  p1 = Publication.load_from_xml(REXML::Document.new('<publication author="Jim"/>').root)
12
- => #<Publication:0x7f487656b308 @author="Jim">
12
+ => #<Publication:0x007fb01548e668 @author="Jim">
13
13
 
14
14
  p2 = Publication.load_from_xml(REXML::Document.new('
15
15
  <publication>
@@ -17,4 +17,4 @@ p2 = Publication.load_from_xml(REXML::Document.new('
17
17
  <contr>Mel</contr>
18
18
  <contr>Toby</contr>
19
19
  </publication>').root)
20
- => #<Publication:0x7f48766be930 @contributors=["Chris", "Mel", "Toby"]>
20
+ => #<Publication:0x007fb01547ef38 @contributors=["Chris", "Mel", "Toby"]>
@@ -12,7 +12,7 @@ class Foo
12
12
  end
13
13
 
14
14
  f = Foo.load_from_xml(REXML::Document.new('<foo name="Jim" more="XYZ"/>').root)
15
- => #<Foo:0x7f48766cdef8 @name="JimXYZ">
15
+ => #<Foo:0x007fb015460c40 @name="JimXYZ">
16
16
 
17
17
  xml = f.save_to_xml
18
18
  xml.write $stdout,2
@@ -1,5 +1,5 @@
1
1
  ppl=People.load_from_file("stringarray.xml")
2
- => #<People:0x7f487665ecd8 @names=["Jim", "Susan", "Herbie", "Nancy"]>
2
+ => #<People:0x007fb015431a58 @names=["Jim", "Susan", "Herbie", "Nancy"]>
3
3
  ppl.names
4
4
  => ["Jim", "Susan", "Herbie", "Nancy"]
5
5
 
@@ -15,21 +15,21 @@ nowxml=Time.now.save_to_xml
15
15
  nowxml.write($stdout,2)
16
16
  <time>
17
17
  <year>
18
- 2010
18
+ 2014
19
19
  </year>
20
20
  <month>
21
- 5
21
+ 9
22
22
  </month>
23
23
  <mday>
24
- 21
24
+ 19
25
25
  </mday>
26
26
  <hours>
27
- 16
27
+ 14
28
28
  </hours>
29
29
  <minutes>
30
- 42
30
+ 1
31
31
  </minutes>
32
32
  <seconds>
33
- 15
33
+ 43
34
34
  </seconds>
35
35
  </time>
@@ -64,7 +64,7 @@ firstbazelt=XML::XXPath.new("/bar/baz").first(rootelt)
64
64
  path2=XML::XXPath.new("@key2")
65
65
 
66
66
  path2.create_new(firstbazelt)
67
- => #<XML::XXPath::Accessors::Attribute:0x7f48764e8688 @name="key2", @parent=<baz key='work' key2='[unset]'> ... </>>
67
+ => #<XML::XXPath::Accessors::Attribute:0x007fb014e37210 @parent=<baz key='work' key2='[unset]'> ... </>, @name="key2">
68
68
  d.write($stdout,2)
69
69
 
70
70
  <foo>
@@ -87,12 +87,12 @@ d.write($stdout,2)
87
87
  ### same call again...
88
88
  path2.create_new(firstbazelt)
89
89
  XML::XXPathError: XPath (@key2): create_new and attribute already exists
90
- from ../lib/xml/../xml/xxpath/steps.rb:215:in `create_on'
91
- from ../lib/xml/../xml/xxpath/steps.rb:80:in `creator'
92
- from ../lib/xml/xxpath.rb:91:in `call'
93
- from ../lib/xml/xxpath.rb:91:in `all'
94
- from ../lib/xml/xxpath.rb:70:in `first'
95
- from ../lib/xml/xxpath.rb:112:in `create_new'
90
+ from /home/olaf/xml-mapping/lib/xml/xxpath/steps.rb:215:in `create_on'
91
+ from /home/olaf/xml-mapping/lib/xml/xxpath/steps.rb:80:in `block in creator'
92
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:91:in `call'
93
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:91:in `all'
94
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:70:in `first'
95
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:112:in `create_new'
96
96
  ### can't create that path anew again -- an element can't have more
97
97
  ### than one attribute with the same name
98
98
 
@@ -150,12 +150,12 @@ d.write($stdout,2)
150
150
 
151
151
  XML::XXPath.new("baz[6]").create_new(baz6elt.parent)
152
152
  XML::XXPathError: XPath: baz[6]: create_new and element already exists
153
- from ../lib/xml/../xml/xxpath/steps.rb:167:in `create_on'
154
- from ../lib/xml/../xml/xxpath/steps.rb:80:in `creator'
155
- from ../lib/xml/xxpath.rb:91:in `call'
156
- from ../lib/xml/xxpath.rb:91:in `all'
157
- from ../lib/xml/xxpath.rb:70:in `first'
158
- from ../lib/xml/xxpath.rb:112:in `create_new'
153
+ from /home/olaf/xml-mapping/lib/xml/xxpath/steps.rb:167:in `create_on'
154
+ from /home/olaf/xml-mapping/lib/xml/xxpath/steps.rb:80:in `block in creator'
155
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:91:in `call'
156
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:91:in `all'
157
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:70:in `first'
158
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:112:in `create_new'
159
159
  ### yep, baz[6] already existed and thus couldn't be created once
160
160
  ### again
161
161
 
@@ -96,7 +96,7 @@ d.write($stdout,2)
96
96
  </foo>### no change
97
97
 
98
98
  XML::XXPath.new("/bar/baz[6]/@haha").first(rootelt,:ensure_created=>true)
99
- => #<XML::XXPath::Accessors::Attribute:0x7f4876522658 @name="haha", @parent=<baz haha='[unset]'/>>
99
+ => #<XML::XXPath::Accessors::Attribute:0x007fb014a51d08 @parent=<baz haha='[unset]'/>, @name="haha">
100
100
  d.write($stdout,2)
101
101
 
102
102
  <foo>
@@ -115,7 +115,7 @@ d.write($stdout,2)
115
115
  </foo>### for there to be a 6th "baz" element, there must be 1st..5th "baz" elements
116
116
 
117
117
  XML::XXPath.new("/bar/baz[6]/@haha").first(rootelt,:ensure_created=>true)
118
- => #<XML::XXPath::Accessors::Attribute:0x7f4876516bc8 @name="haha", @parent=<baz haha='[unset]'/>>
118
+ => #<XML::XXPath::Accessors::Attribute:0x007fb014a479c0 @parent=<baz haha='[unset]'/>, @name="haha">
119
119
  d.write($stdout,2)
120
120
 
121
121
  <foo>
@@ -44,7 +44,7 @@ path2.all(d)
44
44
  ## "first" raises XML::XXPathError in such cases...
45
45
  path2.first(d)
46
46
  XML::XXPathError: path not found: /foo/bar[2]/baz[4]
47
- from ../lib/xml/xxpath.rb:75:in `first'
47
+ from /home/olaf/xml-mapping/lib/xml/xxpath.rb:75:in `first'
48
48
 
49
49
  ##...unless we allow nil returns
50
50
  path2.first(d,:allow_nil=>true)
@@ -1,8 +1,6 @@
1
1
  # xml-mapping -- bidirectional Ruby-XML mapper
2
2
  # Copyright (C) 2004-2006 Olaf Klischat
3
3
 
4
- $:.unshift(File.dirname(__FILE__)+"/..")
5
-
6
4
  require 'xml/mapping/base'
7
5
  require 'xml/mapping/standard_nodes'
8
6
 
@@ -301,16 +301,26 @@ module XML
301
301
  end
302
302
 
303
303
  # Add getter and setter methods for a new attribute named _name_
304
- # to this class. This is a convenience method intended to be
305
- # called from Node class initializers.
304
+ # (must be a symbol or a string) to this class, taking care not
305
+ # to replace existing getters/setters. This is a convenience
306
+ # method intended to be called from Node class initializers.
306
307
  def add_accessor(name)
307
- name = name.id2name if name.kind_of? Symbol
308
- unless self.instance_methods.include?(name)
308
+ # existing methods search. Search for symbols and strings
309
+ # to be compatible with Ruby 1.8 and 1.9.
310
+ methods = self.instance_methods
311
+ if methods[0].kind_of? Symbol
312
+ getter = :"#{name}"
313
+ setter = :"#{name}="
314
+ else
315
+ getter = "#{name}"
316
+ setter = "#{name}="
317
+ end
318
+ unless methods.include?(getter)
309
319
  self.module_eval <<-EOS
310
320
  attr_reader :#{name}
311
321
  EOS
312
322
  end
313
- unless self.instance_methods.include?("#{name}=")
323
+ unless methods.include?(setter)
314
324
  self.module_eval <<-EOS
315
325
  attr_writer :#{name}
316
326
  EOS
@@ -337,6 +347,11 @@ module XML
337
347
  # doesn't work, try allocate, which bypasses the initializer.
338
348
  begin
339
349
  obj = self.new
350
+ #TODO: this will normally invoke our base XML::Mapping#initialize, which calls
351
+ # obj.initialize_xml_mapping, which is called below again (with the correct :mapping parameter).
352
+ # obj.initialize_xml_mapping calls obj_initializing on all nodes.
353
+ # So obj_initializing may be called on the nodes twice for this initialization.
354
+ # Maybe document this for node writers?
340
355
  rescue ArgumentError # TODO: this may hide real errors.
341
356
  # how to statically check whether
342
357
  # self self.new accepts an empty
@@ -538,9 +553,9 @@ module XML
538
553
  alias_method :default_xml_to_obj, :xml_to_obj
539
554
  def xml_to_obj(obj,xml)
540
555
  begin
541
- @options[:reader].call(obj,xml)
542
- rescue ArgumentError
543
556
  @options[:reader].call(obj,xml,self.method(:default_xml_to_obj))
557
+ rescue ArgumentError # thrown if @options[:reader] is a lambda (i.e. no Proc) with !=3 args (e.g. proc{...} in ruby1.8)
558
+ @options[:reader].call(obj,xml)
544
559
  end
545
560
  end
546
561
  end
@@ -552,9 +567,9 @@ module XML
552
567
  alias_method :default_obj_to_xml, :obj_to_xml
553
568
  def obj_to_xml(obj,xml)
554
569
  begin
555
- @options[:writer].call(obj,xml)
556
- rescue ArgumentError
557
570
  @options[:writer].call(obj,xml,self.method(:default_obj_to_xml))
571
+ rescue ArgumentError # thrown if (see above)
572
+ @options[:writer].call(obj,xml)
558
573
  end
559
574
  end
560
575
  end
@@ -3,6 +3,6 @@
3
3
 
4
4
  module XML
5
5
  module Mapping
6
- VERSION = '0.9.1'
6
+ VERSION = '0.10.0'
7
7
  end
8
8
  end
@@ -101,3 +101,46 @@ class WriterTest
101
101
  }
102
102
  text_node :bar, "bar"
103
103
  end
104
+
105
+
106
+ class ReaderWriterProcVsLambdaTest
107
+ include XML::Mapping
108
+
109
+ attr_accessor :read, :written
110
+
111
+ text_node :proc_2args, "proc_2args", :reader=>Proc.new{|obj,xml|
112
+ (obj.read||=[]) << :proc_2args
113
+ },
114
+ :writer=>Proc.new{|obj,xml|
115
+ (obj.written||=[]) << :proc_2args
116
+ }
117
+
118
+ text_node :proc_3args, "proc_3args", :reader=>Proc.new{|obj,xml,default|
119
+ (obj.read||=[]) << :proc_3args
120
+ default.call(obj,xml)
121
+ },
122
+ :writer=>Proc.new{|obj,xml,default|
123
+ (obj.written||=[]) << :proc_3args
124
+ default.call(obj,xml)
125
+ }
126
+
127
+
128
+ text_node :lambda_2args, "lambda_2args", :reader=>lambda{|obj,xml|
129
+ (obj.read||=[]) << :lambda_2args
130
+ },
131
+ :writer=>lambda{|obj,xml|
132
+ (obj.written||=[]) << :lambda_2args
133
+ }
134
+
135
+
136
+ text_node :lambda_3args, "lambda_3args", :reader=>lambda{|obj,xml,default|
137
+ (obj.read||=[]) << :lambda_3args
138
+ default.call(obj,xml)
139
+ },
140
+ :writer=>lambda{|obj,xml,default|
141
+ (obj.written||=[]) << :lambda_3args
142
+ default.call(obj,xml)
143
+ }
144
+
145
+
146
+ end
@@ -4,6 +4,13 @@ class Entry
4
4
  include XML::Mapping
5
5
 
6
6
  text_node :name, "name"
7
+
8
+ def initialize(init_props={})
9
+ super() #the super initialize (inherited from XML::Mapping) must be called
10
+ init_props.entries.each do |name,value|
11
+ self.send :"#{name}=", value
12
+ end
13
+ end
7
14
  end
8
15
 
9
16