Module: MrDarcy

Defined in:
lib/mr_darcy/drivers.rb,
lib/mr_darcy.rb,
lib/mr_darcy/role.rb,
lib/mr_darcy/version.rb,
lib/mr_darcy/promise.rb,
lib/mr_darcy/context.rb,
lib/mr_darcy/deferred.rb,
lib/mr_darcy/promise/em.rb,
lib/mr_darcy/promise/dsl.rb,
lib/mr_darcy/promise/base.rb,
lib/mr_darcy/promise/state.rb,
lib/mr_darcy/drivers/thread.rb,
lib/mr_darcy/promise/thread.rb,
lib/mr_darcy/drivers/celluloid.rb,
lib/mr_darcy/promise/celluloid.rb,
lib/mr_darcy/promise/collection.rb,
lib/mr_darcy/promise/state/base.rb,
lib/mr_darcy/promise/synchronous.rb,
lib/mr_darcy/drivers/synchronous.rb,
lib/mr_darcy/promise/child_promise.rb,
lib/mr_darcy/promise/state/rejected.rb,
lib/mr_darcy/promise/state/resolved.rb,
lib/mr_darcy/promise/state/unresolved.rb

Overview

Driver used for testing synchronously.

Defined Under Namespace

Modules: Drivers, Promise Classes: Context, Deferred, Role

Constant Summary collapse

VERSION =
"0.4.2"

Class Method Summary collapse

Class Method Details

.all_driversObject

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.



29
30
31
32
33
34
# 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

.all_promises(opts = {}) ⇒ Object

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


77
78
79
# File 'lib/mr_darcy.rb', line 77

def all_promises opts={}
  MrDarcy::Promise::Collection.new yield, opts
end

.driverObject

The current driver in use for asynchronicity. Defaults to :Thread



20
21
22
# File 'lib/mr_darcy.rb', line 20

def driver
  @driver ||= :Thread
end

.driver=(driver) ⇒ Object

Set the driver to use for asynchronicity. See #all_drivers for a list of available drivers on your platform.



14
15
16
# File 'lib/mr_darcy.rb', line 14

def driver=(driver)
  @driver=driver
end

.promise(opts = {}, &block) ⇒ Object

Generate a new promise with the provided block. Accepts the following options:

  • driver: override the default driver.

:yields: promise

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


55
56
57
58
# File 'lib/mr_darcy.rb', line 55

def promise opts={}, &block
  driver = opts[:driver] || self.driver
  MrDarcy::Promise.new driver: driver, &block
end