Sha256: 74167d82c7c2d110a237768f5958da258b731f17b74474baf1e37daab8e3ac46

Contents?: true

Size: 1.94 KB

Versions: 25

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/module/delegation"

module ActiveSupport
  # NOTE: This approach has been deprecated for end-user code in favor of {thread_mattr_accessor}[rdoc-ref:Module#thread_mattr_accessor] and friends.
  # Please use that approach instead.
  #
  # 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

    private
      def method_missing(name, *args, &block)
        # 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

25 entries across 25 versions & 6 rubygems

Version Path
activesupport-6.1.4.7 lib/active_support/per_thread_registry.rb
activesupport-6.1.4.6 lib/active_support/per_thread_registry.rb
activesupport-6.1.4.5 lib/active_support/per_thread_registry.rb
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/per_thread_registry.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/per_thread_registry.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/per_thread_registry.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/per_thread_registry.rb
activesupport-6.1.4.4 lib/active_support/per_thread_registry.rb
activesupport-6.1.4.3 lib/active_support/per_thread_registry.rb
activesupport-6.1.4.2 lib/active_support/per_thread_registry.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/per_thread_registry.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/per_thread_registry.rb
activesupport-6.1.4.1 lib/active_support/per_thread_registry.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/per_thread_registry.rb
activesupport-6.1.4 lib/active_support/per_thread_registry.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.3.1/lib/active_support/per_thread_registry.rb
activesupport-6.1.3.2 lib/active_support/per_thread_registry.rb
activesupport-6.1.3.1 lib/active_support/per_thread_registry.rb
activesupport-6.1.3 lib/active_support/per_thread_registry.rb
activesupport-6.1.2.1 lib/active_support/per_thread_registry.rb