pauldix-sax-machine 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
data/README.textile
CHANGED
@@ -48,6 +48,15 @@ feed.entries.first.title # => title of the first entry
|
|
48
48
|
feed.entries.first.author # => the author of the first entry
|
49
49
|
feed.entries.first.url # => the permalink on the blog for this entry
|
50
50
|
# etc ...
|
51
|
+
|
52
|
+
# you can also use the elements method without specifying a class like so
|
53
|
+
class SomeServiceResponse
|
54
|
+
elements :message, :as => :messages
|
55
|
+
end
|
56
|
+
|
57
|
+
response = SomeServiceResponse.parse("<response><message>hi</message><message>world</message></response>")
|
58
|
+
response.messages.first # => "hi"
|
59
|
+
response.messages.last # => "world"
|
51
60
|
</pre>
|
52
61
|
|
53
62
|
h2. LICENSE
|
@@ -35,7 +35,16 @@ module SAXMachine
|
|
35
35
|
|
36
36
|
def elements(name, options = {})
|
37
37
|
options[:as] ||= name
|
38
|
-
|
38
|
+
if options[:class]
|
39
|
+
sax_config.add_collection_element(name, options)
|
40
|
+
else
|
41
|
+
class_eval <<-SRC
|
42
|
+
def add_#{options[:as]}(value)
|
43
|
+
#{options[:as]} << value
|
44
|
+
end
|
45
|
+
SRC
|
46
|
+
sax_config.add_top_level_element(name, options.merge(:collection => true))
|
47
|
+
end
|
39
48
|
|
40
49
|
class_eval <<-SRC
|
41
50
|
def #{options[:as]}
|
@@ -2,7 +2,7 @@ module SAXMachine
|
|
2
2
|
class SAXConfig
|
3
3
|
|
4
4
|
class ElementConfig
|
5
|
-
attr_reader :name
|
5
|
+
attr_reader :name, :setter
|
6
6
|
|
7
7
|
def initialize(name, options)
|
8
8
|
@name = name.to_s
|
@@ -21,16 +21,19 @@ module SAXMachine
|
|
21
21
|
end
|
22
22
|
|
23
23
|
@as = options[:as]
|
24
|
+
@collection = options[:collection]
|
25
|
+
|
26
|
+
if @collection
|
27
|
+
@setter = "add_#{options[:as]}"
|
28
|
+
else
|
29
|
+
@setter = "#{@as}="
|
30
|
+
end
|
24
31
|
end
|
25
32
|
|
26
33
|
def value_from_attrs(attrs)
|
27
34
|
attrs[attrs.index(@value) + 1]
|
28
35
|
end
|
29
36
|
|
30
|
-
def setter
|
31
|
-
"#{@as}="
|
32
|
-
end
|
33
|
-
|
34
37
|
def attrs_match?(attrs)
|
35
38
|
if @with
|
36
39
|
@with == (@with & attrs)
|
@@ -42,6 +45,10 @@ module SAXMachine
|
|
42
45
|
def has_value_and_attrs_match?(attrs)
|
43
46
|
!@value.nil? && attrs_match?(attrs)
|
44
47
|
end
|
48
|
+
|
49
|
+
def collection?
|
50
|
+
@collection
|
51
|
+
end
|
45
52
|
end
|
46
53
|
|
47
54
|
end
|
@@ -214,32 +214,30 @@ describe "SAXMachine" do
|
|
214
214
|
end
|
215
215
|
|
216
216
|
describe "elements" do
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
# end
|
242
|
-
# end
|
217
|
+
describe "when parsing multiple elements" do
|
218
|
+
before :each do
|
219
|
+
@klass = Class.new do
|
220
|
+
include SAXMachine
|
221
|
+
elements :entry, :as => :entries
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should provide a collection accessor" do
|
226
|
+
document = @klass.new
|
227
|
+
document.entries << :foo
|
228
|
+
document.entries.should == [:foo]
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should parse a single element" do
|
232
|
+
document = @klass.parse("<entry>hello</entry>")
|
233
|
+
document.entries.should == ["hello"]
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should parse multiple elements" do
|
237
|
+
document = @klass.parse("<xml><entry>hello</entry><entry>world</entry></xml>")
|
238
|
+
document.entries.should == ["hello", "world"]
|
239
|
+
end
|
240
|
+
end
|
243
241
|
|
244
242
|
describe "when using the class option" do
|
245
243
|
before :each do
|