Class: Module

Inherits:
Object show all
Defined in:
lib/core-extensions/tls.rb

Instance Method Summary collapse

Instance Method Details

#thread_local_attribute(name, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/core-extensions/tls.rb', line 2

def thread_local_attribute(name, &block)
  proc = Proc.new if block_given?
  key = "tls:#{name}"

  singleton_class.class_eval do
    define_method(name) do
      Thread.current[key] ||= proc ? proc.call : nil
    end

    define_method("#{name}=") do |value|
      Thread.current[key] = value
    end
  end
end