Class: MiniKraken::Core::DuckFiber

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_kraken/core/duck_fiber.rb

Overview

A mock class that mimicks the behavior of a Fiber instance. More specifically, it responds to ‘resume` message & returns a Context.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aCallable) ⇒ DuckFiber

Constructor.

Parameters:

  • aCallable (Proc, #call)

    The receiver of the ‘call’ message.



18
19
20
21
# File 'lib/mini_kraken/core/duck_fiber.rb', line 18

def initialize(aCallable)
  @callable = valid_callable(aCallable)
  @state = :initial
end

Instance Attribute Details

#callableProc, #call (readonly)

Returns The callable object to yield.

Returns:

  • (Proc, #call)

    The callable object to yield.



11
12
13
# File 'lib/mini_kraken/core/duck_fiber.rb', line 11

def callable
  @callable
end

#stateSymbol (readonly)

Returns one of: :initial, :yielded.

Returns:

  • (Symbol)

    one of: :initial, :yielded



14
15
16
# File 'lib/mini_kraken/core/duck_fiber.rb', line 14

def state
  @state
end

Instance Method Details

#resume(*_args) ⇒ Core::Context, NilClass

Quacks like a Fiber object. The first time, this method will return a Context objet. Subsequents calls just return nil (= no other solution available)

Returns:



27
28
29
30
31
32
33
34
# File 'lib/mini_kraken/core/duck_fiber.rb', line 27

def resume(*_args)
  if state == :initial
    @state = :yielded
    return callable.call
  else
    return nil
  end
end