Module: Tablesalt::ThreadAccessor::ClassMethods

Includes:
StoreInstance
Defined in:
lib/tablesalt/thread_accessor.rb

Instance Method Summary collapse

Methods included from StoreInstance

#__thread_accessor_store_instance__

Instance Method Details

#thread_accessor(method, thread_key, **options) ⇒ Object (private)

Defines instance methods and singleton methods to read/write a given key in Thread.current

Examples:

class SomeClass
  include Tablesalt::ThreadAccessor

  thread_accessor :current_foo, :foo, private: false
end

SomeClass.current_foo = "bar"
SomeClass.current_foo
=> "bar"

SomeClass.new.current_foo = "baz"
SomeClass.new.current_foo
=> "baz"

Thread.current[:foo]
=> "baz"

Parameters:

  • method (String, Symbol)

    The name of the writer method

  • thread_key (String, Symbol)

    The key to write to on Thread.current

  • :private (Hash)

    a customizable set of options



141
142
143
144
# File 'lib/tablesalt/thread_accessor.rb', line 141

def thread_accessor(method, thread_key, **options)
  thread_reader(method, thread_key, **options)
  thread_writer(method, thread_key, **options)
end

#thread_reader(method, thread_key, **options) ⇒ Object (private)

Defines an instance method and a singleton method to read from a given key in Thread.current

Examples:

class SomeClass
  include Tablesalt::ThreadAccessor

  thread_reader :current_foo, :foo, private: false
end

Thread.current[:foo] = "bar"
SomeClass.current_foo
=> "bar"

SomeClass.new.current_foo
=> "bar"

Parameters:

  • method (String, Symbol)

    The name of the reader method

  • thread_key (String, Symbol)

    The key to read from Thread.current

  • :private (Hash)

    a customizable set of options



68
69
70
71
72
73
74
75
76
# File 'lib/tablesalt/thread_accessor.rb', line 68

def thread_reader(method, thread_key, **options)
  define_method(method) { __thread_accessor_store_instance__[thread_key] }
  define_singleton_method(method) { __thread_accessor_store_instance__[thread_key] }

  return unless options.fetch(:private, true)

  private method
  private_class_method method
end

#thread_writer(method, thread_key, **options) ⇒ Object (private)

Defines an instance method and a singleton method to write to a given key in Thread.current

Note

All written thread variables are tracked on-thread, but will not be automatically cleared when the thread is done processing a request/unit of work. Make sure to either use the provided RackMiddleware or run ThreadAccessor.clean_thread_context manually once the thread is finished to avoid pollluting other requests.

Gem Authors

Thread variables should ideally be kept in a namespaced store instead of the global one. This means you are responsible for clearing your own store - either add your own middleware or advise users how to clear the thread store themselves.

Examples:

class SomeClass
  include Tablesalt::ThreadAccessor

  thread_writer :current_foo, :foo, private: false
end

SomeClass.current_foo = "bar"

Thread.current[:foo]
=> "bar"

Parameters:

  • method (String, Symbol)

    The name of the writer method

  • thread_key (String, Symbol)

    The key to write to on Thread.current

  • :private (Hash)

    a customizable set of options



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tablesalt/thread_accessor.rb', line 106

def thread_writer(method, thread_key, **options)
  method_name = "#{method}="

  define_method(method_name) { |value| __thread_accessor_store_instance__[thread_key] = value }
  define_singleton_method(method_name) { |value| __thread_accessor_store_instance__[thread_key] = value }

  return unless options.fetch(:private, true)

  private method_name
  private_class_method method_name
end