From 8d6cffc9f96d026decdba3657f7adcad4200c395 Mon Sep 17 00:00:00 2001 From: wbr Date: Wed, 23 Nov 2011 19:52:26 -0800 Subject: [PATCH] Added ability to set Xml backend with XmlParser.backend (or backend=). Either method will now accept a symbol, string, or parser object. --- ginjo-rfm.gemspec | 5 ++++- lib/rfm/utilities/xml_parser.rb | 28 +++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ginjo-rfm.gemspec b/ginjo-rfm.gemspec index 8b3003cf..ec657c2f 100644 --- a/ginjo-rfm.gemspec +++ b/ginjo-rfm.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= s.authors = ["Geoff Coffey", "Mufaddal Khumri", "Atsushi Matsuo", "Larry Sprock", "Bill Richardson"] - s.date = "2011-11-18" + s.date = "2011-11-24" s.description = "Rfm brings your FileMaker data to Ruby. Now your Ruby scripts and Rails applications can talk directly to your FileMaker server." s.email = "http://groups.google.com/group/rfmcommunity" s.extra_rdoc_files = [ @@ -45,12 +45,14 @@ Gem::Specification.new do |s| if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, ["~> 1.3.0"]) else s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) @@ -58,6 +60,7 @@ Gem::Specification.new do |s| end else s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) diff --git a/lib/rfm/utilities/xml_parser.rb b/lib/rfm/utilities/xml_parser.rb index 9f3f4ae2..1a2060e5 100644 --- a/lib/rfm/utilities/xml_parser.rb +++ b/lib/rfm/utilities/xml_parser.rb @@ -1,6 +1,5 @@ module Rfm module XmlParser - require 'rubygems' require 'active_support' unless defined? ActiveSupport extend self @@ -27,31 +26,38 @@ def new(string_or_file, opts={}) unless opts[:backend] ActiveSupport::XmlMini.parse(string_or_file) else - ActiveSupport::XmlMini.with_backend(get_backend(opts[:backend])) {ActiveSupport::XmlMini.parse(string_or_file)} + ActiveSupport::XmlMini.with_backend(get_backend_from_hash(opts[:backend])) {ActiveSupport::XmlMini.parse(string_or_file)} end end # Shortcut to XmlMini config getter. - def backend + def backend(name=nil) + self.backend= name if name ActiveSupport::XmlMini.backend end # Shortcut to XmlMini config setter. - def backend=(string_or_class) - ActiveSupport::XmlMini.backend = string_or_class - end + def backend=(name) + if name.is_a? Symbol + set_backend_via_hash name + else + ActiveSupport::XmlMini.backend = name + end + end + + private # Given name, return backend config from BACKENDS, including any preloading. # Will raise LoadError if can't load backend. - def get_backend(name) + def get_backend_from_hash(name) backend_hash = BACKENDS[name.to_sym] require backend_hash[:require] backend_hash[:class].is_a?(Proc) ? backend_hash[:class].call : backend_hash[:class] end # Set XmlMini backend, given symbol matching one of the BACKENDS. - def set_backend(name) - self.backend = get_backend(name) + def set_backend_via_hash(name) + ActiveSupport::XmlMini.backend = get_backend_from_hash(name) end # Select the best backend, returns backend config. @@ -59,7 +65,7 @@ def decide_backend string_or_class = catch(:done) do BACKENDS.keys.each do |name| begin - result = get_backend name + result = get_backend_from_hash name throw(:done, result) rescue LoadError end @@ -84,7 +90,7 @@ def ary # Set XmlMini backend when this file loads. begin - self.backend = get_backend FM_CONFIG[:backend] + self.backend = get_backend_from_hash FM_CONFIG[:backend] rescue self.backend = decide_backend end