Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Just use a single collector for the entire parsing

  • Loading branch information...
commit a2559bf1868faefd95616f74884585c2d5f2eb95 1 parent ad8e686
@craigambrose authored
View
4 README.markdown
@@ -48,13 +48,13 @@ In this example, Product is a mapping class. It maps to an xml node named "produ
### Run the parser
-The parser object must be supplied with a set of mapping classes to use. These are specified in a hash where the keys are the collector objects to use. You can specify as many mapping classes per collector as you like. You can have just one collector, or different ones for different mapping classes.
+The parser object must be supplied with a collector and an array of mapping classes to use.
```
require 'sax_stream/parser'
collector = SaxStream::NaiveCollector.new
-parser = SaxStream::Parser.new(collector => [Product])
+parser = SaxStream::Parser.new(collector, [Product])
parser.parse_stream(File.open('products.xml'))
```
View
6 lib/sax_stream/internal/mapper_handler.rb
@@ -6,8 +6,8 @@ class MapperHandler
attr_accessor :stack
def initialize(mapper_class, collector, element_stack = ElementStack.new)
- raise ArgumentError unless collector
- raise ArgumentError unless mapper_class
+ raise ArgumentError, "no collector" unless collector
+ raise ArgumentError, "no mapper class" unless mapper_class
@mapper_class = mapper_class
@collector = collector
@@ -49,7 +49,7 @@ def start_current_object(name, attrs)
end
def start_child_node(name, attrs)
- handler = @mapper_class.child_handler_for(name)
+ handler = @mapper_class.child_handler_for(name, @collector)
if handler
@stack.push(handler)
handler.start_element(name, attrs)
View
4 lib/sax_stream/mapper.rb
@@ -40,8 +40,8 @@ def map_key_onto_object(object, key, value)
end
end
- def child_handler_for(name)
- raise NotImplementedError
+ def child_handler_for(name, collector)
+ # raise NotImplementedError
end
private
View
14 lib/sax_stream/parser.rb
@@ -4,14 +4,12 @@
module SaxStream
class Parser
- def initialize(collector_mappers)
- raise ArgumentError, "You must supply your parser with at least one collector and mapper class" if collector_mappers.empty?
- mapper_handlers = []
- collector_mappers.each do |collector, mappers|
- mappers_array = mappers.is_a?(Enumerable) ? mappers : [mappers]
- mappers_array.each do |mapper|
- mapper_handlers << Internal::MapperHandler.new(mapper, collector)
- end
+ def initialize(collector, mappers)
+ raise ArgumentError, "You must supply your parser with a collector" unless collector
+ raise ArgumentError, "You must supply your parser with at least one mapper class" if mappers.empty?
+
+ mapper_handlers = mappers.map do |mapper|
+ Internal::MapperHandler.new(mapper, collector)
end
@sax_handler = Internal::SaxHandler.new(mapper_handlers)
end
View
4 spec/integration/parsing_spec.rb
@@ -18,7 +18,7 @@ class Product
end
it "builds the mapped object for the node and passes it to the collector" do
- parser = SaxStream::Parser.new(collector => [Product])
+ parser = SaxStream::Parser.new(collector, [Product])
parser.parse_stream(open_fixture(:simple_product))
@@ -53,7 +53,7 @@ class PropertyList
end
it "builds the appropriate object for each node" do
- parser = SaxStream::Parser.new(collector => [PropertyList])
+ parser = SaxStream::Parser.new(collector, [PropertyList])
parser.parse_stream(open_fixture(:reaxml))
raise collector.mapped_objects.inspect
View
2  spec/sax_stream/internal/mapper_handler_spec.rb
@@ -59,7 +59,7 @@ module SaxStream
before do
subject.start_element('foobar')
- mapper_class.stub!(:child_handler_for).with('post').and_return(post_handler)
+ mapper_class.stub!(:child_handler_for).with('post', collector).and_return(post_handler)
handler_stack.stub(:push)
end
View
4 spec/sax_stream/parser_spec.rb
@@ -2,9 +2,11 @@
require 'sax_stream/parser'
describe SaxStream::Parser do
+ let(:collector) { double("collector") }
+
it "raises an error if no mappers are supplied" do
lambda {
- SaxStream::Parser.new({})
+ SaxStream::Parser.new(collector, [])
}.should raise_error(ArgumentError)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.