mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
2d42fe7fb5
* It focuses on how to use it. * Removes some ambigueties in the original docs about whether the state is stored in the class. * Documents it provides class-level accessors via method_missing. * Documents that if the extended class has an initializer, it must accept no arguments.
44 lines
1.2 KiB
Ruby
44 lines
1.2 KiB
Ruby
module ActiveSupport
|
|
# This module is used to encapsulate access to thread local variables.
|
|
#
|
|
# Given
|
|
#
|
|
# module ActiveRecord
|
|
# class RuntimeRegistry
|
|
# extend ActiveSupport::PerThreadRegistry
|
|
#
|
|
# attr_accessor :connection_handler
|
|
# end
|
|
# end
|
|
#
|
|
# <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+
|
|
#
|
|
# 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
|
|
#
|
|
# 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.
|
|
#
|
|
# If the class has an initializer, it must accept no arguments.
|
|
module PerThreadRegistry
|
|
def instance
|
|
Thread.current[self.name] ||= new
|
|
end
|
|
|
|
protected
|
|
|
|
def method_missing(*args, &block)
|
|
instance.public_send(*args, &block)
|
|
end
|
|
end
|
|
end
|