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) ⇒ Object



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

def after &block
  @afters << block
end

#before(&block) ⇒ Object



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

def before &block
  @befores << block
end

#run(input) ⇒ Object



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

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

#run_aftersObject



21
22
23
# File 'lib/lab42/state_machine/controller.rb', line 21

def run_afters
  run_state_blocks @afters
end

#run_beforesObject



25
26
27
# File 'lib/lab42/state_machine/controller.rb', line 25

def run_befores
  run_state_blocks @befores
end

#run_setups(input) ⇒ Object



35
36
37
38
39
# File 'lib/lab42/state_machine/controller.rb', line 35

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

#run_state_blocks(blox) ⇒ Object



29
30
31
32
33
# File 'lib/lab42/state_machine/controller.rb', line 29

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

#run_statesObject



41
42
43
# File 'lib/lab42/state_machine/controller.rb', line 41

def run_states
  run_state_blocks @handlers[@state]
end

#run_teardowns(input) ⇒ Object



46
47
48
49
50
# File 'lib/lab42/state_machine/controller.rb', line 46

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

#run_transitionsObject



52
53
54
# File 'lib/lab42/state_machine/controller.rb', line 52

def run_transitions
  run_state_blocks @transitions[current_transition]
end

#set_state!(st) ⇒ Object



56
57
58
59
# File 'lib/lab42/state_machine/controller.rb', line 56

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

#setup(&block) ⇒ Object



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

def setup &block; @setups << block end

#state(*args, &block) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

def teardown &block; @teardowns << block end

#transition(trs_hshes, &block) ⇒ Object



72
73
74
75
76
# File 'lib/lab42/state_machine/controller.rb', line 72

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