Module: Thread::Local

Defined in:
lib/thread/local.rb,
lib/thread/local/version.rb

Constant Summary collapse

VERSION =
"1.1.0"

Instance Method Summary collapse

Instance Method Details

#instanceObject

Get the current thread-local instance. Create it if required.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/thread/local.rb', line 36

def instance
  thread = Thread.current
  name = self.name
  
  unless instance = thread.thread_variable_get(name)
    if instance = self.local
      thread.thread_variable_set(name, instance)
    end
  end
  
  return instance
end

#instance=(instance) ⇒ Object

Assigns the current thread-local instance.



51
52
53
54
# File 'lib/thread/local.rb', line 51

def instance= instance
  thread = Thread.current
  thread.thread_variable_set(self.name, instance)
end

#localObject

Instantiate a new thread-local object. By default, invokes new to generate the instance.



30
31
32
# File 'lib/thread/local.rb', line 30

def local
  self.new
end