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!, #has_next?, #last?, #peek, #reject, #rewind, #rewind!, #select, #take_until
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
45
46
|
# File 'lib/lab42/state_machine/controller.rb', line 37
def run_one
@current_input = stream.peek
@stream = stream.drop 1
run_befores
run_states
run_transitions
run_afters
rescue SkipState
end
|
#run_setup_blocks(blox) ⇒ Object
48
49
50
51
52
|
# File 'lib/lab42/state_machine/controller.rb', line 48
def run_setup_blocks blox
blox.each do | block |
@sm.instance_exec( __peek__, &block )
end
end
|
#run_setups ⇒ Object
60
61
62
63
64
65
|
# File 'lib/lab42/state_machine/controller.rb', line 60
def run_setups
@setups.each do | block |
@sm.instance_exec( peek_first, &block )
end
rescue SkipState
end
|
#run_state_blocks(blox) ⇒ Object
54
55
56
57
58
|
# File 'lib/lab42/state_machine/controller.rb', line 54
def run_state_blocks blox
blox.each do | block |
@sm.instance_exec( @current_input, @old_state, @state, &block )
end
end
|
#run_states ⇒ Object
67
68
69
|
# File 'lib/lab42/state_machine/controller.rb', line 67
def run_states
run_state_blocks @handlers[@state]
end
|
#run_teardowns ⇒ Object
71
72
73
74
75
76
|
# File 'lib/lab42/state_machine/controller.rb', line 71
def run_teardowns
@teardowns.each do | teardown |
@sm.instance_exec( @current_input, @state, &teardown )
end
rescue SkipState
end
|
#run_transitions ⇒ Object
78
79
80
|
# File 'lib/lab42/state_machine/controller.rb', line 78
def run_transitions
run_state_blocks @transitions[current_transition]
end
|
#set_state!(st) ⇒ Object
82
83
84
85
|
# File 'lib/lab42/state_machine/controller.rb', line 82
def set_state! st
@old_state = @state
@state = st
end
|
#setup(blk = nil, &block) ⇒ Object
87
88
89
|
# File 'lib/lab42/state_machine/controller.rb', line 87
def setup blk=nil, &block
@setups << mk_block(blk, block)
end
|
#skip ⇒ Object
91
92
93
|
# File 'lib/lab42/state_machine/controller.rb', line 91
def skip
raise SkipState, "skipping actions in this setup/state/teardown"
end
|
#state(*args, &block) ⇒ Object
95
96
97
98
99
100
|
# File 'lib/lab42/state_machine/controller.rb', line 95
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
102
103
104
|
# File 'lib/lab42/state_machine/controller.rb', line 102
def teardown blk=nil, &block
@teardowns << mk_block(blk, block)
end
|
#transition(trs_hshes, &block) ⇒ Object
106
107
108
109
110
|
# File 'lib/lab42/state_machine/controller.rb', line 106
def transition trs_hshes, &block
trs_hshes.each do | from, to |
@transitions[[from, to]] << block
end
end
|