pauldix-sax-machine 0.0.7 → 0.0.8

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/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
- sax_config.add_collection_element(name, options)
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
@@ -74,7 +74,7 @@ module SAXMachine
74
74
  end
75
75
 
76
76
  def mark_as_parsed
77
- @parsed_configs[@element_config] = true
77
+ @parsed_configs[@element_config] = true unless @element_config.collection?
78
78
  end
79
79
 
80
80
  def parsed_config?
@@ -214,32 +214,30 @@ describe "SAXMachine" do
214
214
  end
215
215
 
216
216
  describe "elements" do
217
- # I took this stuff out because I'm not sure yet if this is something I want to bother supporting.
218
-
219
- # describe "when parsing multiple elements" do
220
- # before :each do
221
- # @klass = Class.new do
222
- # include SAXMachine
223
- # elements :entry, :as => :entries
224
- # end
225
- # end
226
- #
227
- # it "should provide a collection accessor" do
228
- # document = @klass.new
229
- # document.entries << :foo
230
- # document.entries.should == [:foo]
231
- # end
232
- #
233
- # it "should parse a single element" do
234
- # document = @klass.parse("<entry>hello</entry>")
235
- # document.entries.should == ["hello"]
236
- # end
237
- #
238
- # it "should parse multiple elements" do
239
- # document = @klass.parse("<xml><entry>hello</entry><entry>world</entry></xml>")
240
- # document.entries.should == ["hello", "world"]
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pauldix-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix