Class: Needle::Lifecycle::Proxy

### Home Classes Methods

### In Files

- lib/needle/lifecycle/proxy.rb

### Parent

Object

### Methods

- ::new
- #inspect
- #method_missing

### 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::Proxy

A proxy class to aid in deferred instantiation of service points. This is used primarily by the "deferred" service models.

### Public Class Methods

new( proc_obj=nil, *args, &callback ) click to toggle source

Create a new proxy object that wraps the object returned by either the `proc_obj` or `callback`. (Only one of `proc_obj` or `callback` should be specified.)

    # File lib/needle/lifecycle/proxy.rb, line 29
    def initialize( proc_obj=nil, *args, &callback )
      if proc_obj && callback
        raise ArgumentError, "only specify argument OR block, not both"
      end

      @callback = proc_obj || callback or
        raise ArgumentError, "callback required"

      @args = args
      @mutex = QueryableMutex.new
      @instantiation_failed = false
      @instance = nil
    end

### Public Instance Methods

inspect() click to toggle source

    # File lib/needle/lifecycle/proxy.rb, line 84
    def inspect
      "#<#{self.class.name}:#{"0x%08x"%self.object_id}:" +
      "instantiated=>#{@instance ? true : false}>"
    end

method_missing( sym, *args, &block ) click to toggle source

Attempts to invoke the given message on the service. If the service has not yet been instantiated, it will be instantiated and stored.

    # File lib/needle/lifecycle/proxy.rb, line 45
    def method_missing( sym, *args, &block )
      unless @instance || @instantiation_failed
        @mutex.synchronize do
          unless @instance || @instantiation_failed
            begin
              @instance = @callback.call( *@args )
            rescue Exception
              @instantiation_failed = true
              raise
            end
          end
        end
      end

      unless @instantiation_failed
        @instance.__send__ sym, *args, &block
      else
        # just return nil... this way, a failed instantiation won't barf
        # more than once... I hope...
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.