roxml 1.2 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/MIT-LICENSE +18 -0
  2. data/README.rdoc +126 -0
  3. data/Rakefile +100 -0
  4. data/lib/roxml.rb +479 -485
  5. data/lib/roxml/extensions/active_support.rb +32 -0
  6. data/lib/roxml/extensions/array.rb +5 -0
  7. data/lib/roxml/extensions/array/conversions.rb +25 -0
  8. data/lib/roxml/extensions/deprecation.rb +28 -0
  9. data/lib/roxml/extensions/string.rb +21 -0
  10. data/lib/roxml/extensions/string/conversions.rb +44 -0
  11. data/lib/roxml/extensions/string/iterators.rb +12 -0
  12. data/lib/roxml/options.rb +266 -0
  13. data/lib/roxml/xml.rb +223 -0
  14. data/lib/roxml/xml/libxml.rb +63 -0
  15. data/lib/roxml/xml/rexml.rb +64 -0
  16. data/roxml.gemspec +105 -0
  17. data/test/fixtures/book_text_with_attribute.xml +1 -1
  18. data/test/fixtures/book_valid.xml +2 -2
  19. data/test/fixtures/book_with_authors.xml +7 -0
  20. data/test/fixtures/book_with_contributors_attrs.xml +7 -0
  21. data/test/fixtures/book_with_default_namespace.xml +9 -0
  22. data/test/fixtures/book_with_depth.xml +6 -0
  23. data/test/fixtures/book_with_wrapped_attr.xml +3 -0
  24. data/test/fixtures/dictionary_of_attr_name_clashes.xml +8 -0
  25. data/test/fixtures/dictionary_of_attrs.xml +6 -0
  26. data/test/fixtures/dictionary_of_guarded_names.xml +6 -0
  27. data/test/fixtures/dictionary_of_mixeds.xml +4 -0
  28. data/test/fixtures/dictionary_of_name_clashes.xml +10 -0
  29. data/test/fixtures/dictionary_of_names.xml +4 -0
  30. data/test/fixtures/dictionary_of_texts.xml +10 -0
  31. data/test/fixtures/library.xml +1 -1
  32. data/test/fixtures/library_uppercase.xml +30 -0
  33. data/test/fixtures/muffins.xml +3 -0
  34. data/test/fixtures/nameless_ageless_youth.xml +2 -0
  35. data/test/fixtures/node_with_attr_name_conflicts.xml +1 -0
  36. data/test/fixtures/node_with_name_conflicts.xml +4 -0
  37. data/test/fixtures/numerology.xml +4 -0
  38. data/test/fixtures/person_with_guarded_mothers.xml +13 -0
  39. data/test/fixtures/person_with_mothers.xml +10 -0
  40. data/test/mocks/dictionaries.rb +56 -0
  41. data/test/mocks/mocks.rb +244 -48
  42. data/test/release/dependencies_test.rb +32 -0
  43. data/test/test_helper.rb +44 -0
  44. data/test/unit/inheritance_test.rb +19 -0
  45. data/test/unit/options_test.rb +102 -0
  46. data/test/unit/roxml_test.rb +24 -0
  47. data/test/unit/string_test.rb +11 -0
  48. data/test/unit/to_xml_test.rb +52 -0
  49. data/test/unit/xml_attribute_test.rb +39 -0
  50. data/test/unit/xml_block_test.rb +81 -0
  51. data/test/unit/xml_bool_test.rb +105 -0
  52. data/test/unit/xml_construct_test.rb +77 -0
  53. data/test/unit/xml_hash_test.rb +65 -0
  54. data/test/unit/xml_initialize_test.rb +50 -0
  55. data/test/unit/xml_name_test.rb +108 -0
  56. data/test/unit/xml_namespace_test.rb +38 -0
  57. data/test/unit/xml_object_test.rb +116 -0
  58. data/test/unit/xml_required_test.rb +93 -0
  59. data/test/unit/xml_text_test.rb +68 -0
  60. metadata +138 -87
  61. data/MIT-LICENSE.txt +0 -9
  62. data/README +0 -2
  63. data/doc/classes/ROXML.html +0 -374
  64. data/doc/classes/ROXML.src/M000003.html +0 -19
  65. data/doc/classes/ROXML.src/M000004.html +0 -25
  66. data/doc/classes/ROXML.src/M000005.html +0 -22
  67. data/doc/classes/ROXML/ROXML_Class.html +0 -429
  68. data/doc/classes/ROXML/ROXML_Class.src/M000006.html +0 -27
  69. data/doc/classes/ROXML/ROXML_Class.src/M000007.html +0 -18
  70. data/doc/classes/ROXML/ROXML_Class.src/M000008.html +0 -19
  71. data/doc/classes/ROXML/ROXML_Class.src/M000009.html +0 -25
  72. data/doc/classes/ROXML/ROXML_Class.src/M000010.html +0 -24
  73. data/doc/classes/ROXML/ROXML_Class.src/M000011.html +0 -18
  74. data/doc/classes/ROXML/ROXML_Class.src/M000012.html +0 -18
  75. data/doc/classes/ROXML/XMLAttributeRef.html +0 -175
  76. data/doc/classes/ROXML/XMLAttributeRef.src/M000015.html +0 -19
  77. data/doc/classes/ROXML/XMLAttributeRef.src/M000016.html +0 -19
  78. data/doc/classes/ROXML/XMLObjectRef.html +0 -175
  79. data/doc/classes/ROXML/XMLObjectRef.src/M000013.html +0 -26
  80. data/doc/classes/ROXML/XMLObjectRef.src/M000014.html +0 -32
  81. data/doc/classes/ROXML/XMLRef.html +0 -166
  82. data/doc/classes/ROXML/XMLRef.src/M000017.html +0 -21
  83. data/doc/classes/ROXML/XMLTextRef.html +0 -198
  84. data/doc/classes/ROXML/XMLTextRef.src/M000018.html +0 -28
  85. data/doc/classes/ROXML/XMLTextRef.src/M000019.html +0 -34
  86. data/doc/classes/String.html +0 -165
  87. data/doc/classes/String.src/M000001.html +0 -23
  88. data/doc/classes/String.src/M000002.html +0 -23
  89. data/doc/created.rid +0 -1
  90. data/doc/files/lib/roxml_rb.html +0 -234
  91. data/doc/fr_class_index.html +0 -33
  92. data/doc/fr_file_index.html +0 -27
  93. data/doc/fr_method_index.html +0 -45
  94. data/doc/index.html +0 -24
  95. data/doc/rdoc-style.css +0 -208
  96. data/test/fixture_helper.rb +0 -5
  97. data/test/test_roxml.rb +0 -105
