Class: Goru::Routine

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

Overview

public

Direct Known Subclasses

Goru::Routines::Channel, Goru::Routines::IO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Routine.



20
21
22
23
24
25
26
# File 'lib/goru/routine.rb', line 20

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

Instance Attribute Details

#debug=(value) ⇒ Object (writeonly)

public


34
35
36
# File 'lib/goru/routine.rb', line 34

def debug=(value)
  @debug = value
end

#errorObject (readonly)

public


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

def error
  @error
end

#stateObject (readonly)

public


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

def state
  @state
end

#statusObject (readonly)

public


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

def status
  @status
end

Instance Method Details

#adoptedObject

public


114
115
116
# File 'lib/goru/routine.rb', line 114

def adopted
  # noop
end

#callObject

public


45
46
47
48
49
50
51
# File 'lib/goru/routine.rb', line 45

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

#finished(result = nil) ⇒ Object

public


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

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

  throw :continue
end

#reactor=(reactor) ⇒ Object

public


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

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

#ready?Boolean

public

Returns:

  • (Boolean)


92
93
94
# File 'lib/goru/routine.rb', line 92

def ready?
  @status == :ready
end

#resultObject

public


70
71
72
73
74
75
76
77
# File 'lib/goru/routine.rb', line 70

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

#sleep(seconds) ⇒ Object

public


81
82
83
84
85
86
87
88
# File 'lib/goru/routine.rb', line 81

def sleep(seconds)
  set_status(:idle)
  @reactor.asleep_for(seconds) do
    set_status(:ready)
  end

  throw :continue
end

#update(state) ⇒ Object

public


64
65
66
# File 'lib/goru/routine.rb', line 64

def update(state)
  @state = state
end