Class: Yaoc::Helper::ThreadGlobalStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/yaoc/helper/thread_global_storage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeThreadGlobalStorage

Returns a new instance of ThreadGlobalStorage.



25
26
27
# File 'lib/yaoc/helper/thread_global_storage.rb', line 25

def initialize
  self.data||={}
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/yaoc/helper/thread_global_storage.rb', line 10

def data
  @data
end

Class Method Details

.for(scope_name = "default") ⇒ Object



18
19
20
21
22
23
# File 'lib/yaoc/helper/thread_global_storage.rb', line 18

def self.for(scope_name="default")
  mutex.synchronize {
    @storage ||= {}
    @storage[scope_name] ||= new
  }
end

.mutexObject



14
15
16
# File 'lib/yaoc/helper/thread_global_storage.rb', line 14

def self.mutex
  @mutex
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/yaoc/helper/thread_global_storage.rb', line 35

def [](key)
  self.data[key]
end

#[]=(key, value) ⇒ Object



29
30
31
32
33
# File 'lib/yaoc/helper/thread_global_storage.rb', line 29

def []=(key, value)
  mutex.synchronize {
    self.data[key]=value
  }
end

#clear!Object



39
40
41
42
43
# File 'lib/yaoc/helper/thread_global_storage.rb', line 39

def clear!
  mutex.synchronize {
    self.data.clear
  }
end

#fetch(*args, &block) ⇒ Object



45
46
47
# File 'lib/yaoc/helper/thread_global_storage.rb', line 45

def fetch(*args, &block)
  self.data.fetch(*args, &block)
end

#mutexObject



49
50
51
# File 'lib/yaoc/helper/thread_global_storage.rb', line 49

def mutex
  self.class.mutex
end