Module: Fiber::Local
- Defined in:
- lib/fiber/local.rb,
lib/fiber/local/version.rb
Constant Summary collapse
- VERSION =
"1.1.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#instance ⇒ Object
Get the current thread-local instance.
-
#instance=(instance) ⇒ Object
Assigns to the fiber-local instance.
-
#local ⇒ Object
Instantiate a new thread-local object.
Class Method Details
.extended(klass) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fiber/local.rb', line 11 def self.extended(klass) attribute_name = klass.name.gsub('::', '_').gsub(/\W/, '').downcase.to_sym # This is used for the general interface and fiber storage key: klass.instance_variable_set(:@fiber_local_attribute_name, attribute_name) klass.singleton_class.attr :fiber_local_attribute_name # This is used for reading and writing directly to the thread instance variables: klass.instance_variable_set(:@fiber_local_variable_name, :"@#{attribute_name}") Thread.attr_accessor(attribute_name) end |
Instance Method Details
#instance ⇒ Object
Get the current thread-local instance. Create it if required.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fiber/local.rb', line 33 def instance # This is considered a local "override" in the dynamic scope of the fiber: if instance = Fiber[@fiber_local_attribute_name] return instance end # This is generally the fast path: thread = Thread.current unless instance = thread.instance_variable_get(@fiber_local_variable_name) if instance = self.local thread.instance_variable_set(@fiber_local_variable_name, instance) end end return instance end |
#instance=(instance) ⇒ Object
Assigns to the fiber-local instance.
52 53 54 |
# File 'lib/fiber/local.rb', line 52 def instance= instance Fiber[@fiber_local_attribute_name] = instance end |
#local ⇒ Object
Instantiate a new thread-local object. By default, invokes new to generate the instance.
27 28 29 |
# File 'lib/fiber/local.rb', line 27 def local self.new end |