Class: AsyncProxy::ObjectProxy
Overview
a wrapper around an object that will intercept all method calls and run them asynchronously
Direct Known Subclasses
Instance Method Summary collapse
- #async ⇒ Object
- #each ⇒ Object
-
#initialize(object) ⇒ ObjectProxy
constructor
A new instance of ObjectProxy.
-
#map(&block) ⇒ Object
calling
map
on a proxy will run the block in parallel for each element of the Enumerable. - #ready? ⇒ Boolean
-
#register_callback(&proc) ⇒ Object
called with a block, runs it as soon the computation’s result is available.
- #sync(options = {}) ⇒ Object
- #to_s ⇒ Object
-
#when_ready(trace_name = nil, &block) ⇒ Object
runs the block with the computation’s result as the first argument as soon as the value is available.
Constructor Details
#initialize(object) ⇒ ObjectProxy
Returns a new instance of ObjectProxy.
5 6 7 8 |
# File 'lib/object_proxy.rb', line 5 def initialize(object) @object = object @callbacks = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object (private)
67 68 69 70 71 |
# File 'lib/object_proxy.rb', line 67 def method_missing(symbol, *args) when_ready(symbol) do |obj| obj.send(symbol, *args) end end |
Instance Method Details
#async ⇒ Object
14 15 16 |
# File 'lib/object_proxy.rb', line 14 def async self end |
#each ⇒ Object
22 23 24 |
# File 'lib/object_proxy.rb', line 22 def each sync.each{yield} end |
#map(&block) ⇒ Object
calling map
on a proxy will run the block in parallel for each element of the Enumerable
async_array.map(&block).each
# starts processing the first element even if the other ones are not ready yet
32 33 34 35 36 37 38 |
# File 'lib/object_proxy.rb', line 32 def map(&block) when_ready do |enum| enum.map do |item| AsyncProxy::ResultProxy.new(item, block) end end end |
#ready? ⇒ Boolean
40 41 42 |
# File 'lib/object_proxy.rb', line 40 def ready? true end |
#register_callback(&proc) ⇒ Object
called with a block, runs it as soon the computation’s result is available. The return value of the block is discarded: use when_ready
if you are interested in the block’s return value
58 59 60 61 62 63 64 |
# File 'lib/object_proxy.rb', line 58 def register_callback(&proc) if ready? proc.call(sync) else @callbacks << proc end end |
#sync(options = {}) ⇒ Object
10 11 12 |
# File 'lib/object_proxy.rb', line 10 def sync( = {}) @object end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/object_proxy.rb', line 18 def to_s sync.to_s end |
#when_ready(trace_name = nil, &block) ⇒ Object
runs the block with the computation’s result as the first argument as soon as the value is available
returns an async proxy object, that can be used exactly to recover the block’s return value or chain more computations
49 50 51 52 53 |
# File 'lib/object_proxy.rb', line 49 def when_ready(trace_name = nil, &block) result_proxy = AsyncProxy::ComputedProxy.new(self, block, trace_name) register_callback {result_proxy.launch_computation} result_proxy end |