From 20d35ac98e27dd9cefe9901efdc65b0a49e1c45f Mon Sep 17 00:00:00 2001 From: Dmitry Krasnoukhov Date: Tue, 5 Aug 2014 15:38:55 +0300 Subject: [PATCH] Make nokogiri dependency optional --- Gemfile | 1 + README.md | 18 +++++++----------- lib/sax-machine.rb | 6 +++++- sax-machine.gemspec | 1 - spec/spec_helper.rb | 4 +--- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Gemfile b/Gemfile index fefa2cf..0577e70 100644 --- a/Gemfile +++ b/Gemfile @@ -9,5 +9,6 @@ group :development, :test do gem 'coveralls', require: false, platforms: [:mri] gem 'activerecord', '~> 4.1' + gem 'nokogiri', '~> 1.6' gem 'ox', '>= 2.1.2', platforms: [:mri, :rbx] end diff --git a/README.md b/README.md index 50a66b7..3f64a27 100644 --- a/README.md +++ b/README.md @@ -28,30 +28,26 @@ $ bundle ## Usage -To use **Nokogiri** as a SAX handler: +SAX Machine can use either `nokogiri` or `ox` as XML SAX handler. + +To use **Nokogiri** add this line to your Gemfile: ```ruby -require 'sax-machine' +gem 'nokogiri', '~> 1.6' ``` -To use **Ox** as a SAX handler: - -Add this line to your application's Gemfile: +To use **Ox** add this line to your Gemfile: ```ruby gem 'ox', '>= 2.1.2' ``` -Tell SAXMachine to use Ox: +You can also specify which handler to use manually, like this: ```ruby -require 'sax-machine' -SAXMachine.handler = :ox +SAXMachine.handler = :nokogiri ``` -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: diff --git a/lib/sax-machine.rb b/lib/sax-machine.rb index 64fa824..5bfa244 100644 --- a/lib/sax-machine.rb +++ b/lib/sax-machine.rb @@ -14,4 +14,8 @@ def self.handler=(handler) end end -SAXMachine.handler = :nokogiri +begin + SAXMachine.handler = :ox +rescue LoadError + SAXMachine.handler = :nokogiri +end diff --git a/sax-machine.gemspec b/sax-machine.gemspec index 6d603f6..44e4f67 100644 --- a/sax-machine.gemspec +++ b/sax-machine.gemspec @@ -16,6 +16,5 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.platform = Gem::Platform::RUBY - s.add_dependency "nokogiri", "~> 1.6.0" s.add_development_dependency "rspec", "~> 3.0" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 546a9d0..91d44dc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,9 +14,7 @@ end require File.expand_path(File.dirname(__FILE__) + '/../lib/sax-machine') -if ENV['HANDLER'] == 'ox' - SAXMachine.handler = :ox -end +SAXMachine.handler = ENV['HANDLER'].to_sym RSpec.configure do |config| config.run_all_when_everything_filtered = true