@@ -0,0 +1,56 @@
1
+ require 'lib/roxml'
2
+
3
+ class DictionaryOfAttrs
4
+ include ROXML
5
+
6
+ xml_name :dictionary
7
+ xml_reader :definitions, {:attrs => [:dt, :dd]}, :in => :definitions
8
+ end
9
+
10
+ class DictionaryOfTexts
11
+ include ROXML
12
+
13
+ xml_name :dictionary
14
+ xml_reader :definitions, {:key => :word,
15
+ :value => :meaning}
16
+ end
17
+
18
+ class DictionaryOfMixeds
19
+ include ROXML
20
+
21
+ xml_name :dictionary
22
+ xml_reader :definitions, {:key => {:attr => :word},
23
+ :value => :content}
24
+ end
25
+
26
+ class DictionaryOfNames
27
+ include ROXML
28
+
29
+ xml_name :dictionary
30
+ xml_reader :definitions, {:key => :name,
31
+ :value => :content}
32
+ end
33
+
34
+ class DictionaryOfGuardedNames
35
+ include ROXML
36
+
37
+ xml_name :dictionary
38
+ xml_reader :definitions, {:key => :name,
39
+ :value => :content}, :in => :definitions
40
+ end
41
+
42
+ class DictionaryOfNameClashes
43
+ include ROXML
44
+
45
+ xml_name :dictionary
46
+ xml_reader :definitions, {:key => 'name',
47
+ :value => 'content'}, :from => :definition
48
+ end
49
+
50
+ class DictionaryOfAttrNameClashes
51
+ include ROXML
52
+
53
+ xml_name :dictionary
54
+ xml_reader :definitions, {:key => {:attr => :name},
55
+ :value => 'content'}, :from => :definition
56
+ end
data/test/mocks/mocks.rb CHANGED
@@ -1,75 +1,271 @@
1
1
  require "lib/roxml"
