Skip to content

Commit

Permalink
Make specifying handler easier
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnoukhov committed Jul 18, 2014
1 parent c00b50d commit 9b3bada
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,30 @@ $ bundle

## Usage

To use *Nokogiri* as a SAX handler:
To use **Nokogiri** as a SAX handler:

```ruby
require 'sax-machine'
```

To use *Ox* as a SAX handler:
To use **Ox** as a SAX handler:

Add this line to your application's Gemfile:

```ruby
gem 'ox', '>= 2.1.2'
```

Require *Ox* handler and tell SAXMachine to use it:
Tell SAXMachine to use Ox:

```ruby
require 'sax-machine'
require 'sax-machine/handlers/sax_ox_handler'
SAXMachine.handler = :ox
```

Please note that this operation is not thread-safe, so it's better to define
handler at initialization stage and do not change it during runtime.

## Examples

Include `SAXMachine` in any class and define properties to parse:
Expand Down
7 changes: 3 additions & 4 deletions lib/sax-machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
require "sax-machine/sax_document"
require "sax-machine/sax_configure"
require "sax-machine/sax_config"
require "sax-machine/handlers/sax_abstract_handler"
require "sax-machine/handlers/sax_nokogiri_handler"

module SAXMachine
@@handler = :nokogiri

def self.handler
@@handler
end

def self.handler=(handler)
require "sax-machine/handlers/sax_#{handler}_handler"
@@handler = handler
end
end

SAXMachine.handler = :nokogiri
2 changes: 1 addition & 1 deletion lib/sax-machine/handlers/sax_abstract_handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "time"
require 'time'

module SAXMachine
module SAXAbstractHandler
Expand Down
3 changes: 2 additions & 1 deletion lib/sax-machine/handlers/sax_nokogiri_handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "nokogiri"
require 'sax-machine/handlers/sax_abstract_handler'
require 'nokogiri'

module SAXMachine
class SAXNokogiriHandler < Nokogiri::XML::SAX::Document
Expand Down
3 changes: 2 additions & 1 deletion lib/sax-machine/handlers/sax_ox_handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "ox"
require 'sax-machine/handlers/sax_abstract_handler'
require 'ox'

module SAXMachine
class SAXOxHandler < Ox::Sax
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

require File.expand_path(File.dirname(__FILE__) + '/../lib/sax-machine')
if ENV['HANDLER'] == 'ox'
require 'sax-machine/handlers/sax_ox_handler'
SAXMachine.handler = :ox
end

Expand Down

0 comments on commit 9b3bada

Please sign in to comment.