Sha256: 29f1c8bf2baf2f1b2dc757bcf2006fefc2af63144e748059ac33d718079c79ee

Contents?: true

Size: 1.73 KB

Versions: 20

Compression:

Stored size: 1.73 KB

Contents

require 'active_support/core_ext/module/delegation'

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.
        #
        # By letting #delegate handle it, we avoid an enclosure that'll capture args.
        singleton_class.delegate name, to: :instance

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

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/per_thread_registry.rb
activesupport-4.2.11.2 lib/active_support/per_thread_registry.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/per_thread_registry.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/per_thread_registry.rb
activesupport-4.2.11.1 lib/active_support/per_thread_registry.rb
activesupport-4.2.11 lib/active_support/per_thread_registry.rb
activesupport-4.2.10 lib/active_support/per_thread_registry.rb
activesupport-4.2.10.rc1 lib/active_support/per_thread_registry.rb
activesupport-4.2.9 lib/active_support/per_thread_registry.rb
activesupport-4.2.9.rc2 lib/active_support/per_thread_registry.rb
activesupport-4.2.9.rc1 lib/active_support/per_thread_registry.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/per_thread_registry.rb
activesupport-4.2.8 lib/active_support/per_thread_registry.rb
activesupport-4.2.8.rc1 lib/active_support/per_thread_registry.rb
activesupport-4.2.7.1 lib/active_support/per_thread_registry.rb
activesupport-4.2.7 lib/active_support/per_thread_registry.rb
activesupport-4.2.7.rc1 lib/active_support/per_thread_registry.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/per_thread_registry.rb
activesupport-4.2.6 lib/active_support/per_thread_registry.rb
activesupport-4.2.6.rc1 lib/active_support/per_thread_registry.rb