Class: YATM::StateMachine
- Inherits:
-
Object
- Object
- YATM::StateMachine
- Defined in:
- lib/yatm/state_machine/state_machine.rb
Instance Attribute Summary collapse
-
#current_state ⇒ Object
readonly
The current state of the state machine.
-
#events ⇒ Array<YATM::StateMachine::Event>
readonly
A list of the events that can processed by the machine.
-
#final_states ⇒ Array
readonly
A list of the final states.
Class Method Summary collapse
-
.statify(state) ⇒ Symbol | nil
If the object could be statified, it’s statified form.
-
.statify!(state) ⇒ Symbol
The object’s statified form.
Instance Method Summary collapse
-
#event(name, **transitions) ⇒ YATM::StateMachine::Event
The event object creared.
-
#final_state(*states) ⇒ Array
The complete list of final states.
- #initial_state(state = nil) ⇒ Object
-
#initialize ⇒ StateMachine
constructor
A new instance of StateMachine.
-
#process(value) ⇒ Hash | nil
A representation of the transition which was triggered, or nil if none was.
-
#process!(value) ⇒ Hash
A representation of the transition which was triggered.
- #reset ⇒ Object
-
#states ⇒ Array
The list of all states of the state machine.
-
#to_s ⇒ String
A string representation of the state machine.
Constructor Details
#initialize ⇒ StateMachine
Returns a new instance of StateMachine.
14 15 16 17 |
# File 'lib/yatm/state_machine/state_machine.rb', line 14 def initialize @events = {} @final_states = [] end |
Instance Attribute Details
#current_state ⇒ Object (readonly)
The current state of the state machine
8 9 10 |
# File 'lib/yatm/state_machine/state_machine.rb', line 8 def current_state @current_state end |
#events ⇒ Array<YATM::StateMachine::Event> (readonly)
Returns A list of the events that can processed by the machine.
10 11 12 |
# File 'lib/yatm/state_machine/state_machine.rb', line 10 def events @events end |
#final_states ⇒ Array (readonly)
Returns A list of the final states.
12 13 14 |
# File 'lib/yatm/state_machine/state_machine.rb', line 12 def final_states @final_states end |
Class Method Details
.statify(state) ⇒ Symbol | nil
Returns If the object could be statified, it’s statified form. Nil otherwise.
134 135 136 |
# File 'lib/yatm/state_machine/state_machine.rb', line 134 def statify(state) statify!(state) rescue StateMachineError end |
.statify!(state) ⇒ Symbol
Returns The object’s statified form.
122 123 124 125 126 127 128 129 |
# File 'lib/yatm/state_machine/state_machine.rb', line 122 def statify!(state) raise InvalidState, state unless state.respond_to?(:to_s) state = state.to_s raise InvalidState, state unless state.respond_to?(:to_sym) state.to_sym end |
Instance Method Details
#event(name, **transitions) ⇒ YATM::StateMachine::Event
Returns The event object creared.
85 86 87 |
# File 'lib/yatm/state_machine/state_machine.rb', line 85 def event(name, **transitions) @events[name] = YATM::Event.new(name, **transitions) end |
#final_state(*states) ⇒ Array
Returns The complete list of final states.
63 64 65 66 67 68 |
# File 'lib/yatm/state_machine/state_machine.rb', line 63 def final_state(*states) states.each do |state| (@final_states << self.class.statify(state)).uniq! end @final_states end |
#initial_state(state) ⇒ Object #initial_state ⇒ Object
53 54 55 56 57 58 |
# File 'lib/yatm/state_machine/state_machine.rb', line 53 def initial_state(state = nil) return @initial_state if state.nil? @initial_state = self.class.statify(state) @current_state = @initial_state end |
#process(value) ⇒ Hash | nil
Returns A representation of the transition which was triggered, or nil if none was.
93 94 95 96 97 |
# File 'lib/yatm/state_machine/state_machine.rb', line 93 def process(value) process!(value) rescue StateMachineError nil end |
#process!(value) ⇒ Hash
Returns A representation of the transition which was triggered.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/yatm/state_machine/state_machine.rb', line 106 def process!(value) return { final: @current_state } if @final_states.include?(@current_state) raise InitialStateNotSet unless @current_state raise InvalidEvent, value unless (event = @events[value]) raise InvalidTransition.new(@current_state, event) unless ( transition = event[@current_state] || event[YATM::ANY] ) @current_state = transition[:to] unless transition[:to] == YATM::SAME transition end |
#reset ⇒ Object
19 20 21 |
# File 'lib/yatm/state_machine/state_machine.rb', line 19 def reset @current_state = @initial_state end |
#states ⇒ Array
Returns The list of all states of the state machine.
40 41 42 43 44 |
# File 'lib/yatm/state_machine/state_machine.rb', line 40 def states @events.map do |_name, event| event.keys + event.map { |_, val| val[:to] } end.flatten.uniq end |
#to_s ⇒ String
Returns A string representation of the state machine.
31 32 33 34 35 36 37 |
# File 'lib/yatm/state_machine/state_machine.rb', line 31 def to_s <<~TO_S.chomp | states: #{states} | current: #{@current_state} | events: #{@events.map(&:name)} TO_S end |