Class: Needle::Lifecycle::Multiton

### Home Classes Methods

### In Files

- lib/needle/lifecycle/multiton.rb

### Parent

Needle::Pipeline::Element

### Methods

- #call
- #initialize_element
- #reset!

### Files

- README

### Class/Module Index

Quicksearch

- Needle
- Needle::Bug
- Needle::Container
- Needle::DefinitionContext
- Needle::IncludeExclude
- Needle::Interceptor
- Needle::Interceptor::DynamicInterceptor
- Needle::InterceptorChainBuilder
- Needle::InterceptorChainBuilder::InterceptedServiceProxy
- Needle::InterceptorChainBuilder::InterceptorChainElement
- Needle::InterceptorChainBuilder::ProxyObjectChainElement
- Needle::InterceptorConfigurationError
- Needle::Lifecycle
- Needle::Lifecycle::Deferred
- Needle::Lifecycle::Initialize
- Needle::Lifecycle::Multiton
- Needle::Lifecycle::Proxy
- Needle::Lifecycle::Singleton
- Needle::Lifecycle::Threaded
- Needle::LogFactory
- Needle::Logger
- Needle::LoggingInterceptor
- Needle::NeedleError
- Needle::Pipeline
- Needle::Pipeline::Collection
- Needle::Pipeline::Collection::BlockElement
- Needle::Pipeline::Element
- Needle::Pipeline::InterceptorElement
- Needle::QueryableMutex
- Needle::Registry
- Needle::ServiceNotFound
- Needle::ServicePoint
- Needle::Version

No matching classes.

# Needle::Lifecycle::Multiton

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.

### Public Instance Methods

call( container, point, *args ) click to toggle source

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.

    # 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() click to toggle source

Creates the mutex to use and initializes the cache.

    # File lib/needle/lifecycle/multiton.rb, line 31
    def initialize_element
      @mutex = QueryableMutex.new
      @cached = Hash.new
      @is_cached = Hash.new( false )
    end

reset!() click to toggle source

Resets the caches for this multiton object.

    # File lib/needle/lifecycle/multiton.rb, line 54
    def reset!
      @mutex.synchronize do
        @cached = Hash.new
        @is_cached = Hash.new( false )
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.