Class: Goru::Routine

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

Overview

public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Routine.



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

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

Instance Attribute Details

#stateObject (readonly)

public


27
28
29
# File 'lib/goru/routine.rb', line 27

def state
  @state
end

#statusObject (readonly)

public


27
28
29
# File 'lib/goru/routine.rb', line 27

def status
  @status
end

Instance Method Details

#callObject

public


38
39
40
41
42
43
44
# File 'lib/goru/routine.rb', line 38

def call
  @block.call(self)
rescue => error
  @error = error
  set_status(:errored)
  trigger(error)
end

#finished(result = nil) ⇒ Object

public


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

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

#reactor=(reactor) ⇒ Object

public


31
32
33
34
# File 'lib/goru/routine.rb', line 31

def reactor=(reactor)
  @reactor = reactor
  status_changed
end

#resultObject

public


63
64
65
66
67
68
69
70
# File 'lib/goru/routine.rb', line 63

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

#sleep(seconds) ⇒ Object

public


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

def sleep(seconds)
  set_status(:idle)
  @reactor.routine_asleep(self, seconds)
end

#update(state) ⇒ Object

public


57
58
59
# File 'lib/goru/routine.rb', line 57

def update(state)
  @state = state
end

#wakeObject

public


81
82
83
# File 'lib/goru/routine.rb', line 81

def wake
  set_status(:ready)
end