module AS::PerThreadRegistry
This module is used to encapsulate access to thread local variables.
Instead of polluting the thread locals namespace:
Thread.current[:connection_handler]
you define a class that extends this module:
module ActiveRecord class RuntimeRegistry extend AS::PerThreadRegistry attr_accessor :connection_handler end end
and invoke the declared instance accessors as class methods. So
ActiveRecord::RuntimeRegistry.connection_handler = connection_handler
sets a connection handler local to the current thread, and
ActiveRecord::RuntimeRegistry.connection_handler
returns a connection handler local to the current thread.
This feature is accomplished by instantiating the class and storing the instance as a thread local keyed by the class name. In the example above a key “ActiveRecord::RuntimeRegistry” is stored in Thread.current
. The class methods proxy to said thread local instance.
If the class has an initializer, it must accept no arguments.
Private Instance Methods
per_thread_registry_instance()
click to toggle source
# File lib/as/per_thread_registry.rb, line 53 def per_thread_registry_instance Thread.current[name] ||= new end