Class: Lab42::StateMachine::Controller
- Inherits:
-
Object
- Object
- Lab42::StateMachine::Controller
show all
- Includes:
- StreamApi
- Defined in:
- lib/lab42/state_machine/controller.rb,
lib/lab42/state_machine/controller/stream_api.rb
Defined Under Namespace
Modules: StreamApi
Constant Summary
collapse
- SkipState =
Class.new RuntimeError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from StreamApi
#drop_until, #drop_until!, #drop_while, #drop_while!, #rewind!
Instance Attribute Details
#stream ⇒ Object
Returns the value of attribute stream.
8
9
10
|
# File 'lib/lab42/state_machine/controller.rb', line 8
def stream
@stream
end
|
Instance Method Details
#after(blk = nil, &block) ⇒ Object
12
13
14
|
# File 'lib/lab42/state_machine/controller.rb', line 12
def after blk=nil, &block
@afters << mk_block(blk, block)
end
|
#before(blk = nil, &block) ⇒ Object
16
17
18
|
# File 'lib/lab42/state_machine/controller.rb', line 16
def before blk=nil, &block
@befores << mk_block(blk, block)
end
|
#run(input) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/lab42/state_machine/controller.rb', line 20
def run input
mk_input_stream input
run_setups
loop do
run_one
end
run_teardowns
end
|
#run_afters ⇒ Object
29
30
31
|
# File 'lib/lab42/state_machine/controller.rb', line 29
def run_afters
run_state_blocks @afters
end
|
#run_befores ⇒ Object
33
34
35
|
# File 'lib/lab42/state_machine/controller.rb', line 33
def run_befores
run_state_blocks @befores
end
|
#run_one ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/lab42/state_machine/controller.rb', line 37
def run_one
@current_input = stream.next
run_befores
run_states
run_transitions
run_afters
rescue SkipState
end
|
#run_setup_blocks(blox) ⇒ Object
46
47
48
49
50
|
# File 'lib/lab42/state_machine/controller.rb', line 46
def run_setup_blocks blox
blox.each do | block |
@sm.instance_exec( __peek__, &block )
end
end
|
#run_setups ⇒ Object
58
59
60
61
62
63
|
# File 'lib/lab42/state_machine/controller.rb', line 58
def run_setups
@setups.each do | block |
@sm.instance_exec( peek_first, &block )
end
rescue SkipState
end
|
#run_state_blocks(blox) ⇒ Object
52
53
54
55
56
|
# File 'lib/lab42/state_machine/controller.rb', line 52
def run_state_blocks blox
blox.each do | block |
@sm.instance_exec( @current_input, @old_state, @state, &block )
end
end
|
#run_states ⇒ Object
65
66
67
|
# File 'lib/lab42/state_machine/controller.rb', line 65
def run_states
run_state_blocks @handlers[@state]
end
|
#run_teardowns ⇒ Object
69
70
71
72
73
74
|
# File 'lib/lab42/state_machine/controller.rb', line 69
def run_teardowns
@teardowns.each do | teardown |
@sm.instance_exec( @current_input, @state, &teardown )
end
rescue SkipState
end
|
#run_transitions ⇒ Object
76
77
78
|
# File 'lib/lab42/state_machine/controller.rb', line 76
def run_transitions
run_state_blocks @transitions[current_transition]
end
|
#set_state!(st) ⇒ Object
80
81
82
83
|
# File 'lib/lab42/state_machine/controller.rb', line 80
def set_state! st
@old_state = @state
@state = st
end
|
#setup(blk = nil, &block) ⇒ Object
85
86
87
|
# File 'lib/lab42/state_machine/controller.rb', line 85
def setup blk=nil, &block
@setups << mk_block(blk, block)
end
|
#skip ⇒ Object
89
90
91
|
# File 'lib/lab42/state_machine/controller.rb', line 89
def skip
raise SkipState, "skipping actions in this setup/state/teardown"
end
|
#state(*args, &block) ⇒ Object
93
94
95
96
97
98
|
# File 'lib/lab42/state_machine/controller.rb', line 93
def state *args, &block
raise ArgumentError, "args.size = #{args.size} instead of 0..1" if args.size > 1
return @state if block.nil? && args.empty?
return set_state! args.first unless block
@handlers[ args.first ] << block
end
|
#teardown(blk = nil, &block) ⇒ Object
100
101
102
|
# File 'lib/lab42/state_machine/controller.rb', line 100
def teardown blk=nil, &block
@teardowns << mk_block(blk, block)
end
|
#transition(trs_hshes, &block) ⇒ Object
104
105
106
107
108
|
# File 'lib/lab42/state_machine/controller.rb', line 104
def transition trs_hshes, &block
trs_hshes.each do | from, to |
@transitions[[from, to]] << block
end
end
|