Class: FutureProof::FutureQueue

Inherits:
Queue
  • Object
show all
Includes:
Exceptionable
Defined in:
lib/future_proof/future_queue.rb

Overview

FutureQueue is designed to handle exceptions that happen in a threads. It raises an exception when user tries to access “exceptional” value.

Instance Method Summary collapse

Constructor Details

#initializeFutureQueue

Returns a new instance of FutureQueue.



9
10
11
12
# File 'lib/future_proof/future_queue.rb', line 9

def initialize
  @finished = false
  super()
end

Instance Method Details

#[](index) ⇒ Object

Note:

allowed only when queue is stopped.

Note:

raises an exception if it happened during execution.

Returns FutureArray with all values.

Returns:

  • (Object)

    value in a queue.



59
60
61
62
# File 'lib/future_proof/future_queue.rb', line 59

def [](index)
  raise_future_proof_exception unless finished?
  values[index]
end

#finished?true, false

Checks if queue is stopped.

Returns:

  • (true, false)

    true if queue is not working.



78
79
80
# File 'lib/future_proof/future_queue.rb', line 78

def finished?
  @finished
end

#popObject

Note:

allowed only when queue is running.

Pops value or raises an exception.

Returns:

  • (Object)

    first value of queue.



38
39
40
41
# File 'lib/future_proof/future_queue.rb', line 38

def pop
  raise_future_proof_exception if finished?
  raise_or_value super
end

#push(*values, &block) ⇒ Object

Note:

allowed only when queue is running.

Pushes value into a queue by executing it. If execution results in an exception then exception is stored itself.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/future_proof/future_queue.rb', line 19

def push(*values, &block)
  raise_future_proof_exception if finished?
  value = if block_given?
    begin
      block.call(*values)
    rescue => e
      e
    end
  else
    values.size == 1 ? values[0] : values
  end
  super(value)
end

#start!Object

Starts queue.



70
71
72
73
# File 'lib/future_proof/future_queue.rb', line 70

def start!
  @values   = nil
  @finished = false
end

#stop!Object

Stops queue.



65
66
67
# File 'lib/future_proof/future_queue.rb', line 65

def stop!
  @finished = true
end

#valuesFutureProof::FutureArray

Note:

allowed only when queue is stopped.

Returns FutureArray with all values.

Returns:



48
49
50
51
# File 'lib/future_proof/future_queue.rb', line 48

def values
  raise_future_proof_exception unless finished?
  @values ||= FutureProof::FutureArray.new(instance_variable_get(:@que).dup)
end