Class: Core::Async::Collection

Inherits:
Object
  • Object
show all
Extended by:
Is::Async
Includes:
Enumerable, Is::Async
Defined in:
lib/core/async/collection.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Is::Async

async, await, aware, defer, resolve, sleep, timeout

Constructor Details

#initialize(values = []) ⇒ Collection

Returns a new instance of Collection.



33
34
35
36
37
38
39
# File 'lib/core/async/collection.rb', line 33

def initialize(values = [])
  unless values.respond_to?(:each)
    raise ArgumentError, "values are not enumerable"
  end

  @values = values
end

Class Method Details

.build(object) ⇒ Object

public

Builds a collection from an enumerable object.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/core/async/collection.rb', line 13

def build(object)
  aware do
    values = object.map { |value|
      async do
        if block_given?
          yield value
        else
          value
        end
      end
    }

    new(values)
  end
end

Instance Method Details

#<<(value) ⇒ Object Also known as: add

public

Adds a value to the collection.



43
44
45
# File 'lib/core/async/collection.rb', line 43

def <<(value)
  @values << value
end

#eachObject

public

Yields each resolved value.



50
51
52
53
54
55
56
# File 'lib/core/async/collection.rb', line 50

def each
  return to_enum(:each) unless block_given?

  @values.each do |value|
    yield resolve(value)
  end
end