forked from andreyberya/xml-mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompany.rb
103 lines (72 loc) · 2.55 KB
/
company.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'xml/mapping'
# forward declarations
class Address; end
class Office; end
class Customer; end
class Thing; end
class Company
include XML::Mapping
text_node :name, "@name"
object_node :address, "address", :class=>Address
array_node :offices, "offices", "office", :class=>Office
hash_node :customers, "customers", "customer", "@uid", :class=>Customer
text_node :ent1, "arrtest/entry[1]"
text_node :ent2, "arrtest/entry[2]"
text_node :ent3, "arrtest/entry[3]"
array_node :stuff, "stuff", "*"
array_node :things, "stuff2", "thing", :class=>Thing
object_node :test_default_value_identity, "dummy", :default_value => ["default"]
end
class Address
include XML::Mapping
text_node :city, "city"
numeric_node :zip, "zip", :default_value=>12576
text_node :street, "street", :optional=>true
numeric_node :number, "number"
end
class Office
include XML::Mapping
text_node :speciality, "@speciality"
boolean_node :classified, "classified", "yes", "no"
# object_node :address, "address", :class=>Address
object_node :address, "address",
:marshaller=>proc {|xml,value| value.fill_into_xml(xml)},
:unmarshaller=>proc {|xml| Address.load_from_xml(xml)}
end
class Customer
include XML::Mapping
text_node :uid, "@uid"
text_node :name, "name"
end
class Thing
include XML::Mapping
choice_node 'name', (text_node :name, 'name'),
'@name', (text_node :name, '@name'),
:else, (text_node :name, '.')
end
class Names1
include XML::Mapping
choice_node :if, 'name', :then, (text_node :name, 'name'),
:elsif, 'names/name', :then, (array_node :names, 'names', 'name', :class=>String)
end
class ReaderTest
include XML::Mapping
attr_accessor :read
text_node :foo, "foo"
text_node :foo2, "foo2", :reader=>proc{|obj,xml| (obj.read||=[]) << :foo2 }
text_node :foo3, "foo3", :reader=>proc{|obj,xml,default|
(obj.read||=[]) << :foo3
default.call(obj,xml)
}
text_node :bar, "bar"
end
class WriterTest
include XML::Mapping
text_node :foo, "foo"
text_node :foo2, "foo2", :writer=>proc{|obj,xml| e = xml.elements.add; e.name='quux'; e.text='dingdong2' }
text_node :foo3, "foo3", :writer=>proc{|obj,xml,default|
default.call(obj,xml)
e = xml.elements.add; e.name='quux'; e.text='dingdong3'
}
text_node :bar, "bar"
end