Permalink
Browse files

Removed Configuration class and implemented a builder pattern for def…

…ining parser behavior
  • Loading branch information...
1 parent f4b58e3 commit e4f348e0e1d862fdca2742a9f040b058f3446a1a @soulcutter committed Apr 14, 2012
View
@@ -1,7 +1,7 @@
require 'nokogiri'
require "saxerator/version"
-require 'saxerator/configuration'
+require 'saxerator/document'
require 'saxerator/string_with_attributes'
require 'saxerator/hash_with_attributes'
require 'saxerator/xml_node'
@@ -10,12 +10,11 @@
require 'saxerator/parser/document_latch'
require 'saxerator/parser/element_name_latch'
require 'saxerator/parser/latched_accumulator'
-require 'saxerator/parser/nokogiri'
module Saxerator
extend self
def parser(xml)
- Saxerator::Configuration.new(xml)
+ Saxerator::Document.new(xml)
end
end
@@ -1,13 +0,0 @@
-module Saxerator
- class Configuration
- attr_reader :source
-
- def initialize(source)
- @source = source
- end
-
- def for_tag(tag)
- Saxerator::Parser::Nokogiri.new(self, source, tag.to_s)
- end
- end
-end
@@ -0,0 +1,25 @@
+module Saxerator
+ class Document
+ include Enumerable
+
+ def initialize(source, config = nil, latches = [])
+ @source = source
+ @latches = latches
+ @config = config
+ end
+
+ def for_tag(tag)
+ Document.new(@source, @config, @latches + [Parser::ElementNameLatch.new(tag.to_s)])
+ end
+
+ def each(&block)
+ document = Parser::LatchedAccumulator.new(@config, @latches, block)
+ parser = ::Nokogiri::XML::SAX::Parser.new document
+
+ # Always have to start at the beginning of a File
+ @source.rewind if(@source.is_a?(File))
+
+ parser.parse(@source)
+ end
+ end
+end
@@ -1,10 +1,6 @@
module Saxerator
module Parser
class DocumentLatch < ::Nokogiri::XML::SAX::Document
- def initialize
- @open = false
- end
-
def open
@open = true
end
@@ -1,23 +0,0 @@
-module Saxerator
- module Parser
- class Nokogiri
- include Enumerable
-
- def initialize(config, source, tag)
- @config = config
- @source = source
- @tag = tag
- end
-
- def each(&block)
- document = LatchedAccumulator.new(@config, [ElementNameLatch.new(@tag)], block)
- parser = ::Nokogiri::XML::SAX::Parser.new document
-
- # Always have to start at the beginning of a File
- @source.rewind if(@source.is_a?(File))
-
- parser.parse(@source)
- end
- end
- end
-end

0 comments on commit e4f348e

Please sign in to comment.