From 37cf224fdb7259c139450bc33c68ec09489be9c2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Mar 2009 12:08:42 -0700 Subject: Make it easier to swap XmlMini backends. Require Nokogiri >= 1.1.1 for XmlMini backend tests. --- activesupport/lib/active_support/xml_mini.rb | 21 +++++++++++++++------ .../lib/active_support/xml_mini/nokogiri.rb | 9 ++++++--- activesupport/test/xml_mini/nokogiri_engine_test.rb | 16 +++++++--------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index 0513c0d4d0..ccd1349491 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -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 diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb index bfafa29dd5..5c8a6bfe89 100644 --- a/activesupport/lib/active_support/xml_mini/nokogiri.rb +++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb @@ -1,4 +1,4 @@ -# = XML Mini Nokogiri implementation +# = XmlMini Nokogiri implementation module ActiveSupport module XmlMini_Nokogiri #:nodoc: extend self @@ -7,8 +7,11 @@ module ActiveSupport # 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 diff --git a/activesupport/test/xml_mini/nokogiri_engine_test.rb b/activesupport/test/xml_mini/nokogiri_engine_test.rb index 5c4002d34e..1ab36785ac 100644 --- a/activesupport/test/xml_mini/nokogiri_engine_test.rb +++ b/activesupport/test/xml_mini/nokogiri_engine_test.rb @@ -1,8 +1,11 @@ require 'abstract_unit' require 'active_support/xml_mini' - begin + gem 'nokogiri', '>= 1.1.1' +rescue Gem::LoadError + # Skip nokogiri tests +else require 'nokogiri' @@ -10,7 +13,7 @@ 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 @@ class NokogiriEngineTest < Test::Unit::TestCase 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 -- cgit v1.2.3