module ActiveSupport::PerThreadRegistry

def self.extended(object)

def self.extended(object)
  object.instance_variable_set "@per_thread_registry_key", object.name.freeze
end

def instance

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

def method_missing(name, *args, &block)

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