Class: Needle::Lifecycle::Singleton

### Home Classes Methods

### In Files

- lib/needle/lifecycle/singleton.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::Singleton

The instantiation pipeline element that enforces the singleton multiplicity.

### Public Instance Methods

call( container, point ) click to toggle source

Returns the cached reference, 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/singleton.rb, line 38
    def call( container, point )
      unless @is_cached
        @mutex.synchronize do
          unless @is_cached
            @cached = succ.call( container, point )
            @is_cached = true
          end
        end
      end

      @cached
    end

initialize_element() click to toggle source

Creates the mutex to use and sets the cached reference to `nil`.

    # File lib/needle/lifecycle/singleton.rb, line 29
    def initialize_element
      @mutex = QueryableMutex.new
      @cached = nil
      @is_cached = false
    end

reset!() click to toggle source

Resets the cached singleton instance, so that the next time it is requested it is re-constructed.

    # File lib/needle/lifecycle/singleton.rb, line 53
    def reset!
      @mutex.synchronize do
        @cached = nil
        @is_cached = false
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.