aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/xml_mini.rb
diff options
context:
space:
mode:
authorNikita Afanasenko <nikita@afanasenko.name>2012-11-14 09:58:33 +0400
committerNikita Afanasenko <nikita@afanasenko.name>2012-11-15 20:23:05 +0400
commit1fab2002e3385c40ef48008b649d78ce5e16a868 (patch)
tree795f34b3a2ec860c67884fed1ffe3fbbed969511 /activesupport/lib/active_support/xml_mini.rb
parentf732f9b7b822c9806cf3e07f127d0771b815500b (diff)
downloadrails-1fab2002e3385c40ef48008b649d78ce5e16a868.tar.gz
rails-1fab2002e3385c40ef48008b649d78ce5e16a868.tar.bz2
rails-1fab2002e3385c40ef48008b649d78ce5e16a868.zip
Make XmlMini.with_backend usable with threads
`XmlMini.with_backend` now may be safely used with threads: Thread.new do XmlMini.with_backend("REXML") { rexml_power } end Thread.new do XmlMini.with_backend("LibXML") { libxml_power } end Each thread will use it's own backend.
Diffstat (limited to 'activesupport/lib/active_support/xml_mini.rb')
-rw-r--r--activesupport/lib/active_support/xml_mini.rb38
1 files changed, 29 insertions, 9 deletions
diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb
index 88f9acb588..d082a0a499 100644
--- a/activesupport/lib/active_support/xml_mini.rb
+++ b/activesupport/lib/active_support/xml_mini.rb
@@ -76,23 +76,24 @@ module ActiveSupport
)
end
- attr_reader :backend
delegate :parse, :to => :backend
+ def backend
+ current_thread_backend || @backend
+ end
+
def backend=(name)
- if name.is_a?(Module)
- @backend = name
- else
- require "active_support/xml_mini/#{name.downcase}"
- @backend = ActiveSupport.const_get("XmlMini_#{name}")
- end
+ backend = name && cast_backend_name_to_module(name)
+ self.current_thread_backend = backend if current_thread_backend
+ @backend = backend
end
def with_backend(name)
- old_backend, self.backend = backend, name
+ old_backend = current_thread_backend
+ self.current_thread_backend = name && cast_backend_name_to_module(name)
yield
ensure
- self.backend = old_backend
+ self.current_thread_backend = old_backend
end
def to_tag(key, value, options)
@@ -163,6 +164,25 @@ module ActiveSupport
f.content_type = entity['content_type']
f
end
+
+ private
+
+ def current_thread_backend
+ Thread.current[:xml_mini_backend]
+ end
+
+ def current_thread_backend=(name)
+ Thread.current[:xml_mini_backend] = name && cast_backend_name_to_module(name)
+ end
+
+ def cast_backend_name_to_module(name)
+ if name.is_a?(Module)
+ name
+ else
+ require "active_support/xml_mini/#{name.downcase}"
+ ActiveSupport.const_get("XmlMini_#{name}")
+ end
+ end
end
XmlMini.backend = 'REXML'