xml-mapping 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
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,19 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- begin
4
- Object.send(:remove_const, "Address") # name clash with order_usage...
5
- rescue
6
- end
7
- require 'company' #<=
8
- #:visible:
9
- c = Company.load_from_file('company.xml') #<=
10
- c.name #<=
11
- c.customers.size #<=
12
- c.customers[1] #<=
13
- c.customers[1].name #<=
14
- c.customers[0].name #<=
15
- c.customers[0].name = 'James Tiberius Kirk' #<=
16
- c.customers << Customer.new('cm','Cookie Monster') #<=
17
- xml2 = c.save_to_xml #<=
18
- #:invisible_retval:
19
- xml2.write($stdout,2) #<=
@@ -1,18 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- require 'documents_folders' #<=
4
- #:visible:
5
-
6
- root = XML::Mapping.load_object_from_file "documents_folders.xml" #<=
7
- root.name #<=
8
- root.entries #<=
9
-
10
- root.append "etc", Folder.new
11
- root["etc"].append "passwd", Document.new
12
- root["etc"]["passwd"].contents = "foo:x:2:2:/bin/sh"
13
- root["etc"].append "hosts", Document.new
14
- root["etc"]["hosts"].contents = "127.0.0.1 localhost"
15
-
16
- xml = root.save_to_xml #<=
17
- #:invisible_retval:
18
- xml.write $stdout,2
@@ -1,12 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- require 'xml/mapping'
4
- require 'time_node.rb'
5
- require 'order'
6
- require 'order_signature_enhanced'
7
- #<=
8
- #:visible:
9
- s=Signature.load_from_file("order_signature_enhanced.xml") #<=
10
- s.signed_on #<=
11
- s.signed_on=Time.local(1976,12,18) #<=
12
- s.save_to_xml.write($stdout,2) #<=
@@ -1,120 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- begin
4
- Object.send(:remove_const, "Address") # name clash with company_usage...
5
- rescue
6
- end
7
- require 'order'
8
-
9
- require 'xml/xxpath_methods'
10
-
11
- require 'test/unit/assertions'
12
- include Test::Unit::Assertions #<=
13
- #:visible:
14
- ####read access
15
- o=Order.load_from_file("order.xml") #<=
16
- o.reference #<=
17
- o.client #<=
18
- o.items.keys #<=
19
- o.items["RF-0034"].descr #<=
20
- o.items["RF-0034"].total_price #<=
21
- o.signatures #<=
22
- o.signatures[2].name #<=
23
- o.signatures[2].position #<=
24
- ## default value was set
25
-
26
- o.total_price #<=
27
-
28
- #:invisible:
29
- assert_equal "12343-AHSHE-314159", o.reference
30
- assert_equal "Jean Smith", o.client.name
31
- assert_equal "San Francisco", o.client.work_address.city
32
- assert_equal "San Mateo", o.client.home_address.city
33
- assert_equal %w{RF-0001 RF-0034 RF-3341}, o.items.keys.sort
34
- assert_equal ['John Doe','Jill Smith','Miles O\'Brien'], o.signatures.map{|s|s.name}
35
- assert_equal 2575, (10 * o.total_price).round
36
- #<=
37
- #:visible:
38
-
39
- ####write access
40
- o.client.name="James T. Kirk"
41
- o.items['RF-4711'] = Item.new
42
- o.items['RF-4711'].descr = 'power transfer grid'
43
- o.items['RF-4711'].quantity = 2
44
- o.items['RF-4711'].unit_price = 29.95
45
-
46
- s=Signature.new
47
- s.name='Harry Smith'
48
- s.position='general manager'
49
- o.signatures << s
50
- xml=o.save_to_xml #convert to REXML node; there's also o.save_to_file(name) #<=
51
- #:invisible_retval:
52
- xml.write($stdout,2) #<=
53
-
54
- #:invisible:
55
- assert_equal %w{RF-0001 RF-0034 RF-3341 RF-4711}, xml.all_xpath("Item/@reference").map{|x|x.text}.sort
56
- assert_equal ['John Doe','Jill Smith','Miles O\'Brien','Harry Smith'],
57
- xml.all_xpath("Signed-By/Signature/Name").map{|x|x.text}
58
- #<=
59
- #:visible:
60
-
61
-
62
- #<=
63
- #:visible_retval:
64
- ####Starting a new order from scratch
65
- o = Order.new #<=
66
- ## attributes with default values (here: signatures) are set
67
- ## automatically
68
-
69
- #:handle_exceptions:
70
- xml=o.save_to_xml #<=
71
- #:no_exceptions:
72
- ## can't save as long as there are still unset attributes without
73
- ## default values
74
-
75
- o.reference = "FOOBAR-1234"
76
-
77
- o.client = Client.new
78
- o.client.name = 'Ford Prefect'
79
- o.client.home_address = Address.new
80
- o.client.home_address.street = '42 Park Av.'
81
- o.client.home_address.city = 'small planet'
82
- o.client.home_address.zip = 17263
83
- o.client.home_address.state = 'Betelgeuse system'
84
-
85
- o.items={'XY-42' => Item.new}
86
- o.items['XY-42'].descr = 'improbability drive'
87
- o.items['XY-42'].quantity = 3
88
- o.items['XY-42'].unit_price = 299.95
89
-
90
- #:invisible_retval:
91
- xml=o.save_to_xml
92
- xml.write($stdout,2)
93
- #<=
94
- #:invisible:
95
- assert_equal "order", xml.name
96
- assert_equal o.reference, xml.first_xpath("@reference").text
97
- assert_equal o.client.name, xml.first_xpath("Client/Name").text
98
- assert_equal o.client.home_address.street, xml.first_xpath("Client/Address[@where='home']/Street").text
99
- assert_equal o.client.home_address.city, xml.first_xpath("Client/Address[@where='home']/City").text
100
- assert_nil xml.first_xpath("Client/Address[@where='work']", :allow_nil=>true)
101
- assert_equal 1, xml.all_xpath("Client/Address").size
102
-
103
- o.client.work_address = Address.new
104
- o.client.work_address.street = 'milky way 2'
105
- o.client.work_address.city = 'Ursa Major'
106
- o.client.work_address.zip = 18293
107
- o.client.work_address.state = 'Magellan Cloud'
108
- xml=o.save_to_xml
109
-
110
- assert_equal o.client.work_address.street, xml.first_xpath("Client/Address[@where='work']/Street").text
111
- assert_equal o.client.work_address.city, xml.first_xpath("Client/Address[@where='work']/City").text
112
- assert_equal o.client.home_address.street, xml.first_xpath("Client/Address[@where='home']/Street").text
113
- assert_equal 2, xml.all_xpath("Client/Address").size
114
- #<=
115
- #:visible:
116
-
117
- ## the root element name when saving an object to XML will by default
118
- ## be derived from the class name (in this example, "Order" became
119
- ## "order"). This can be overridden on a per-class basis; see
120
- ## XML::Mapping::ClassMethods#root_element_name for details.
@@ -1,44 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- require 'xml/mapping'
4
- require 'xml/xxpath_methods'
5
- #<=
6
- #:visible:
7
-
8
- class Person
9
- include XML::Mapping
10
-
11
- choice_node :if, 'name', :then, (text_node :name, 'name'),
12
- :elsif, '@name', :then, (text_node :name, '@name'),
13
- :else, (text_node :name, '.')
14
- end
15
-
16
- ### usage
17
-
18
- p1 = Person.load_from_xml(REXML::Document.new('<person name="Jim"/>').root)#<=
19
-
20
- p2 = Person.load_from_xml(REXML::Document.new('<person><name>James</name></person>').root)#<=
21
-
22
- p3 = Person.load_from_xml(REXML::Document.new('<person>Suzy</person>').root)#<=
23
-
24
-
25
- #:invisible_retval:
26
- p1.save_to_xml.write($stdout)#<=
27
-
28
- p2.save_to_xml.write($stdout)#<=
29
-
30
- p3.save_to_xml.write($stdout)#<=
31
-
32
- #:invisible:
33
- require 'test/unit/assertions'
34
- include Test::Unit::Assertions
35
-
36
- assert_equal "Jim", p1.name
37
- assert_equal "James", p2.name
38
- assert_equal "Suzy", p3.name
39
-
40
- xml = p3.save_to_xml
41
- assert_equal "name", xml.elements[1].name
42
- assert_equal "Suzy", xml.elements[1].text
43
-
44
- #<=
@@ -1,119 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- begin
4
- Object.send(:remove_const, "Address") # remove any previous definitions
5
- Object.send(:remove_const, "Person") # remove any previous definitions
6
- rescue
7
- end
8
- #<=
9
- #:visible:
10
- require 'xml/mapping'
11
-
12
- class Address; end
13
-
14
- class Person
15
- include XML::Mapping
16
-
17
- # the default mapping. Stores the name and age in XML attributes,
18
- # and the address in a sub-element "address".
19
-
20
- text_node :name, "@name"
21
- numeric_node :age, "@age"
22
- object_node :address, "address", :class=>Address
23
-
24
- use_mapping :other
25
-
26
- # the ":other" mapping. Non-default root element name; name and age
27
- # stored in XML elements; address stored in the person's element
28
- # itself
29
-
30
- root_element_name "individual"
31
- text_node :name, "name"
32
- numeric_node :age, "age"
33
- object_node :address, ".", :class=>Address
34
-
35
- # you could also specify the mapping on a per-node basis with the
36
- # :mapping option, e.g.:
37
- #
38
- # numeric_node :age, "age", :mapping=>:other
39
- end
40
-
41
-
42
- class Address
43
- include XML::Mapping
44
-
45
- # the default mapping.
46
-
47
- text_node :street, "street"
48
- numeric_node :number, "number"
49
- text_node :city, "city"
50
- numeric_node :zip, "zip"
51
-
52
- use_mapping :other
53
-
54
- # the ":other" mapping.
55
-
56
- text_node :street, "street-name"
57
- numeric_node :number, "street-name/@number"
58
- text_node :city, "city-name"
59
- numeric_node :zip, "city-name/@zip-code"
60
- end
61
-
62
-
63
- ### usage
64
-
65
- ## XML representation of a person in the default mapping
66
- xml = REXML::Document.new('
67
- <person name="Suzy" age="28">
68
- <address>
69
- <street>Abbey Road</street>
70
- <number>72</number>
71
- <city>London</city>
72
- <zip>18827</zip>
73
- </address>
74
- </person>').root
75
-
76
- ## load using the default mapping
77
- p = Person.load_from_xml xml #<=
78
-
79
- #:invisible_retval:
80
- ## save using the default mapping
81
- xml2 = p.save_to_xml
82
- xml2.write $stdout,2 #<=
83
-
84
- ## xml2 identical to xml
85
-
86
-
87
- ## now, save the same person to XML using the :other mapping...
88
- other_xml = p.save_to_xml :mapping=>:other
89
- other_xml.write $stdout,2 #<=
90
-
91
- #:visible_retval:
92
- ## load it again using the :other mapping
93
- p2 = Person.load_from_xml other_xml, :mapping=>:other #<=
94
-
95
- #:invisible_retval:
96
- ## p2 identical to p #<=
97
-
98
- #:invisible:
99
- require 'test/unit/assertions'
100
- include Test::Unit::Assertions
101
-
102
- require 'xml/xxpath_methods'
103
-
104
- assert_equal "Suzy", p.name
105
- assert_equal 28, p.age
106
- assert_equal "Abbey Road", p.address.street
107
- assert_equal 72, p.address.number
108
- assert_equal "London", p.address.city
109
- assert_equal 18827, p.address.zip
110
-
111
- assert_equal "individual", other_xml.name
112
- assert_equal p.name, other_xml.first_xpath("name").text
113
- assert_equal p.age, other_xml.first_xpath("age").text.to_i
114
- assert_equal p.address.street, other_xml.first_xpath("street-name").text
115
- assert_equal p.address.number, other_xml.first_xpath("street-name/@number").text.to_i
116
- assert_equal p.address.city, other_xml.first_xpath("city-name").text
117
- assert_equal p.address.zip, other_xml.first_xpath("city-name/@zip-code").text.to_i
118
-
119
- #<=
@@ -1,44 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- require 'xml/mapping'
4
- require 'xml/xxpath_methods'
5
- #<=
6
- #:visible:
7
-
8
- class Publication
9
- include XML::Mapping
10
-
11
- choice_node :if, '@author', :then, (text_node :author, '@author'),
12
- :elsif, 'contr', :then, (array_node :contributors, 'contr', :class=>String)
13
- end
14
-
15
- ### usage
16
-
17
- p1 = Publication.load_from_xml(REXML::Document.new('<publication author="Jim"/>').root)#<=
18
-
19
- p2 = Publication.load_from_xml(REXML::Document.new('
20
- <publication>
21
- <contr>Chris</contr>
22
- <contr>Mel</contr>
23
- <contr>Toby</contr>
24
- </publication>').root)#<=
25
-
26
- #:invisible:
27
- require 'test/unit/assertions'
28
- include Test::Unit::Assertions
29
-
30
- assert_equal "Jim", p1.author
31
- assert_nil p1.contributors
32
-
33
- assert_nil p2.author
34
- assert_equal ["Chris", "Mel", "Toby"], p2.contributors
35
-
36
- xml1 = p1.save_to_xml
37
- xml2 = p2.save_to_xml
38
-
39
- assert_equal p1.author, xml1.first_xpath("@author").text
40
- assert_nil xml1.first_xpath("contr", :allow_nil=>true)
41
-
42
- assert_nil xml2.first_xpath("@author", :allow_nil=>true)
43
- assert_equal p2.contributors, xml2.all_xpath("contr").map{|elt|elt.text}
44
- #<=
@@ -1,33 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- require 'xml/mapping'
4
- require 'xml/xxpath_methods'
5
- #<=
6
- #:visible:
7
-
8
- class Foo
9
- include XML::Mapping
10
-
11
- text_node :name, "@name", :reader=>proc{|obj,xml,default_reader|
12
- default_reader.call(obj,xml)
13
- obj.name += xml.attributes['more']
14
- },
15
- :writer=>proc{|obj,xml|
16
- xml.attributes['bar'] = "hi #{obj.name} ho"
17
- }
18
- end
19
-
20
- f = Foo.load_from_xml(REXML::Document.new('<foo name="Jim" more="XYZ"/>').root)#<=
21
-
22
- #:invisible_retval:
23
- xml = f.save_to_xml
24
- xml.write $stdout,2 #<=
25
-
26
- #:invisible:
27
- require 'test/unit/assertions'
28
- include Test::Unit::Assertions
29
-
30
- assert_equal "JimXYZ", f.name
31
- assert_equal "hi JimXYZ ho", xml.attributes['bar']
32
-
33
- #<=
@@ -1,11 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- require 'stringarray' #<=
4
- #:visible:
5
- ppl=People.load_from_file("stringarray.xml") #<=
6
- ppl.names #<=
7
-
8
- ppl.names.concat ["Mary","Arnold"] #<=
9
- #:invisible_retval:
10
- ppl.save_to_xml.write $stdout,2
11
- #<=
@@ -1,19 +0,0 @@
1
- #:invisible:
2
- $:.unshift "../lib"
3
- require 'order' #<=
4
- #:visible:
5
- class Time
6
- include XML::Mapping
7
-
8
- numeric_node :year, "year"
9
- numeric_node :month, "month"
10
- numeric_node :day, "mday"
11
- numeric_node :hour, "hours"
12
- numeric_node :min, "minutes"
13
- numeric_node :sec, "seconds"
14
- end
15
-
16
-
17
- nowxml=Time.now.save_to_xml #<=
18
- #:invisible_retval:
19
- nowxml.write($stdout,2)#<=