Class: Goru::Routine
- Inherits:
-
Object
- Object
- Goru::Routine
- Includes:
- Is::Handler
- Defined in:
- lib/goru/routine.rb
Overview
- public
Direct Known Subclasses
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
-
#debug ⇒ Object
writeonly
[public].
-
#error ⇒ Object
readonly
[public].
-
#reactor ⇒ Object
[public].
-
#state ⇒ Object
readonly
[public].
-
#status ⇒ Object
readonly
[public].
Instance Method Summary collapse
-
#add_observer(observer = nil, &block) ⇒ Object
[public].
-
#adopted ⇒ Object
[public].
-
#call ⇒ Object
[public].
-
#finished(result = nil) ⇒ Object
[public].
-
#finished? ⇒ Boolean
[public].
-
#initialize(state = nil, &block) ⇒ Routine
constructor
A new instance of Routine.
-
#pause ⇒ Object
[public].
-
#ready? ⇒ Boolean
[public].
-
#remove_observer(observer) ⇒ Object
[public].
-
#result ⇒ Object
[public].
-
#resume ⇒ Object
[public].
-
#sleep(seconds) ⇒ Object
[public].
-
#update(state) ⇒ Object
[public].
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 |
#error ⇒ Object (readonly)
- public
51 52 53 |
# File 'lib/goru/routine.rb', line 51 def error @error end |
#reactor ⇒ Object
- public
51 52 53 |
# File 'lib/goru/routine.rb', line 51 def reactor @reactor end |
#state ⇒ Object (readonly)
- public
51 52 53 |
# File 'lib/goru/routine.rb', line 51 def state @state end |
#status ⇒ Object (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 |
#adopted ⇒ Object
- public
155 156 157 |
# File 'lib/goru/routine.rb', line 155 def adopted # noop end |
#call ⇒ Object
- 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
119 120 121 |
# File 'lib/goru/routine.rb', line 119 def finished? @status == STATUS_ERRORED || @status == STATUS_FINISHED end |
#pause ⇒ Object
- public
125 126 127 |
# File 'lib/goru/routine.rb', line 125 def pause set_status(STATUS_PAUSED) end |
#ready? ⇒ Boolean
- public
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 |
#result ⇒ Object
- 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 |
#resume ⇒ Object
- 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 |