Class: Bstard::Fsm
- Inherits:
-
Object
- Object
- Bstard::Fsm
- Defined in:
- lib/bstard/fsm.rb
Instance Attribute Summary collapse
-
#current_state ⇒ Object
readonly
Returns the value of attribute current_state.
Instance Method Summary collapse
- #event(event_name, transitions) ⇒ Object
- #events ⇒ Object
- #initial(state) ⇒ Object
-
#initialize ⇒ Fsm
constructor
A new instance of Fsm.
- #on(event, &block) ⇒ Object
- #states ⇒ Object
- #when(state, &block) ⇒ Object
Constructor Details
#initialize ⇒ Fsm
Returns a new instance of Fsm.
4 5 6 7 8 9 10 |
# File 'lib/bstard/fsm.rb', line 4 def initialize() @current_state = :uninitialized @events = {} @states = [] @event_callbacks = {} @state_callbacks = {} end |
Instance Attribute Details
#current_state ⇒ Object (readonly)
Returns the value of attribute current_state.
2 3 4 |
# File 'lib/bstard/fsm.rb', line 2 def current_state @current_state end |
Instance Method Details
#event(event_name, transitions) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/bstard/fsm.rb', line 19 def event(event_name, transitions) evt = event_name.to_sym t = @events.fetch(evt, {}) @events[evt] = t.merge(transitions) transitions.each { |k,v| add_state(k.to_sym); add_state(v.to_sym) } add_event_method(evt) add_can_event_method(evt) end |
#events ⇒ Object
38 39 40 |
# File 'lib/bstard/fsm.rb', line 38 def events @events.keys.sort end |
#initial(state) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/bstard/fsm.rb', line 12 def initial(state) unless state.nil? || state.empty? @current_state = state.to_sym add_state(current_state) end end |
#on(event, &block) ⇒ Object
28 29 30 31 |
# File 'lib/bstard/fsm.rb', line 28 def on(event, &block) callbacks = @event_callbacks.fetch(event.to_sym, []) @event_callbacks[event.to_sym] = callbacks << block end |
#states ⇒ Object
42 43 44 |
# File 'lib/bstard/fsm.rb', line 42 def states @states.sort end |
#when(state, &block) ⇒ Object
33 34 35 36 |
# File 'lib/bstard/fsm.rb', line 33 def when(state, &block) callbacks = @state_callbacks.fetch(state.to_sym, []) @state_callbacks[state.to_sym] = callbacks << block end |