module MrDarcy
This class defines the abstract ability to create DCI roles in MrDarcy
. Start by subclassing and adding some roles and actions and away you go.
The backend abstracting for scheduling blocks asynchronously with Celluloid.
Driver used for testing synchronously.
Backend abstraction for scheduling blocks into threads.
This class implements a synchronous interface to promise execution. It’s not much use, except for unit testing.
Constants
- VERSION
Public Instance Methods
The available drivers for your combination of Ruby implementation and operating system. Note that this is possible drivers, not that you have the necessary dependencies installed - ie it doesn’t check that you have required celluloid before it tries to use it.
# File lib/mr_darcy.rb, line 29 def all_drivers return @drivers if @drivers && !@drivers.empty? drivers ||= %w| synchronous thread celluloid em |.map(&:to_sym) drivers.delete :em if RUBY_ENGINE=='jruby' @drivers = drivers end
Generate a new promise representing a collection of promises. The collection promise resolves once all the collected promises are resolved and rejects as soon as the first promise rejects.
This method collects all promises returned from a provided block:
MrDarcy.all_promises do 10.times.map do |i| MrDarcy.promise do |p| sleep 1 p.resolve i end end end
# File lib/mr_darcy.rb, line 77 def all_promises opts={} MrDarcy::Promise::Collection.new yield, opts end
The current driver in use for asynchronicity. Defaults to :Thread
# File lib/mr_darcy.rb, line 20 def driver @driver ||= :Thread end
Set the driver to use for asynchronicity. See all_drivers
for a list of available drivers on your platform.
# File lib/mr_darcy.rb, line 14 def driver=(driver) @driver=driver end
Generate a new promise with the provided block. Accepts the following options:
-
driver: override the default driver.
Yields a promise into the block as the first argument so that you can resolve or reject from within the block.
MrDarcy.promise do |p| r = rand(10) if r > 5 p.resolve r else p.reject r end end
# File lib/mr_darcy.rb, line 55 def promise opts={}, &block driver = opts[:driver] || self.driver MrDarcy::Promise.new driver: driver, &block end