Permalink
Browse files

Make it easier to swap XmlMini backends. Require Nokogiri >= 1.1.1 fo…

…r XmlMini backend tests.
  • Loading branch information...
1 parent 694998e commit 37cf224fdb7259c139450bc33c68ec09489be9c2 @jeremy jeremy committed Mar 10, 2009
@@ -6,15 +6,24 @@ module ActiveSupport
# XmlMini.backend = 'LibXML'
module XmlMini
extend self
- delegate :parse, :to => :@backend
- class << self
- attr_reader :backend
- end
+ attr_reader :backend
+ delegate :parse, :to => :backend
def backend=(name)
- require "active_support/xml_mini/#{name.to_s.downcase}.rb"
- @backend = ActiveSupport.const_get("XmlMini_#{name}")
+ if name.is_a?(Module)
+ @backend = name
+ else
+ require "active_support/xml_mini/#{name.to_s.downcase}.rb"
+ @backend = ActiveSupport.const_get("XmlMini_#{name}")
+ end
+ end
+
+ def with_backend(name)
+ old_backend, self.backend = backend, name
+ yield
+ ensure
+ self.backend = old_backend
end
end
@@ -1,4 +1,4 @@
-# = XML Mini Nokogiri implementation
+# = XmlMini Nokogiri implementation
module ActiveSupport
module XmlMini_Nokogiri #:nodoc:
extend self
@@ -7,8 +7,11 @@ module XmlMini_Nokogiri #:nodoc:
# string::
# XML Document string to parse
def parse(string)
- return {} if string.blank?
- doc = Nokogiri::XML(string).to_hash
+ if string.blank?
+ {}
+ else
+ Nokogiri::XML(string).to_hash
+ end
end
module Conversions
@@ -1,16 +1,19 @@
require 'abstract_unit'
require 'active_support/xml_mini'
-
begin
+ gem 'nokogiri', '>= 1.1.1'
+rescue Gem::LoadError
+ # Skip nokogiri tests
+else
require 'nokogiri'
class NokogiriEngineTest < Test::Unit::TestCase
include ActiveSupport
def setup
- @default_backend = XmlMini.backend.to_s.split('_').last
+ @default_backend = XmlMini.backend
XmlMini.backend = 'Nokogiri'
end
@@ -99,14 +102,9 @@ def test_children_with_non_adjacent_text
private
def assert_equal_rexml(xml)
- XmlMini.backend = 'REXML'
- hash = XmlMini.parse(xml)
-
- XmlMini.backend = 'Nokogiri'
-
+ hash = XmlMini.with_backend('REXML') { XmlMini.parse(xml) }
assert_equal(hash, XmlMini.parse(xml))
end
end
-rescue LoadError
- # Yay, no errors
+
end

0 comments on commit 37cf224

Please sign in to comment.