Class: Needle::Lifecycle::Threaded

Inherits:
Pipeline::Element show all
Defined in:
lib/needle/lifecycle/threaded.rb

Overview

The instantiation pipeline element that enforces the singleton multiplicity, on a per-thread basis.

Instance Attribute Summary

Attributes inherited from Pipeline::Element

#name, #options, #priority, #service_point, #succ

Instance Method Summary collapse

Methods inherited from Pipeline::Element

#<=>, #initialize, #initialize_element, set_default_priority

Constructor Details

This class inherits a constructor from Needle::Pipeline::Element

Instance Method Details

#call(container, point) ⇒ Object

Returns the cached reference, if it has been previously cached for the current thread. Otherwise, invokes the next element in the pipeline and caches the result. The cached reference is returned.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/needle/lifecycle/threaded.rb', line 36

def call( container, point )
  cache = service_cache
  name = service_point.fullname

  unless cache.has_key?( name )
    service = succ.call( container, point )
    cache[ name ] = service
  end

  cache[ name ]
end

#reset!Object

Resets the cached singleton instance, so that the next time it is requested it is re-constructed. Only the cache for the current thread and service point is reset.



51
52
53
54
# File 'lib/needle/lifecycle/threaded.rb', line 51

def reset!
  cache = service_cache
  cache.delete service_point.fullname
end

#service_cacheObject

Returns a Hash of threaded services that are cached by the current thread.



29
30
31
# File 'lib/needle/lifecycle/threaded.rb', line 29

def service_cache
  Thread.current[ :threaded_services ] ||= Hash.new
end