2
2
 
3
- class RoxmlMockObject
3
+ class Muffins
4
4
  include ROXML
5
+
6
+ xml_reader(:count, :from => 'bakers_dozens') {|val| val.to_i * 13 }
7
+ end
8
+
9
+ class MuffinsWithStackedBlocks
10
+ include ROXML
11
+
12
+ xml_reader(:count, :from => 'bakers_dozens', :as => Integer) {|val| val * 13 }
13
+ end
14
+
15
+ class Numerology
16
+ include ROXML
17
+
18
+ xml_reader :predictions, {:attrs => ['number', 'meaning']} do |k, v|
19
+ [Integer(k), v]
20
+ end
21
+ end
22
+
23
+ class Contributor
24
+ include ROXML
25
+
26
+ xml_reader :role, :attr
27
+ xml_reader :name
28
+ end
29
+
30
+ class WriteableContributor
31
+ include ROXML
32
+
33
+ xml_accessor :role, :attr
34
+ xml_accessor :name
35
+ end
36
+
37
+ class Book
38
+ include ROXML
39
+
40
+ xml_accessor :isbn, :attr => 'ISBN'
41
+ xml_reader :title
42
+ xml_reader :description, :as => :cdata
43
+ xml_reader :author
44
+ xml_accessor :pages, :text => 'pagecount', :as => Integer
45
+ end
46
+
47
+ class BookWithRequired
48
+ include ROXML
49
+
50
+ xml_accessor :isbn, :attr => 'ISBN', :required => true
51
+ xml_reader :title, :required => true
52
+ xml_reader :contributors, [Contributor], :in => 'contributor_array', :required => true
53
+ xml_reader :contributor_hash, {:attrs => ['role', 'name']},
54
+ :from => 'contributor', :in => 'contributor_hash', :required => true
5
55
  end
6
56
 
7
- class Book < RoxmlMockObject
8
- xml_attribute :isbn
9
- xml_text :title
10
- xml_text :description, nil, ROXML::TAG_CDATA
11
- xml_text :author
57
+ class BookWithAttrFrom
58
+ include ROXML
59
+
60
+ xml_accessor :isbn, :attr, :from => 'ISBN'
12
61
  end
13
62
 
14
- class Author < RoxmlMockObject
15
- xml_attribute :role
16
- xml_text :text, nil, ROXML::TEXT_CONTENT
63
+ class BookWithWrappedAttr
64
+ include ROXML
65
+
66
+ xml_name :book
67
+ xml_accessor :isbn, :attr => 'ISBN', :in => 'ids'
17
68
  end
18
69
 
19
- class BookWithAuthorTextAttribute < RoxmlMockObject
20
- xml_attribute :isbn
21
- xml_text :title
22
- xml_text :description, nil, ROXML::TAG_CDATA
23
- xml_object :author, Author
70
+ class Measurement
71
+ include ROXML
72
+
73
+ xml_reader :units, :attr
74
+ xml_reader :value, :content
75
+
76
+ def xml_initialize
77
+ initialize(value, units)
78
+ end
79
+
80
+ def initialize(value, units = 'pixels')
81
+ @value = Float(value)
82
+ @units = units.to_s
83
+ if @units.starts_with? 'hundredths-'
84
+ @value /= 100
85
+ @units = @units.split('hundredths-')[1]
86
+ end
87
+ end
88
+
89
+ def to_s
90
+ "#{value} #{units}"
91
+ end
92
+
93
+ def ==(other)
94
+ other.units == @units && other.value == @value
95
+ end
24
96
  end
25
97
 
26
- class Contributor < RoxmlMockObject
27
- xml_attribute :role
28
- xml_text :name
98
+ class BookWithDepth
99
+ include ROXML
100
+
101
+ xml_reader :isbn, :attr => 'ISBN'
102
+ xml_reader :title
103
+ xml_reader :description, :as => :cdata
104
+ xml_reader :author
105
+ xml_reader :depth, Measurement
29
106
  end
30
107
 
31
- class BookWithContributions < RoxmlMockObject
32
- xml_name :book
33
- xml_attribute :isbn
34
- xml_text :title
35
- xml_text :description
36
- xml_object :contributions, Contributor, ROXML::TAG_ARRAY, "contributions"
108
+ class InheritedBookWithDepth < Book
109
+ xml_reader :depth, Measurement
37
110
  end
