Class: FSM::FSMState
- Inherits:
-
Object
- Object
- FSM::FSMState
- Defined in:
- lib/barebone-fsm.rb
Overview
FSMState class represents a state of the finite state machine.
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
The events for this state mapped to corresponding event codes.
-
#state ⇒ Object
readonly
The name of the state.
Instance Method Summary collapse
-
#build(&build_block) ⇒ Object
(also: #run)
The #build/#run method sets up the events described as DSL code in the build_block.
-
#event(event_name, &event_block) ⇒ Object
Setup or trigger an event.
-
#initialize(state_machine, state_name) ⇒ FSMState
constructor
A new instance of FSMState.
-
#to_s ⇒ Object
A String representation of the FSMState object.
Constructor Details
#initialize(state_machine, state_name) ⇒ FSMState
Returns a new instance of FSMState.
52 53 54 55 56 |
# File 'lib/barebone-fsm.rb', line 52 def initialize(state_machine, state_name) @fsm = state_machine @state = state_name @events = {} end |
Instance Attribute Details
#events ⇒ Object (readonly)
The events for this state mapped to corresponding event codes.
46 47 48 |
# File 'lib/barebone-fsm.rb', line 46 def events @events end |
#state ⇒ Object (readonly)
The name of the state.
43 44 45 |
# File 'lib/barebone-fsm.rb', line 43 def state @state end |
Instance Method Details
#build(&build_block) ⇒ Object Also known as: run
The #build/#run method sets up the events described as DSL code in the build_block. Only event method is supported within the build_block with the name of the event and an optional block supplied. The operation for each such line is carried out by the #event method.
136 137 138 |
# File 'lib/barebone-fsm.rb', line 136 def build(&build_block) self.instance_eval &build_block end |
#event(event_name, &event_block) ⇒ Object #event(events_hash) ⇒ Object #event(event_name) ⇒ Object
Setup or trigger an event. It sets up a new event when the event_block is provided or event_name is a Hash map. The event_name is triggered otherwise. If the event is nil or not already setup, then the default event is triggered.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/barebone-fsm.rb', line 99 def event(event_name, &event_block) if block_given? then @events[event_name] = event_block elsif event_name.is_a?(Hash) then event_name.each{ |ev, st| @events[ev] = Proc.new{st} } elsif event_name and @events.has_key? event_name then @fsm.event = event_name @fsm.instance_eval &@events[event_name] elsif @events.has_key? :default then @fsm.event = :default @fsm.instance_eval &@events[:default] end end |
#to_s ⇒ Object
A String representation of the FSMState object.
61 62 63 64 65 66 |
# File 'lib/barebone-fsm.rb', line 61 def to_s() @state.to_s + ": [" + @events.keys.map(&:to_s).join(', ') + "]" end |