Sha256: 494459dd89a5041cb3c5498be358d8a23051623e9d5b893110657747aeea2c62

Contents?: true

Size: 1.64 KB

Versions: 281

Compression:

Stored size: 1.64 KB

Contents

module ActiveSupport
  # 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 ActiveSupport::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 <tt>Thread.current</tt>.
  # The class methods proxy to said thread local instance.
  #
  # If the class has an initializer, it must accept no arguments.
  module PerThreadRegistry
    def self.extended(object)
      object.instance_variable_set '@per_thread_registry_key', object.name.freeze
    end

    def instance
      Thread.current[@per_thread_registry_key] ||= new
    end

    protected
      def method_missing(name, *args, &block) # :nodoc:
        # Caches the method definition as a singleton method of the receiver.
        define_singleton_method(name) do |*a, &b|
          instance.public_send(name, *a, &b)
        end

        send(name, *args, &block)
      end
  end
end

Version data entries

281 entries across 277 versions & 10 rubygems

Version Path
elasticsearch-persistence-queryable-0.1.9 lib/elasticsearch/per_thread_registry.rb
elasticsearch-persistence-queryable-0.1.8 lib/elasticsearch/per_thread_registry.rb
appsignal-1.3.6 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.6.beta.1 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.5 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.5.beta.1 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.4 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.3 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.4.0.alpha.2 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.4.0.alpha.1 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.2 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.1 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.0 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.0.beta.3 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.0.beta.2 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.3.0.beta.1 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.2.5 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.2.4 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.2.4.beta.4 lib/vendor/active_support/per_thread_registry.rb
appsignal-1.2.4.beta.3 lib/vendor/active_support/per_thread_registry.rb