A proxy class to aid in deferred instantiation of service points. This is used primarily by the "deferred" service models.
Methods
Public Class methods
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.)
[ show source ]
# File lib/needle/lifecycle/proxy.rb, line 29
29: def initialize( proc_obj=nil, *args, &callback )
30: if proc_obj && callback
31: raise ArgumentError, "only specify argument OR block, not both"
32: end
33:
34: @callback = proc_obj || callback or
35: raise ArgumentError, "callback required"
36:
37: @args = args
38: @mutex = QueryableMutex.new
39: @instantiation_failed = false
40: @instance = nil
41: end
Public Instance methods
[ show source ]
# File lib/needle/lifecycle/proxy.rb, line 85
85: def inspect
86: "#<#{self.class.name}:#{"0x%08x"%self.id}:" +
87: "instantiated=>#{@instance ? true : false}>"
88: end
Attempts to invoke the given message on the service. If the service has not yet been instantiated, it will be instantiated and stored.
[ show source ]
# File lib/needle/lifecycle/proxy.rb, line 45
45: def method_missing( sym, *args, &block )
46: unless @instance || @instantiation_failed
47: @mutex.synchronize do
48: unless @instance || @instantiation_failed
49: begin
50: @instance = @callback.call( *@args )
51: rescue Exception
52: @instantiation_failed = true
53: raise
54: end
55: end
56: end
57: end
58:
59: unless @instantiation_failed
60: @instance.__send__ sym, *args, &block
61: else
62: # just return nil... this way, a failed instantiation won't barf
63: # more than once... I hope...
64: end
65: end