Class: MrDarcy::Promise::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/mr_darcy/promise/collection.rb

Overview

A meta-promise that represents a collection of other promises and their states.

Instance Method Summary collapse

Constructor Details

#initialize(promises, opts = {}) ⇒ Collection

Creates a meta-promise based on a collection of promises.

  • promises: an array of promises

  • opts: options for MrDarcy.promise


18
19
20
21
22
23
24
25
26
27
# File 'lib/mr_darcy/promise/collection.rb', line 18

def initialize promises, opts={}
  @lock     = Mutex.new
  @promises = []
  @size     = promises.size
  @promise  = MrDarcy.promise opts do
    promises.each do |p|
      add_promise p
    end
  end
end

Instance Method Details

#each(&block) ⇒ Object

Allow iteration of promises for Enumerable.


36
37
38
# File 'lib/mr_darcy/promise/collection.rb', line 36

def each &block
  promises.each &block
end

#finalObject

See MrDarcy::Promise::Base#final


30
31
32
33
# File 'lib/mr_darcy/promise/collection.rb', line 30

def final
  my_promise.final
  self
end

#promisesObject

Return an array of promises in this collection.


53
54
55
# File 'lib/mr_darcy/promise/collection.rb', line 53

def promises
  @lock.synchronize { @promises }
end

#push(promise) ⇒ Object Also known as: <<

Add a new promise to our collection.


41
42
43
44
# File 'lib/mr_darcy/promise/collection.rb', line 41

def push promise
  @lock.synchronize { @size = @size + 1 }
  add_promise promise
end

#sizeObject

Return the number of promises in this collection.


48
49
50
# File 'lib/mr_darcy/promise/collection.rb', line 48

def size
  @lock.synchronize { @size }
end