class 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