Class: Needle::Lifecycle::Multiton
- Inherits:
-
Pipeline::Element
- Object
- Pipeline::Element
- Needle::Lifecycle::Multiton
- Defined in:
- lib/needle/lifecycle/multiton.rb
Overview
The instantiation pipeline element that enforces the multiton multiplicity. “Multiton” multiplicity is like singleton multiplicity, except that the guarded instance is unique for each unique set of arguments passed to the multiton.
Instance Attribute Summary
Attributes inherited from Pipeline::Element
#name, #options, #priority, #service_point, #succ
Instance Method Summary collapse
-
#call(container, point, *args) ⇒ Object
Returns the cached reference for the given arguments, if it has been previously cached.
-
#initialize_element ⇒ Object
Creates the mutex to use and initializes the cache.
-
#reset! ⇒ Object
Resets the caches for this multiton object.
Methods inherited from Pipeline::Element
#<=>, #initialize, set_default_priority
Constructor Details
This class inherits a constructor from Needle::Pipeline::Element
Instance Method Details
#call(container, point, *args) ⇒ Object
Returns the cached reference for the given arguments, if it has been previously cached. Otherwise, invokes the next element in the pipeline and caches the result. The cached reference is returned.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/needle/lifecycle/multiton.rb', line 40 def call( container, point, *args ) unless @is_cached[ args ] @mutex.synchronize do unless @is_cached[ args ] @cached[ args ] = succ.call( container, point, *args ) @is_cached[ args ] = true end end end @cached[ args ] end |
#initialize_element ⇒ Object
Creates the mutex to use and initializes the cache.
31 32 33 34 35 |
# File 'lib/needle/lifecycle/multiton.rb', line 31 def initialize_element @mutex = QueryableMutex.new @cached = Hash.new @is_cached = Hash.new( false ) end |
#reset! ⇒ Object
Resets the caches for this multiton object.
54 55 56 57 58 59 |
# File 'lib/needle/lifecycle/multiton.rb', line 54 def reset! @mutex.synchronize do @cached = Hash.new @is_cached = Hash.new( false ) end end |