Class: Failbot::ThreadLocalVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/failbot/thread_local_variable.rb

Overview

Public: A simplified implementation of [::Concurrent::ThreadLocalVar](github.com/ruby-concurrency/concurrent-ruby/blob/7dc6eb04142f008ffa79a59c125669c6fcbb85a8/lib/concurrent-ruby/concurrent/atomic/thread_local_var.rb)

Why not just use concurrent-ruby? We wanted to minimize external dependencies to avoid conflicts with gems already installed with github/github.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ThreadLocalVariable

Returns a new instance of ThreadLocalVariable.



7
8
9
10
# File 'lib/failbot/thread_local_variable.rb', line 7

def initialize(&block)
  @default_block = block || proc {}
  @key = "_thread_local_variable##{object_id}"
end

Instance Method Details

#valueObject



12
13
14
15
16
17
18
# File 'lib/failbot/thread_local_variable.rb', line 12

def value
  if Thread.current.key?(@key)
    Thread.current[@key]
  else
    Thread.current[@key] = @default_block.call
  end
end

#value=(val) ⇒ Object



20
21
22
# File 'lib/failbot/thread_local_variable.rb', line 20

def value=(val)
  Thread.current[@key] = val
end