38
111
 
39
- class BookWithContributors < RoxmlMockObject
40
- xml_name :book
41
- xml_attribute :isbn
42
- xml_text :title
43
- xml_text :description
44
- xml_object :contributors, Contributor, ROXML::TAG_ARRAY
112
+ class Author
113
+ include ROXML
114
+
115
+ xml_reader :role, :attr
116
+ xml_reader :text, :content
45
117
  end
46
118
 
47
- class Publisher < RoxmlMockObject
48
- xml_text :name
119
+ class BookWithAuthors
120
+ include ROXML
121
+
122
+ xml_name :book
123
+ xml_reader :isbn, :attr, :from => 'ISBN'
124
+ xml_reader :title
125
+ xml_reader :description, :as => :cdata
126
+ xml_reader :authors, :as => :array
49
127
  end
50
128
 
51
- class BookWithPublisher < RoxmlMockObject
52
- xml_name :book
53
- xml_attribute :isbn
54
- xml_text :title
55
- xml_text :description
56
- xml_object :publisher, Publisher
129
+ class BookWithAuthorTextAttribute
130
+ include ROXML
131
+
132
+ xml_name :book
133
+ xml_reader :isbn, :attr, :from => 'ISBN'
134
+ xml_reader :title
135
+ xml_reader :description, :as => :cdata
136
+ xml_reader :author, Author
57
137
  end
58
138
 
59
- class BookPair < RoxmlMockObject
60
- xml_attribute :isbn
61
- xml_text :title
62
- xml_text :description
63
- xml_text :author
64
- xml_object :book, Book
139
+ class BookWithContributions
140
+ include ROXML
141
+
142
+ xml_name :book
143
+ xml_reader :isbn, :attr
144
+ xml_reader :title
145
+ xml_reader :description
146
+ xml_reader :contributions, [Contributor], :from => 'contributor', :in => "contributions"
65
147
  end
66
148
 
67
- class Library < RoxmlMockObject
68
- xml_text :name
69
- xml_object :books, BookWithContributions, ROXML::TAG_ARRAY
149
+ class BookWithContributors
150
+ include ROXML
151
+
152
+ xml_name :book
153
+ xml_reader :isbn, :attr
154
+ xml_reader :title
155
+ xml_reader :description
156
+ xml_reader :contributors, Contributor, :as => :array
70
157
  end
71
158
 
72
- class Person < RoxmlMockObject
73
- xml_text :name, nil, ROXML::TEXT_CONTENT
159
+ class WriteableBookWithContributors
160
+ include ROXML
161
+
162
+ xml_name :book
163
+ xml_accessor :isbn, :attr
164
+ xml_accessor :title
165
+ xml_accessor :description
166
+ xml_accessor :contributors, Contributor, :as => :array
74
167
  end
75
168
 
