Class: Lab42::StateMachine::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/lab42/state_machine/controller.rb

Instance Method Summary collapse

Instance Method Details

#after(block = nil, &blk) ⇒ Object



5
6
7
8
# File 'lib/lab42/state_machine/controller.rb', line 5

def after  block=nil, &blk
  behavior = get_behavior block, blk
  @afters << behavior
end

#before(block = nil, &blk) ⇒ Object



10
11
12
13
# File 'lib/lab42/state_machine/controller.rb', line 10

def before  block=nil, &blk
  behavior = get_behavior block, blk
  @befores << behavior
end

#run(input) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/lab42/state_machine/controller.rb', line 15

def run input
  @current_input = input
  run_befores
  run_states
  run_transitions
  run_afters
end

#run_aftersObject



23
24
25
# File 'lib/lab42/state_machine/controller.rb', line 23

def run_afters
  run_state_blocks @afters
end

#run_beforesObject



27
28
29
# File 'lib/lab42/state_machine/controller.rb', line 27

def run_befores
  run_state_blocks @befores
end

#run_setups(input) ⇒ Object



37
38
39
40
41
# File 'lib/lab42/state_machine/controller.rb', line 37

def run_setups input
  @setups.each do | block |
    @sm.instance_exec( input, &block )
  end
end

#run_state_blocks(blox) ⇒ Object



31
32
33
34
35
# File 'lib/lab42/state_machine/controller.rb', line 31

def run_state_blocks blox
  blox.each do | block |
    @sm.instance_exec( @current_input, @old_state, @state, &block )
  end
end

#run_statesObject



43
44
45
# File 'lib/lab42/state_machine/controller.rb', line 43

def run_states
  run_state_blocks @handlers[@state]
end

#run_teardowns(input) ⇒ Object



48
49
50
51
52
# File 'lib/lab42/state_machine/controller.rb', line 48

def run_teardowns input
  @teardowns.each do | teardown |
    @sm.instance_exec( input, @state, &teardown )
  end
end

#run_transitionsObject



54
55
56
# File 'lib/lab42/state_machine/controller.rb', line 54

def run_transitions
  run_state_blocks @transitions[current_transition]
end

#set_state!(st) ⇒ Object



58
59
60
61
# File 'lib/lab42/state_machine/controller.rb', line 58

def set_state! st
  @old_state = @state
  @state     = st
end

#setup(block = nil, &blk) ⇒ Object



63
64
65
66
# File 'lib/lab42/state_machine/controller.rb', line 63

def setup  block=nil, &blk
  behavior = get_behavior block, blk
  @setups << behavior
end

#state(*args, &block) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
# File 'lib/lab42/state_machine/controller.rb', line 68

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(&block) ⇒ Object



75
# File 'lib/lab42/state_machine/controller.rb', line 75

def teardown &block; @teardowns << block end

#transition(trs_hshes, &block) ⇒ Object



77
78
79
80
81
# File 'lib/lab42/state_machine/controller.rb', line 77

def transition trs_hshes, &block
  trs_hshes.each do | from, to |
    @transitions[[from, to]] << block
  end
end