2013-04-09 12:04:39 -04:00
|
|
|
module ActiveSupport
|
2013-04-13 02:15:11 -04:00
|
|
|
# This module is used to encapsulate access to thread local variables.
|
2013-04-09 12:04:39 -04:00
|
|
|
#
|
2013-04-13 02:15:11 -04:00
|
|
|
# Given
|
2013-04-09 12:04:39 -04:00
|
|
|
#
|
2013-04-13 02:15:11 -04:00
|
|
|
# module ActiveRecord
|
|
|
|
# class RuntimeRegistry
|
|
|
|
# extend ActiveSupport::PerThreadRegistry
|
2013-04-09 12:04:39 -04:00
|
|
|
#
|
2013-04-13 02:15:11 -04:00
|
|
|
# attr_accessor :connection_handler
|
|
|
|
# end
|
|
|
|
# end
|
2013-04-09 12:04:39 -04:00
|
|
|
#
|
2013-04-13 02:15:11 -04:00
|
|
|
# <tt>ActiveRecord::RuntimeRegistry</tt> gets an +instance+ class method
|
|
|
|
# that returns an instance of the class unique to the current thread. Thus,
|
|
|
|
# instead of polluting +Thread.current+
|
2013-04-09 12:04:39 -04:00
|
|
|
#
|
2013-04-13 02:15:11 -04:00
|
|
|
# Thread.current[:connection_handler]
|
|
|
|
#
|
|
|
|
# you write
|
|
|
|
#
|
|
|
|
# ActiveRecord::RuntimeRegistry.instance.connection_handler
|
|
|
|
#
|
|
|
|
# A +method_missing+ handler that proxies to the thread local instance is
|
|
|
|
# installed in the extended class so the call above can be shortened to
|
|
|
|
#
|
|
|
|
# ActiveRecord::RuntimeRegistry.connection_handler
|
2013-04-09 12:04:39 -04:00
|
|
|
#
|
2013-04-13 02:15:11 -04:00
|
|
|
# The instance is stored as a thread local keyed by the name of the class,
|
|
|
|
# that is the string "ActiveRecord::RuntimeRegistry" in the example above.
|
2013-04-09 12:04:39 -04:00
|
|
|
#
|
2013-04-13 02:15:11 -04:00
|
|
|
# If the class has an initializer, it must accept no arguments.
|
2013-04-09 12:04:39 -04:00
|
|
|
module PerThreadRegistry
|
|
|
|
def instance
|
|
|
|
Thread.current[self.name] ||= new
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def method_missing(*args, &block)
|
2013-04-10 09:56:40 -04:00
|
|
|
instance.public_send(*args, &block)
|
2013-04-09 12:04:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|