Class: SGF::StateMachine
- Inherits:
-
Object
- Object
- SGF::StateMachine
- Includes:
- Debugger
- Defined in:
- lib/sgf/state_machine.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Transition
Instance Attribute Summary collapse
-
#before_state ⇒ Object
readonly
Returns the value of attribute before_state.
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#context ⇒ Object
Returns the value of attribute context.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#start_state ⇒ Object
readonly
Returns the value of attribute start_state.
-
#state ⇒ Object
Returns the value of attribute state.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
- #clear_buffer ⇒ Object
- #desc(description) ⇒ Object
- #end ⇒ Object
- #event(input) ⇒ Object
-
#initialize(start_state) ⇒ StateMachine
constructor
A new instance of StateMachine.
- #reset ⇒ Object
- #transition(before_state, event_pattern, after_state, callback = nil) ⇒ Object
- #transition_if(condition, before_state, event_pattern, after_state, callback = nil) ⇒ Object
Methods included from Debugger
#debug, #disable_debug_mode, #enable_debug_mode
Constructor Details
#initialize(start_state) ⇒ StateMachine
Returns a new instance of StateMachine.
11 12 13 14 15 |
# File 'lib/sgf/state_machine.rb', line 11 def initialize start_state @start_state = @state = start_state @transitions = {} @buffer = "" end |
Instance Attribute Details
#before_state ⇒ Object (readonly)
Returns the value of attribute before_state.
8 9 10 |
# File 'lib/sgf/state_machine.rb', line 8 def before_state @before_state end |
#buffer ⇒ Object
Returns the value of attribute buffer.
9 10 11 |
# File 'lib/sgf/state_machine.rb', line 9 def buffer @buffer end |
#context ⇒ Object
Returns the value of attribute context.
9 10 11 |
# File 'lib/sgf/state_machine.rb', line 9 def context @context end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
8 9 10 |
# File 'lib/sgf/state_machine.rb', line 8 def input @input end |
#start_state ⇒ Object (readonly)
Returns the value of attribute start_state.
7 8 9 |
# File 'lib/sgf/state_machine.rb', line 7 def start_state @start_state end |
#state ⇒ Object
Returns the value of attribute state.
9 10 11 |
# File 'lib/sgf/state_machine.rb', line 9 def state @state end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
7 8 9 |
# File 'lib/sgf/state_machine.rb', line 7 def transitions @transitions end |
Instance Method Details
#clear_buffer ⇒ Object
72 73 74 |
# File 'lib/sgf/state_machine.rb', line 72 def clear_buffer self.buffer = "" end |
#desc(description) ⇒ Object
17 18 19 |
# File 'lib/sgf/state_machine.rb', line 17 def desc description @description = description end |
#end ⇒ Object
68 69 70 |
# File 'lib/sgf/state_machine.rb', line 68 def end event nil end |
#event(input) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sgf/state_machine.rb', line 45 def event input debug "'#{@state}' + '#{input}'" @before_state = @state @input = input transitions_for_state = self.transitions[@state] return false unless transitions_for_state transition = transitions_for_state.detect do |t| next false if t.condition and not t.condition.call(self) (input.nil? and t.event_pattern.nil?) or input =~ t.event_pattern end if transition @state = transition.after_state unless transition.after_state.nil? transition.callback.call(self) unless transition.callback.nil? true else false end end |
#reset ⇒ Object
41 42 43 |
# File 'lib/sgf/state_machine.rb', line 41 def reset @state = @start_state end |
#transition(before_state, event_pattern, after_state, callback = nil) ⇒ Object
21 22 23 |
# File 'lib/sgf/state_machine.rb', line 21 def transition before_state, event_pattern, after_state, callback = nil transition_if nil, before_state, event_pattern, after_state, callback end |
#transition_if(condition, before_state, event_pattern, after_state, callback = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sgf/state_machine.rb', line 25 def transition_if condition, before_state, event_pattern, after_state, callback = nil if before_state.class == Array saved_description = @description before_state.each do |s| @description = saved_description transition_if(condition, s, event_pattern, after_state, callback) end return end transition = self.transitions[before_state] ||= [] transition << Transition.new(@description, condition, before_state, event_pattern, after_state, callback) @description = nil end |