Class: Hifsm::FSM
- Inherits:
-
Object
- Object
- Hifsm::FSM
- Defined in:
- lib/hifsm/fsm.rb
Overview
This class holds immutable state machine definition
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
-
#all_events ⇒ Object
internals.
- #all_states ⇒ Object
-
#event(name, options = {}, &block) ⇒ Object
DSL.
- #get_state!(name) ⇒ Object
- #initial_state! ⇒ Object
-
#initialize(name = :state, parent = nil, &block) ⇒ FSM
constructor
public API.
- #instantiate(target = nil, initial_state = nil) ⇒ Object
- #state(name, options = {}, &block) ⇒ Object
- #states ⇒ Object
- #to_module ⇒ Object
Constructor Details
#initialize(name = :state, parent = nil, &block) ⇒ FSM
public API
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/hifsm/fsm.rb', line 8 def initialize(name = :state, parent = nil, &block) @name = name @parent = parent @states = {} @initial_state = nil instance_eval(&block) if block @fsm_module = nil @machine_class = nil end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/hifsm/fsm.rb', line 5 def name @name end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
5 6 7 |
# File 'lib/hifsm/fsm.rb', line 5 def transitions @transitions end |
Instance Method Details
#all_events ⇒ Object
internals
59 60 61 |
# File 'lib/hifsm/fsm.rb', line 59 def all_events @states.flat_map {|name, st| st.events }.uniq end |
#all_states ⇒ Object
63 64 65 66 67 68 |
# File 'lib/hifsm/fsm.rb', line 63 def all_states @states.flat_map do |state_name, st| # state should delegate to sub_fsm :) [st] + (st.sub_fsm && st.sub_fsm.all_states || []) end end |
#event(name, options = {}, &block) ⇒ Object
DSL
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hifsm/fsm.rb', line 35 def event(name, = {}, &block) Hifsm::DSL::EventBuilder.new(, &block).each do |ev_def| ev = Hifsm::Event.new name, get_state!(ev_def[:to]), ev_def from_states = ev_def[:from] from_states = @states.keys if from_states.empty? from_states.each do |from| st = get_state!(from) st.add_transition(ev) end end end |
#get_state!(name) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/hifsm/fsm.rb', line 74 def get_state!(name) top_level_state, rest = name.to_s.split('.', 2) st = @states[top_level_state] || raise(Hifsm::MissingState.new(name.to_s)) if rest st.get_substate!(rest) else st end end |
#initial_state! ⇒ Object
70 71 72 |
# File 'lib/hifsm/fsm.rb', line 70 def initial_state! @initial_state || raise(Hifsm::MissingState.new("<initial>")) end |
#instantiate(target = nil, initial_state = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/hifsm/fsm.rb', line 20 def instantiate(target = nil, initial_state = nil) machine_module = get_machine_module machine_name = "#{name}_machine" @machine_class ||= Class.new(Hifsm::Machine) do include machine_module define_method(machine_name) { self } end @machine_class.new(self, target, initial_state) end |
#state(name, options = {}, &block) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/hifsm/fsm.rb', line 51 def state(name, = {}, &block) Hifsm::DSL::StateBuilder.new(, &block).each do |st_def| st = @states[name.to_s] = Hifsm::State.new(name, @parent, st_def) @initial_state = st if [:initial] end end |
#states ⇒ Object
30 31 32 |
# File 'lib/hifsm/fsm.rb', line 30 def states @states.keys end |
#to_module ⇒ Object
84 85 86 |
# File 'lib/hifsm/fsm.rb', line 84 def to_module get_fsm_module end |