Class: Core::Async::Collection

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Is::Async

async

Constructor Details

#initialize(values = []) ⇒ Collection

Returns a new instance of Collection.



53
54
55
56
57
58
59
# File 'lib/core/async/collection.rb', line 53

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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/core/async/collection.rb', line 15

def build(object)
  errored = false
  stopped = false
  values = []

  object.each do |value|
    future = async {
      begin
        if block_given?
          yield value
        else
          value
        end
      rescue LocalJumpError
        stopped = true
      rescue => error
        errored = true
        raise error
      end
    }

    unless stopped
      values << future
    end

    if errored || stopped
      break
    end
  end

  new(values)
end

Instance Method Details

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

public

Adds a value to the collection.



63
64
65
# File 'lib/core/async/collection.rb', line 63

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

#eachObject

public

Yields each resolved value.



70
71
72
73
74
75
76
# File 'lib/core/async/collection.rb', line 70

def each
  return to_enum(:each) unless block_given?

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