aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorwangjohn <wangjohn@mit.edu>2013-04-09 12:04:39 -0400
committerwangjohn <wangjohn@mit.edu>2013-04-09 22:27:21 -0400
commite94f024e142526181ab98714f919cc2d5c4c394a (patch)
tree0d2784df28028075b9497fb5b11c3ae1f173386c /activesupport
parent9039c5038823754f79e04f1e83723e46229dbe05 (diff)
downloadrails-e94f024e142526181ab98714f919cc2d5c4c394a.tar.gz
rails-e94f024e142526181ab98714f919cc2d5c4c394a.tar.bz2
rails-e94f024e142526181ab98714f919cc2d5c4c394a.zip
Creating a module so that per thread registries can be easily created as
thread local variables.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/per_thread_registry.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/per_thread_registry.rb b/activesupport/lib/active_support/per_thread_registry.rb
new file mode 100644
index 0000000000..b89ce2cbeb
--- /dev/null
+++ b/activesupport/lib/active_support/per_thread_registry.rb
@@ -0,0 +1,41 @@
+module ActiveSupport
+ # This module creates a local registry class inside each thread. It provides
+ # basic methods which will store thread locals in a single class. This
+ # prevents the proliferation of too many thread locals and allows you to
+ # explicitly keep track of each of the variables you're using as thread
+ # locals in a class which includes this module.
+ #
+ # For example, instead of using a bunch of different thread locals to keep
+ # track of some variables like so:
+ #
+ # Thread.current[:active_record_connection_handler] = connection_handler
+ # Thread.current[:active_record_sql_runtime] = sql_runtime
+ #
+ # You could use the following class which implements the +PerThreadRegistry+
+ # module:
+ #
+ # class NewRegistry
+ # extend ActiveSupport::PerThreadRegistry
+ #
+ # attr_accessor :connection_handler, :sql_runtime
+ # end
+ #
+ # NewRegistry.instance.connection_handler = connection_handler
+ # NewRegistry.instance.sql_runtime = sql_runtime
+ #
+ # The new way of keeping track of the thread locals will create a new local
+ # inside of +Thread.current+ with a key which is the name of the extended
+ # class. Now you can easily access per thread variables by just calling the
+ # variable name on the registry.
+ module PerThreadRegistry
+ def instance
+ Thread.current[self.name] ||= new
+ end
+
+ protected
+
+ def method_missing(*args, &block)
+ instance.send(*args, &block)
+ end
+ end
+end