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

Constant Summary collapse

STATUS_READY =
public
:ready
STATUS_FINISHED =
public
:finished
STATUS_ERRORED =
public
:errored
STATUS_IDLE =
public
:idle
STATUS_PAUSED =
public
:paused

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
27
# File 'lib/goru/routine.rb', line 20

def initialize(state = nil, &block)
  @state = state
  @block = block
  @observers = Set.new
  set_status(STATUS_READY)
  @result, @error, @reactor = nil
  @debug = true
end

Instance Attribute Details

#debug=(value) ⇒ Object (writeonly)

public


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

def debug=(value)
  @debug = value
end

#errorObject (readonly)

public


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

def error
  @error
end

#reactorObject

public


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

def reactor
  @reactor
end

#stateObject (readonly)

public


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

def state
  @state
end

#statusObject (readonly)

public


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

def status
  @status
end

Instance Method Details

#add_observer(observer = nil, &block) ⇒ Object

public


161
162
163
# File 'lib/goru/routine.rb', line 161

def add_observer(observer = nil, &block)
  @observers << (block || observer.method(:routine_status_changed))
end

#adoptedObject

public


155
156
157
# File 'lib/goru/routine.rb', line 155

def adopted
  # noop
end

#callObject

public


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

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

#finished(result = nil) ⇒ Object

public


76
77
78
79
80
81
# File 'lib/goru/routine.rb', line 76

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

  throw :continue
end

#finished?Boolean

public

Returns:

  • (Boolean)


119
120
121
# File 'lib/goru/routine.rb', line 119

def finished?
  @status == STATUS_ERRORED || @status == STATUS_FINISHED
end

#pauseObject

public


125
126
127
# File 'lib/goru/routine.rb', line 125

def pause
  set_status(STATUS_PAUSED)
end

#ready?Boolean

public

Returns:

  • (Boolean)


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

def ready?
  @status == STATUS_READY
end

#remove_observer(observer) ⇒ Object

public


167
168
169
# File 'lib/goru/routine.rb', line 167

def remove_observer(observer)
  @observers.delete(observer)
end

#resultObject

public


91
92
93
94
95
96
97
98
# File 'lib/goru/routine.rb', line 91

def result
  case @status
  when STATUS_ERRORED
    raise @error
  else
    @result
  end
end

#resumeObject

public


131
132
133
# File 'lib/goru/routine.rb', line 131

def resume
  set_status(STATUS_READY)
end

#sleep(seconds) ⇒ Object

public


102
103
104
105
106
107
108
109
# File 'lib/goru/routine.rb', line 102

def sleep(seconds)
  set_status(STATUS_IDLE)
  @reactor.asleep_for(seconds) do
    set_status(STATUS_READY)
  end

  throw :continue
end

#update(state) ⇒ Object

public


85
86
87
# File 'lib/goru/routine.rb', line 85

def update(state)
  @state = state
end