169
+ class NamelessBook
170
+ include ROXML
171
+
172
+ xml_reader :isbn, :attr
173
+ xml_reader :title
174
+ xml_reader :description
175
+ xml_reader :contributors, Contributor, :as => :array
176
+ end
177
+
178
+ class Publisher
179
+ include ROXML
180
+
181
+ xml_reader :name
182
+ end
183
+
184
+ class BookWithPublisher
185
+ include ROXML
186
+
187
+ xml_reader :book
188
+ xml_reader :isbn, :attr
189
+ xml_reader :title
190
+ xml_reader :description
191
+ xml_reader :publisher, Publisher
192
+ end
193
+
194
+ class BookPair
195
+ include ROXML
196
+
197
+ xml_reader :isbn, :attr
198
+ xml_reader :title
199
+ xml_reader :description
200
+ xml_reader :author
201
+ xml_reader :book, Book
202
+ end
203
+
204
+ class Library
205
+ include ROXML
206
+
207
+ xml_reader :name
208
+ xml_reader :books, BookWithContributions, :as => :array
209
+ end
210
+
211
+ class UppercaseLibrary
212
+ include ROXML
213
+
214
+ xml_name :library
215
+ xml_reader :name, :from => 'NAME'
216
+ xml_reader :books, [BookWithContributions], :from => 'BOOK'
217
+ end
218
+
219
+ class LibraryWithBooksOfUnderivableName
220
+ include ROXML
221
+
222
+ xml :name, true
223
+ xml_reader :novels, NamelessBook, :as => :array
224
+ end
225
+
226
+ class NodeWithNameConflicts
227
+ include ROXML
228
+
229
+ xml_name :node
230
+ xml_reader :content
231
+ xml_reader :name
232
+ end
233
+
234
+ class NodeWithAttrNameConflicts
235
+ include ROXML
236
+
237
+ xml_name :node
238
+ xml_reader :content, :attr => :content
239
+ xml_reader :name, :attr => :name
240
+ end
241
+
242
+ class Person
243
+ include ROXML
244
+
245
+ xml_reader :age, :attr, :else => 21
246
+ xml_accessor :name, :content, :else => 'Unknown'
247
+ end
248
+
249
+ class PersonWithMother
250
+ include ROXML
251
+
252
+ xml_name :person
253
+ xml_reader :name
254
+ xml_reader :mother, PersonWithMother, :from => 'mother'
255
+ end
256
+
257
+ class PersonWithGuardedMother
258
+ include ROXML
259
+
260
+ xml_name :person
261
+ xml_reader :name
262
+ xml_reader :mother, PersonWithGuardedMother, :from => :person, :in => :mother
263
+ end
264
+
265
+ class PersonWithMotherOrMissing
266
+ include ROXML
267
+
268
+ xml_reader :age, :attr, :else => 21
269
+ xml_reader :name, :else => 'Anonymous'
270
+ xml_reader :mother, PersonWithMotherOrMissing, :else => Person.new
271
+ end
@@ -0,0 +1,32 @@
1
+ require 'test/unit'
2
+
3
+ class TestDependencies < Test::Unit::TestCase
4
+ def assert_dependency_included(*args)
5
+ # assert methods are removed
6
+ args.each do |type, method, source|
7
+ type.send(:remove_method, method) if type.instance_methods.include?(method.to_s)
8
+ assert !type.instance_methods.include?(method.to_s)
9
+ end
10
+
11
+ load File.join(File.dirname(__FILE__), '../../lib/roxml.rb')
12
+
13
+ # assert_instance_methods returned to their rightful positions
14
+ args.each do |type, method, source|
15
+ assert type.instance_methods.include?(method.to_s)
16
+ end
17
+
18
+ #assert ROXML has what it needs
19
+ assert_nothing_raised do
20
+ Class.new do
21
+ include ROXML
22
+
23
+ xml_reader :deps
24
+ end
25
+ end
26
+ end
27
+
28
+ def test_symbol_to_proc_is_added_by_roxml
29
+ assert_dependency_included([Symbol, :to_proc, 'active_support/core_ext/symbol.rb'],
30
+ [Enumerable, :one?, 'extensions/enumerable.rb'])
31
+ end
32
+ end
@@ -0,0 +1,44 @@
1
+ require "lib/roxml"
2
+ require 'test/mocks/mocks'
3
+ require 'test/mocks/dictionaries'
4
+
5
+ def fixture(name)
6
+ File.read(fixture_path(name))
7
+ end
8
+
9
+ def xml_fixture(name)
10
+ ROXML::XML::Parser.parse_file(fixture_path(name)).root
11
+ end
12
+
13
+ def fixture_path(name)
14
+ "test/fixtures/#{name}.xml"
15
+ end
16
+
17
+ def to_xml_test(*names)
18
+ names = names.only if names.one? && names.only.is_a?(Hash)
19
+ names.each do |name, xml_name|
20
+ xml_name ||= name
21
+
22
+ define_method "test_#{name}" do
23
+ klass = name.is_a?(Symbol) ? name.to_s.camelize.constantize : name
24
+ xml = xml_name.is_a?(Symbol) ? xml_fixture(xml_name) : xml_name
25
+
26
+ dict = klass.from_xml(xml)
27
+ xml = remove_children(xml)
28
+ assert_equal xml, dict.to_xml
29
+ end
30
+ end
31
+ end
32
+
33
+ def remove_children(xml)
34
+ xml = ROXML::XML::Parser.parse(xml).root if xml.is_a?(String)
35
+ return unless xml.respond_to? :children
36
+ xml.children.each do |child|
37
+ if child.blank?
38
+ child.remove!
39
+ else
40
+ remove_children(child)
41
+ end
42
+ end
43
+ xml
44
+ end