Class: Goru::Routine

Inherits:
Object
  • Object
show all
Defined in:
lib/goru/routine.rb

Overview

public

Direct Known Subclasses

Goru::Routines::IO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state = nil, &block) ⇒ Routine

Returns a new instance of Routine.



7
8
9
10
11
12
# File 'lib/goru/routine.rb', line 7

def initialize(state = nil, &block)
  @state = state
  @block = block
  @status = :running
  @result, @error, @reactor = nil
end

Instance Attribute Details

#reactor=(value) ⇒ Object (writeonly)

public


20
21
22
# File 'lib/goru/routine.rb', line 20

def reactor=(value)
  @reactor = value
end

#stateObject (readonly)

public


16
17
18
# File 'lib/goru/routine.rb', line 16

def state
  @state
end

#statusObject (readonly)

public


16
17
18
# File 'lib/goru/routine.rb', line 16

def status
  @status
end

Instance Method Details

#callObject

public


30
31
32
33
34
35
36
37
38
# File 'lib/goru/routine.rb', line 30

def call
  @block.call(self)
rescue => error
  puts "[routine error] #{error}"
  puts error.backtrace

  @error = error
  @status = :errored
end

#finished(result = nil) ⇒ Object

public


42
43
44
45
46
47
# File 'lib/goru/routine.rb', line 42

def finished(result = nil)
  unless @finished
    @result = result
    @status = :finished
  end
end

#resultObject

public


57
58
59
60
61
62
63
64
# File 'lib/goru/routine.rb', line 57

def result
  case @status
  when :errored
    raise @error
  else
    @result
  end
end

#running?Boolean

public

Returns:

  • (Boolean)


24
25
26
# File 'lib/goru/routine.rb', line 24

def running?
  @status == :running
end

#sleep(seconds) ⇒ Object

public


68
69
70
71
# File 'lib/goru/routine.rb', line 68

def sleep(seconds)
  @status = :sleeping
  @reactor.sleep(self, seconds)
end

#update(state) ⇒ Object

public


51
52
53
# File 'lib/goru/routine.rb', line 51

def update(state)
  @state = state
end

#wakeObject

public


75
76
77
# File 'lib/goru/routine.rb', line 75

def wake
  @status = :running
end