roxml 3.1.2 → 3.1.3
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.
- data/History.txt +14 -0
- data/README.rdoc +3 -1
- data/TODO +2 -0
- data/VERSION +1 -1
- data/lib/roxml.rb +2 -1
- data/lib/roxml/definition.rb +11 -11
- data/lib/roxml/xml/references.rb +64 -52
- data/roxml.gemspec +6 -3
- data/spec/definition_spec.rb +7 -7
- data/spec/xml/attributes_spec.rb +45 -10
- data/spec/xml/namespace_spec.rb +1 -0
- data/spec/xml/namespaces_spec.rb +8 -1
- data/spec/xml/object_spec.rb +82 -0
- data/spec/xml/text_spec.rb +71 -0
- data/test/unit/definition_test.rb +15 -15
- data/test/unit/deprecations_test.rb +1 -1
- data/test/unit/xml_hash_test.rb +1 -1
- metadata +6 -3
- data/config/website.yml +0 -2
data/History.txt
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
== 3.1.3 (October 30, 2009)
|
2
|
+
|
3
|
+
* minor enhancements
|
4
|
+
|
5
|
+
* Add support for local-name() reference reading via :namespace => '*'
|
6
|
+
(ala http://techrageo.us/2008/11/01/wildcard-namespaces-in-xpath/)
|
7
|
+
|
8
|
+
* bug fixes
|
9
|
+
|
10
|
+
* attributes collected :as => [] were previously output improperly,
|
11
|
+
as a single attribute on a single node
|
12
|
+
* don't output crazy, context-less namespaces and local-name xpaths
|
13
|
+
when trying to output namespaced attributes
|
14
|
+
|
1
15
|
== 3.1.2 (October 18, 2009)
|
2
16
|
|
3
17
|
* minor enhancements
|
data/README.rdoc
CHANGED
@@ -95,7 +95,9 @@ in this case.
|
|
95
95
|
|
96
96
|
=== Namespace Support
|
97
97
|
|
98
|
-
|
98
|
+
Namespaced nodes are supported via the xml_namespace and xml_namespaces declarations and the :from and :namespace attr options. See spec/xml/namespace_spec.rb for usage.
|
99
|
+
|
100
|
+
Note that ROXML does not currently support outputting namespaced nodes. This is planned for a future version.
|
99
101
|
|
100
102
|
== Manipulation
|
101
103
|
|
data/TODO
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.1.
|
1
|
+
3.1.3
|
data/lib/roxml.rb
CHANGED
@@ -5,7 +5,7 @@ require 'roxml/definition'
|
|
5
5
|
require 'roxml/xml'
|
6
6
|
|
7
7
|
module ROXML # :nodoc:
|
8
|
-
VERSION = '3.1.
|
8
|
+
VERSION = '3.1.3'
|
9
9
|
|
10
10
|
def self.included(base) # :nodoc:
|
11
11
|
base.class_eval do
|
@@ -22,6 +22,7 @@ module ROXML # :nodoc:
|
|
22
22
|
# Returns an XML object representing this object
|
23
23
|
def to_xml(params = {})
|
24
24
|
params.reverse_merge!(:name => self.class.tag_name, :namespace => self.class.roxml_namespace)
|
25
|
+
params[:namespace] = nil if ['*', 'xmlns'].include?(params[:namespace])
|
25
26
|
XML::Node.create([params[:namespace], params[:name]].compact.join(':')).tap do |root|
|
26
27
|
refs = (self.roxml_references.present? \
|
27
28
|
? self.roxml_references \
|
data/lib/roxml/definition.rb
CHANGED
@@ -15,7 +15,7 @@ module ROXML
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class Definition # :nodoc:
|
18
|
-
attr_reader :name, :
|
18
|
+
attr_reader :name, :sought_type, :wrapper, :hash, :blocks, :accessor, :to_xml, :attr_name, :namespace
|
19
19
|
bool_attr_reader :name_explicit, :array, :cdata, :required, :frozen
|
20
20
|
|
21
21
|
def initialize(sym, opts = {}, &block)
|
@@ -43,9 +43,9 @@ module ROXML
|
|
43
43
|
@array = opts[:as].is_a?(Array)
|
44
44
|
@blocks = collect_blocks(block, opts[:as])
|
45
45
|
|
46
|
-
@
|
47
|
-
if @
|
48
|
-
opts[:from] ||= @
|
46
|
+
@sought_type = extract_type(opts[:as])
|
47
|
+
if @sought_type.respond_to?(:roxml_tag_name)
|
48
|
+
opts[:from] ||= @sought_type.roxml_tag_name
|
49
49
|
end
|
50
50
|
|
51
51
|
if opts[:from] == :content
|
@@ -53,12 +53,12 @@ module ROXML
|
|
53
53
|
elsif opts[:from] == :name
|
54
54
|
opts[:from] = '*'
|
55
55
|
elsif opts[:from] == :attr
|
56
|
-
@
|
56
|
+
@sought_type = :attr
|
57
57
|
opts[:from] = nil
|
58
58
|
elsif opts[:from] == :name
|
59
59
|
opts[:from] = '*'
|
60
60
|
elsif opts[:from].to_s.starts_with?('@')
|
61
|
-
@
|
61
|
+
@sought_type = :attr
|
62
62
|
opts[:from].sub!('@', '')
|
63
63
|
end
|
64
64
|
|
@@ -83,13 +83,13 @@ module ROXML
|
|
83
83
|
|
84
84
|
def hash
|
85
85
|
if hash?
|
86
|
-
@
|
87
|
-
@
|
86
|
+
@sought_type.wrapper ||= name
|
87
|
+
@sought_type
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
91
|
def hash?
|
92
|
-
@
|
92
|
+
@sought_type.is_a?(HashDefinition)
|
93
93
|
end
|
94
94
|
|
95
95
|
def name?
|
@@ -109,11 +109,11 @@ module ROXML
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def to_ref(inst)
|
112
|
-
case
|
112
|
+
case sought_type
|
113
113
|
when :attr then XMLAttributeRef
|
114
114
|
when :text then XMLTextRef
|
115
115
|
when HashDefinition then XMLHashRef
|
116
|
-
when Symbol then raise ArgumentError, "Invalid type argument #{
|
116
|
+
when Symbol then raise ArgumentError, "Invalid type argument #{sought_type}"
|
117
117
|
else XMLObjectRef
|
118
118
|
end.new(self, inst)
|
119
119
|
end
|
data/lib/roxml/xml/references.rb
CHANGED
@@ -7,24 +7,22 @@ module ROXML
|
|
7
7
|
#
|
8
8
|
class XMLRef # :nodoc:
|
9
9
|
attr_reader :opts
|
10
|
-
delegate :required?, :array?, :
|
10
|
+
delegate :required?, :array?, :accessor, :default, :wrapper, :to => :opts
|
11
11
|
|
12
12
|
def initialize(opts, instance)
|
13
13
|
@opts = opts
|
14
14
|
@instance = instance
|
15
15
|
end
|
16
16
|
|
17
|
+
def blocks
|
18
|
+
opts.blocks || []
|
19
|
+
end
|
20
|
+
|
17
21
|
def to_xml(instance)
|
18
22
|
val = instance.__send__(accessor)
|
19
23
|
opts.to_xml.respond_to?(:call) ? opts.to_xml.call(val) : val
|
20
24
|
end
|
21
25
|
|
22
|
-
def update_xml(xml, value)
|
23
|
-
returning wrap(xml) do |xml|
|
24
|
-
write_xml(xml, value)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
26
|
def name
|
29
27
|
opts.name_explicit? ? opts.name : conventionize(opts.name)
|
30
28
|
end
|
@@ -52,14 +50,12 @@ module ROXML
|
|
52
50
|
end
|
53
51
|
|
54
52
|
def namespacify(what)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
"#{namespace}:#{what}"
|
60
|
-
else
|
61
|
-
what
|
53
|
+
if what.to_s.present? && !what.to_s.include?(':') && opts.namespace != false
|
54
|
+
[opts.namespace, @instance.class.roxml_namespace, @default_namespace].each do |namespace|
|
55
|
+
return opts.namespace == '*' ? "*[local-name()='#{what}']" : "#{namespace}:#{what}" if namespace
|
56
|
+
end
|
62
57
|
end
|
58
|
+
what
|
63
59
|
end
|
64
60
|
|
65
61
|
def apply_blocks(val)
|
@@ -95,11 +91,11 @@ module ROXML
|
|
95
91
|
array?
|
96
92
|
end
|
97
93
|
|
98
|
-
def wrap(xml)
|
94
|
+
def wrap(xml, opts = {:always_create => false})
|
99
95
|
wrap_with = @auto_vals ? auto_wrapper : wrapper
|
100
96
|
|
101
97
|
return xml if !wrap_with || xml.name == wrap_with
|
102
|
-
if child = xml.children.find {|c| c.name == wrap_with }
|
98
|
+
if !opts[:always_create] && (child = xml.children.find {|c| c.name == wrap_with })
|
103
99
|
return child
|
104
100
|
end
|
105
101
|
xml.add_child(XML::Node.create(wrap_with.to_s))
|
@@ -134,13 +130,23 @@ module ROXML
|
|
134
130
|
# XMLTextRef
|
135
131
|
# </element>
|
136
132
|
class XMLAttributeRef < XMLRef # :nodoc:
|
137
|
-
private
|
138
133
|
# Updates the attribute in the given XML block to
|
139
134
|
# the value provided.
|
140
|
-
def
|
141
|
-
|
135
|
+
def update_xml(xml, values)
|
136
|
+
if array?
|
137
|
+
values.each do |value|
|
138
|
+
wrap(xml, :always_create => true).tap do |node|
|
139
|
+
node.attributes[name] = value.to_s
|
140
|
+
end
|
141
|
+
end
|
142
|
+
else
|
143
|
+
wrap(xml).tap do |xml|
|
144
|
+
xml.attributes[name] = values.to_s
|
145
|
+
end
|
146
|
+
end
|
142
147
|
end
|
143
148
|
|
149
|
+
private
|
144
150
|
def fetch_value(xml)
|
145
151
|
nodes_in(xml) do |node|
|
146
152
|
node.value
|
@@ -161,23 +167,25 @@ module ROXML
|
|
161
167
|
class XMLTextRef < XMLRef # :nodoc:
|
162
168
|
delegate :cdata?, :content?, :name?, :to => :opts
|
163
169
|
|
164
|
-
private
|
165
170
|
# Updates the text in the given _xml_ block to
|
166
171
|
# the _value_ provided.
|
167
|
-
def
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
172
|
+
def update_xml(xml, value)
|
173
|
+
wrap(xml).tap do |xml|
|
174
|
+
if content?
|
175
|
+
add(xml, value)
|
176
|
+
elsif name?
|
177
|
+
xml.name = value
|
178
|
+
elsif array?
|
179
|
+
value.each do |v|
|
180
|
+
add(xml.add_child(XML::Node.create(name)), v)
|
181
|
+
end
|
182
|
+
else
|
183
|
+
add(xml.add_child(XML::Node.create(name)), value)
|
175
184
|
end
|
176
|
-
else
|
177
|
-
add(xml.add_child(XML::Node.create(xpath_name)), value)
|
178
185
|
end
|
179
186
|
end
|
180
187
|
|
188
|
+
private
|
181
189
|
def fetch_value(xml)
|
182
190
|
if content? || name?
|
183
191
|
value =
|
@@ -222,17 +230,19 @@ module ROXML
|
|
222
230
|
true
|
223
231
|
end
|
224
232
|
|
225
|
-
private
|
226
233
|
# Updates the composed XML object in the given XML block to
|
227
234
|
# the value provided.
|
228
|
-
def
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
235
|
+
def update_xml(xml, value)
|
236
|
+
wrap(xml).tap do |xml|
|
237
|
+
value.each_pair do |k, v|
|
238
|
+
node = xml.add_child(XML::Node.create(hash.wrapper))
|
239
|
+
@key.update_xml(node, k)
|
240
|
+
@value.update_xml(node, v)
|
241
|
+
end
|
233
242
|
end
|
234
243
|
end
|
235
244
|
|
245
|
+
private
|
236
246
|
def fetch_value(xml)
|
237
247
|
nodes_in(xml) do |node|
|
238
248
|
[@key.value_in(node), @value.value_in(node)]
|
@@ -270,32 +280,34 @@ module ROXML
|
|
270
280
|
end
|
271
281
|
|
272
282
|
class XMLObjectRef < XMLTextRef # :nodoc:
|
273
|
-
delegate :
|
283
|
+
delegate :sought_type, :to => :opts
|
274
284
|
|
275
|
-
private
|
276
285
|
# Updates the composed XML object in the given XML block to
|
277
286
|
# the value provided.
|
278
|
-
def
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
287
|
+
def update_xml(xml, value)
|
288
|
+
wrap(xml).tap do |xml|
|
289
|
+
params = {:name => name, :namespace => opts.namespace}
|
290
|
+
if array?
|
291
|
+
value.each do |v|
|
292
|
+
xml.add_child(v.to_xml(params))
|
293
|
+
end
|
294
|
+
elsif value.is_a?(ROXML)
|
295
|
+
xml.add_child(value.to_xml(params))
|
296
|
+
else
|
297
|
+
node = XML::Node.create(name)
|
298
|
+
node.content = value.to_xml
|
299
|
+
xml.add_child(node)
|
283
300
|
end
|
284
|
-
elsif value.is_a?(ROXML)
|
285
|
-
xml.add_child(value.to_xml(params))
|
286
|
-
else
|
287
|
-
node = XML::Node.create(xpath_name)
|
288
|
-
node.content = value.to_xml
|
289
|
-
xml.add_child(node)
|
290
301
|
end
|
291
302
|
end
|
292
303
|
|
304
|
+
private
|
293
305
|
def fetch_value(xml)
|
294
306
|
nodes_in(xml) do |node|
|
295
|
-
if
|
296
|
-
|
307
|
+
if sought_type.respond_to? :from_xml
|
308
|
+
sought_type.from_xml(node)
|
297
309
|
else
|
298
|
-
|
310
|
+
sought_type.new(node)
|
299
311
|
end
|
300
312
|
end
|
301
313
|
end
|
data/roxml.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{roxml}
|
8
|
-
s.version = "3.1.
|
8
|
+
s.version = "3.1.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ben Woosley", "Zak Mandhro", "Anders Engstrom", "Russ Olsen"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-30}
|
13
13
|
s.description = %q{ROXML is a Ruby library designed to make it easier for Ruby developers to work with XML.
|
14
14
|
Using simple annotations, it enables Ruby classes to be mapped to XML. ROXML takes care
|
15
15
|
of the marshalling and unmarshalling of mapped attributes so that developers can focus on
|
@@ -30,7 +30,6 @@ RESTful applications, Web Services, and XML-RPC.
|
|
30
30
|
"Rakefile",
|
31
31
|
"TODO",
|
32
32
|
"VERSION",
|
33
|
-
"config/website.yml",
|
34
33
|
"examples/amazon.rb",
|
35
34
|
"examples/current_weather.rb",
|
36
35
|
"examples/dashed_elements.rb",
|
@@ -69,7 +68,9 @@ RESTful applications, Web Services, and XML-RPC.
|
|
69
68
|
"spec/xml/attributes_spec.rb",
|
70
69
|
"spec/xml/namespace_spec.rb",
|
71
70
|
"spec/xml/namespaces_spec.rb",
|
71
|
+
"spec/xml/object_spec.rb",
|
72
72
|
"spec/xml/parser_spec.rb",
|
73
|
+
"spec/xml/text_spec.rb",
|
73
74
|
"tasks/rdoc.rake",
|
74
75
|
"tasks/rspec.rake",
|
75
76
|
"tasks/test.rake",
|
@@ -147,7 +148,9 @@ RESTful applications, Web Services, and XML-RPC.
|
|
147
148
|
"spec/xml/attributes_spec.rb",
|
148
149
|
"spec/xml/namespace_spec.rb",
|
149
150
|
"spec/xml/namespaces_spec.rb",
|
151
|
+
"spec/xml/object_spec.rb",
|
150
152
|
"spec/xml/parser_spec.rb",
|
153
|
+
"spec/xml/text_spec.rb",
|
151
154
|
"test/load_test.rb",
|
152
155
|
"test/mocks/dictionaries.rb",
|
153
156
|
"test/mocks/mocks.rb",
|
data/spec/definition_spec.rb
CHANGED
@@ -87,7 +87,7 @@ describe ROXML::Definition do
|
|
87
87
|
it "should means array of texts" do
|
88
88
|
opts = ROXML::Definition.new(:authors, :as => [])
|
89
89
|
opts.array?.should be_true
|
90
|
-
opts.
|
90
|
+
opts.sought_type.should == :text
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
@@ -98,7 +98,7 @@ describe ROXML::Definition do
|
|
98
98
|
|
99
99
|
it "should store type" do
|
100
100
|
opts = ROXML::Definition.new(:name, :as => RoxmlClass)
|
101
|
-
opts.
|
101
|
+
opts.sought_type.should == RoxmlClass
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
@@ -111,7 +111,7 @@ describe ROXML::Definition do
|
|
111
111
|
|
112
112
|
it "should accept type" do
|
113
113
|
opts = ROXML::Definition.new(:name, :as => OctalInteger)
|
114
|
-
opts.
|
114
|
+
opts.sought_type.should == OctalInteger
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
@@ -134,8 +134,8 @@ describe ROXML::Definition do
|
|
134
134
|
end
|
135
135
|
|
136
136
|
it "should have hash definition" do
|
137
|
-
{@opts.hash.key.
|
138
|
-
{@opts.hash.value.
|
137
|
+
{@opts.hash.key.sought_type => @opts.hash.key.name}.should == @hash_args[:key]
|
138
|
+
{@opts.hash.value.sought_type => @opts.hash.value.name}.should == @hash_args[:value]
|
139
139
|
end
|
140
140
|
|
141
141
|
it "should not represent an array" do
|
@@ -196,7 +196,7 @@ describe ROXML::Definition do
|
|
196
196
|
end
|
197
197
|
|
198
198
|
it "should be normal otherwise" do
|
199
|
-
@opts.
|
199
|
+
@opts.sought_type.should == :text
|
200
200
|
@opts.blocks.size.should == 1
|
201
201
|
end
|
202
202
|
end
|
@@ -392,7 +392,7 @@ describe ROXML::Definition do
|
|
392
392
|
describe ":from" do
|
393
393
|
describe "attribute reference", :shared => true do
|
394
394
|
it "should be interpreted as :attr" do
|
395
|
-
@opts.
|
395
|
+
@opts.sought_type.should == :attr
|
396
396
|
end
|
397
397
|
|
398
398
|
it "should strip '@' from name" do
|
data/spec/xml/attributes_spec.rb
CHANGED
@@ -3,21 +3,20 @@ require 'spec/spec_helper'
|
|
3
3
|
describe ROXML::XMLAttributeRef do
|
4
4
|
before do
|
5
5
|
@xml = ROXML::XML::Parser.parse %(
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
)
|
6
|
+
<myxml>
|
7
|
+
<node name="first" />
|
8
|
+
<node name="second" />
|
9
|
+
<node name="third" />
|
10
|
+
</myxml>)
|
12
11
|
end
|
13
|
-
|
12
|
+
|
14
13
|
context "plain vanilla" do
|
15
14
|
before do
|
16
15
|
@ref = ROXML::XMLAttributeRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => false), RoxmlObject.new)
|
17
16
|
end
|
18
17
|
|
19
18
|
it "should return one instance" do
|
20
|
-
@ref.
|
19
|
+
@ref.value_in(@xml).should == "first"
|
21
20
|
end
|
22
21
|
it "should output one instance"
|
23
22
|
end
|
@@ -28,9 +27,45 @@ describe ROXML::XMLAttributeRef do
|
|
28
27
|
end
|
29
28
|
|
30
29
|
it "should collect all instances" do
|
31
|
-
@ref.
|
30
|
+
@ref.value_in(@xml).should == ["first", "second", "third"]
|
32
31
|
end
|
33
32
|
|
34
|
-
it "should output all instances"
|
33
|
+
it "should output all instances" do
|
34
|
+
xml = ROXML::XML::Node.create('myxml')
|
35
|
+
@ref.update_xml(xml, ["first", "second", "third"])
|
36
|
+
xml.to_s.squeeze(' ').should == @xml.root.to_s.squeeze(' ')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when the namespaces are different" do
|
41
|
+
before do
|
42
|
+
@xml = ROXML::XML::Parser.parse %(
|
43
|
+
<document>
|
44
|
+
<myxml xmlns="http://example.com/three" xmlns:one="http://example.com/one" xmlns:two="http://example.com/two">
|
45
|
+
<one:node name="first" />
|
46
|
+
<two:node name="second" />
|
47
|
+
<node name="third" />
|
48
|
+
</myxml>
|
49
|
+
</document>
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
context "with :namespace => '*'" do
|
54
|
+
before do
|
55
|
+
@ref = ROXML::XMLAttributeRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :namespace => '*'), RoxmlObject.new)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should collect all instances" do
|
59
|
+
pending "Test bug?"
|
60
|
+
@ref.value_in(@xml).should == ["first", "second", "third"]
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should output all instances with namespaces" do
|
64
|
+
pending "Full namespace write support"
|
65
|
+
xml = ROXML::XML::Node.create('result')
|
66
|
+
@ref.update_xml(xml, ["first", "second", "third"])
|
67
|
+
xml.should == @xml.root
|
68
|
+
end
|
69
|
+
end
|
35
70
|
end
|
36
71
|
end
|
data/spec/xml/namespace_spec.rb
CHANGED
data/spec/xml/namespaces_spec.rb
CHANGED
@@ -11,6 +11,7 @@ describe ROXML, "#xml_namespaces" do
|
|
11
11
|
|
12
12
|
xml_reader :bike_tires, :as => [], :from => '@name', :in => 'bobsbike:tire'
|
13
13
|
xml_reader :car_tires, :as => [], :from => '@name', :in => 'alicesauto:tire'
|
14
|
+
xml_reader :tires, :as => [], :from => '@name', :in => 'tire', :namespace => '*'
|
14
15
|
end
|
15
16
|
|
16
17
|
before do
|
@@ -22,7 +23,7 @@ describe ROXML, "#xml_namespaces" do
|
|
22
23
|
</inventory>
|
23
24
|
}
|
24
25
|
end
|
25
|
-
|
26
|
+
|
26
27
|
it "should remap default namespaces" do
|
27
28
|
Tires.from_xml(@xml).car_tires.should =~ ['super slick racing tire', 'all weather tire']
|
28
29
|
end
|
@@ -30,6 +31,12 @@ describe ROXML, "#xml_namespaces" do
|
|
30
31
|
it "should remap prefix namespaces" do
|
31
32
|
Tires.from_xml(@xml).bike_tires.should == ['skinny street']
|
32
33
|
end
|
34
|
+
|
35
|
+
context "with namespace-indifferent option" do
|
36
|
+
it "should return all tires" do
|
37
|
+
Tires.from_xml(@xml).tires.should =~ ['super slick racing tire', 'all weather tire', 'skinny street']
|
38
|
+
end
|
39
|
+
end
|
33
40
|
end
|
34
41
|
|
35
42
|
context "when an included namespace is not defined in the xml" do
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe ROXML::XMLObjectRef do
|
4
|
+
class SubObject
|
5
|
+
include ROXML
|
6
|
+
|
7
|
+
xml_reader :value, :from => :attr
|
8
|
+
|
9
|
+
def initialize(value = nil)
|
10
|
+
@value = value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
before do
|
15
|
+
@xml = ROXML::XML::Parser.parse %(
|
16
|
+
<myxml>
|
17
|
+
<node>
|
18
|
+
<name value="first" />
|
19
|
+
<name value="second" />
|
20
|
+
<name value="third" />
|
21
|
+
</node>
|
22
|
+
</myxml>)
|
23
|
+
end
|
24
|
+
|
25
|
+
context "plain vanilla" do
|
26
|
+
before do
|
27
|
+
@ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => false, :sought_type => SubObject), RoxmlObject.new)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return one instance" do
|
31
|
+
@ref.value_in(@xml).value.should == "first"
|
32
|
+
end
|
33
|
+
it "should output one instance"
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with :as => []" do
|
37
|
+
before do
|
38
|
+
@ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :sought_type => SubObject), RoxmlObject.new)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should collect all instances" do
|
42
|
+
@ref.value_in(@xml).map(&:value).should == ["first", "second", "third"]
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should output all instances" do
|
46
|
+
xml = ROXML::XML::Node.create('myxml')
|
47
|
+
@ref.update_xml(xml, ["first", "second", "third"].map {|value| SubObject.new(value) })
|
48
|
+
xml.to_s.squeeze(' ').should == @xml.root.to_s.squeeze(' ')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when the namespaces are different" do
|
53
|
+
before do
|
54
|
+
@xml = ROXML::XML::Parser.parse %(
|
55
|
+
<myxml xmlns="http://example.com/three" xmlns:one="http://example.com/one" xmlns:two="http://example.com/two">
|
56
|
+
<node>
|
57
|
+
<one:name>first</one:name>
|
58
|
+
<two:name>second</two:name>
|
59
|
+
<name>third</name>
|
60
|
+
</node>
|
61
|
+
</myxml>)
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with :namespace => '*'" do
|
65
|
+
before do
|
66
|
+
@ref = ROXML::XMLObjectRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :namespace => '*', :sought_type => SubObject), RoxmlObject.new)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should collect all instances" do
|
70
|
+
pending "Test bug?"
|
71
|
+
@ref.value_in(@xml).map(&:value).should == ["first", "second", "third"]
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should output all instances with namespaces" do
|
75
|
+
pending "Full namespace write support"
|
76
|
+
xml = ROXML::XML::Node.create('myxml')
|
77
|
+
@ref.update_xml(xml, ["first", "second", "third"].map {|value| SubObject.new(value) })
|
78
|
+
xml.should == @xml.root
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe ROXML::XMLTextRef do
|
4
|
+
before do
|
5
|
+
@xml = ROXML::XML::Parser.parse %(
|
6
|
+
<myxml>
|
7
|
+
<node>
|
8
|
+
<name>first</name>
|
9
|
+
<name>second</name>
|
10
|
+
<name>third</name>
|
11
|
+
</node>
|
12
|
+
</myxml>)
|
13
|
+
end
|
14
|
+
|
15
|
+
context "plain vanilla" do
|
16
|
+
before do
|
17
|
+
@ref = ROXML::XMLTextRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => false), RoxmlObject.new)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return one instance" do
|
21
|
+
@ref.value_in(@xml).should == "first"
|
22
|
+
end
|
23
|
+
it "should output one instance"
|
24
|
+
end
|
25
|
+
|
26
|
+
context "with :as => []" do
|
27
|
+
before do
|
28
|
+
@ref = ROXML::XMLTextRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true), RoxmlObject.new)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should collect all instances" do
|
32
|
+
@ref.value_in(@xml).should == ["first", "second", "third"]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should output all instances" do
|
36
|
+
xml = ROXML::XML::Node.create('myxml')
|
37
|
+
@ref.update_xml(xml, ["first", "second", "third"])
|
38
|
+
xml.to_s.squeeze(' ').should == @xml.root.to_s.squeeze(' ')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when the namespaces are different" do
|
43
|
+
before do
|
44
|
+
@xml = ROXML::XML::Parser.parse %(
|
45
|
+
<myxml xmlns="http://example.com/three" xmlns:one="http://example.com/one" xmlns:two="http://example.com/two">
|
46
|
+
<node>
|
47
|
+
<one:name>first</one:name>
|
48
|
+
<two:name>second</two:name>
|
49
|
+
<name>third</name>
|
50
|
+
</node>
|
51
|
+
</myxml>)
|
52
|
+
end
|
53
|
+
|
54
|
+
context "with :namespace => '*'" do
|
55
|
+
before do
|
56
|
+
@ref = ROXML::XMLTextRef.new(OpenStruct.new(:name => 'name', :wrapper => 'node', :array? => true, :namespace => '*'), RoxmlObject.new)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should collect all instances" do
|
60
|
+
@ref.value_in(@xml).should == ["first", "second", "third"]
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should output all instances with namespaces" do
|
64
|
+
pending "Full namespace write support"
|
65
|
+
xml = ROXML::XML::Node.create('myxml')
|
66
|
+
@ref.update_xml(xml, ["first", "second", "third"])
|
67
|
+
xml.should == @xml.root
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -4,26 +4,26 @@ class TestDefinition < ActiveSupport::TestCase
|
|
4
4
|
def assert_hash(opts, kvp)
|
5
5
|
assert opts.hash?
|
6
6
|
assert !opts.array?
|
7
|
-
assert_equal kvp, {opts.hash.key.
|
8
|
-
opts.hash.value.
|
7
|
+
assert_equal kvp, {opts.hash.key.sought_type => opts.hash.key.name,
|
8
|
+
opts.hash.value.sought_type => opts.hash.value.name}
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_empty_array_means_as_array_for_text
|
12
12
|
opts = ROXML::Definition.new(:authors, :as => [])
|
13
13
|
assert opts.array?
|
14
|
-
assert_equal :text, opts.
|
14
|
+
assert_equal :text, opts.sought_type
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_attr_in_array_means_as_array_for_attr
|
18
18
|
opts = ROXML::Definition.new(:authors, :as => [], :from => :attr)
|
19
19
|
assert opts.array?
|
20
|
-
assert_equal :attr, opts.
|
20
|
+
assert_equal :attr, opts.sought_type
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_block_shorthand_in_array_means_array
|
24
24
|
opts = ROXML::Definition.new(:intarray, :as => [Integer])
|
25
25
|
assert opts.array?
|
26
|
-
assert_equal :text, opts.
|
26
|
+
assert_equal :text, opts.sought_type
|
27
27
|
assert 1, opts.blocks.size
|
28
28
|
end
|
29
29
|
|
@@ -87,13 +87,13 @@ class TestDefinition < ActiveSupport::TestCase
|
|
87
87
|
def test_from_attr_is_supported
|
88
88
|
opts = ROXML::Definition.new(:count, :from => :attr)
|
89
89
|
assert_equal "count", opts.name
|
90
|
-
assert_equal :attr, opts.
|
90
|
+
assert_equal :attr, opts.sought_type
|
91
91
|
end
|
92
92
|
|
93
93
|
def test_from_at_name_is_supported
|
94
94
|
opts = ROXML::Definition.new(:count, :from => "@COUNT")
|
95
95
|
assert_equal "COUNT", opts.name
|
96
|
-
assert_equal :attr, opts.
|
96
|
+
assert_equal :attr, opts.sought_type
|
97
97
|
end
|
98
98
|
|
99
99
|
def test_multiple_shorthands_raises
|
@@ -164,11 +164,11 @@ class TestDefinition < ActiveSupport::TestCase
|
|
164
164
|
end
|
165
165
|
|
166
166
|
def test_as_supports_generic_roxml_types
|
167
|
-
assert_equal RoxmlObject, ROXML::Definition.new(:type, :as => RoxmlObject).
|
167
|
+
assert_equal RoxmlObject, ROXML::Definition.new(:type, :as => RoxmlObject).sought_type
|
168
168
|
end
|
169
169
|
|
170
170
|
def test_as_supports_generic_roxml_types_in_arrays
|
171
|
-
assert_equal RoxmlObject, ROXML::Definition.new(:types, :as => [RoxmlObject]).
|
171
|
+
assert_equal RoxmlObject, ROXML::Definition.new(:types, :as => [RoxmlObject]).sought_type
|
172
172
|
end
|
173
173
|
|
174
174
|
def test_default_works
|
@@ -195,24 +195,24 @@ class TestDefinition < ActiveSupport::TestCase
|
|
195
195
|
opts = ROXML::Definition.new(:author, :from => :content)
|
196
196
|
assert opts.content?
|
197
197
|
assert_equal '.', opts.name
|
198
|
-
assert_equal :text, opts.
|
198
|
+
assert_equal :text, opts.sought_type
|
199
199
|
end
|
200
200
|
|
201
201
|
def test_content_symbol_as_target_is_translated_to_string
|
202
202
|
opts = ROXML::Definition.new(:content, :from => :attr)
|
203
203
|
assert_equal 'content', opts.name
|
204
|
-
assert_equal :attr, opts.
|
204
|
+
assert_equal :attr, opts.sought_type
|
205
205
|
end
|
206
206
|
|
207
207
|
def test_attr_is_accepted_as_from
|
208
|
-
assert_equal :attr, ROXML::Definition.new(:author, :from => :attr).
|
209
|
-
assert_equal :attr, ROXML::Definition.new(:author, :from => '@author').
|
208
|
+
assert_equal :attr, ROXML::Definition.new(:author, :from => :attr).sought_type
|
209
|
+
assert_equal :attr, ROXML::Definition.new(:author, :from => '@author').sought_type
|
210
210
|
end
|
211
211
|
|
212
212
|
def test_attr_is_a_recognized_type
|
213
213
|
opts = ROXML::Definition.new(:author, :from => :attr)
|
214
214
|
assert_equal 'author', opts.name
|
215
|
-
assert_equal :attr, opts.
|
215
|
+
assert_equal :attr, opts.sought_type
|
216
216
|
end
|
217
217
|
end
|
218
218
|
|
@@ -230,6 +230,6 @@ class HashDefinitionTest < ActiveSupport::TestCase
|
|
230
230
|
def test_content_detected_as_from
|
231
231
|
opts = ROXML::Definition.new(:hash, :as => {:key => :content, :value => :name})
|
232
232
|
assert_equal '.', opts.hash.key.name
|
233
|
-
assert_equal :text, opts.hash.key.
|
233
|
+
assert_equal :text, opts.hash.key.sought_type
|
234
234
|
end
|
235
235
|
end
|
data/test/unit/xml_hash_test.rb
CHANGED
@@ -108,7 +108,7 @@ class TestXMLHash < ActiveSupport::TestCase
|
|
108
108
|
assert_not_deprecated do
|
109
109
|
opts = ROXML::Definition.new(:name, :as => {:key => :name, :value => {:from => 'value', :as => OctalInteger}})
|
110
110
|
assert opts.hash?
|
111
|
-
assert_equal OctalInteger, opts.hash.value.
|
111
|
+
assert_equal OctalInteger, opts.hash.value.sought_type
|
112
112
|
assert_equal 'value', opts.hash.value.name
|
113
113
|
end
|
114
114
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roxml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.1.
|
4
|
+
version: 3.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Woosley
|
@@ -12,7 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2009-10-
|
15
|
+
date: 2009-10-30 00:00:00 -04:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
@@ -89,7 +89,6 @@ files:
|
|
89
89
|
- Rakefile
|
90
90
|
- TODO
|
91
91
|
- VERSION
|
92
|
-
- config/website.yml
|
93
92
|
- examples/amazon.rb
|
94
93
|
- examples/current_weather.rb
|
95
94
|
- examples/dashed_elements.rb
|
@@ -128,7 +127,9 @@ files:
|
|
128
127
|
- spec/xml/attributes_spec.rb
|
129
128
|
- spec/xml/namespace_spec.rb
|
130
129
|
- spec/xml/namespaces_spec.rb
|
130
|
+
- spec/xml/object_spec.rb
|
131
131
|
- spec/xml/parser_spec.rb
|
132
|
+
- spec/xml/text_spec.rb
|
132
133
|
- tasks/rdoc.rake
|
133
134
|
- tasks/rspec.rake
|
134
135
|
- tasks/test.rake
|
@@ -227,7 +228,9 @@ test_files:
|
|
227
228
|
- spec/xml/attributes_spec.rb
|
228
229
|
- spec/xml/namespace_spec.rb
|
229
230
|
- spec/xml/namespaces_spec.rb
|
231
|
+
- spec/xml/object_spec.rb
|
230
232
|
- spec/xml/parser_spec.rb
|
233
|
+
- spec/xml/text_spec.rb
|
231
234
|
- test/load_test.rb
|
232
235
|
- test/mocks/dictionaries.rb
|
233
236
|
- test/mocks/mocks.rb
|
data/config/website.yml
DELETED