Class: FiniteMachine::EventsMap Private
- Inherits:
-
Object
- Object
- FiniteMachine::EventsMap
- Extended by:
- Forwardable
- Defined in:
- lib/finite_machine/events_map.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class responsible for storing mappings between event namess and their transition objects.
Used internally by StateMachine.
Instance Method Summary collapse
-
#add(name, transition) ⇒ nil
Add transition under name.
-
#can_perform?(name, from_state, *conditions) ⇒ Boolean
Check if event is valid and transition can be performed.
-
#choice_transition?(name, from_state) ⇒ Boolean
Check if event has branching choice transitions or not.
-
#clear ⇒ self
Reset map.
-
#events ⇒ Array[Symbol]
Retrieve all event names.
-
#exists?(name) ⇒ Boolean
Check if event is present.
-
#find(name) ⇒ Array[Transition]
(also: #[])
Finds transitions for the event name.
-
#initialize ⇒ EventsMap
constructor
private
Initialize a EventsMap.
-
#inspect ⇒ String
Inspect map content.
-
#match_transition(name, from_state) ⇒ Transition?
private
Find transition without checking conditions.
-
#match_transition_with(name, from_state, *conditions) ⇒ Transition
Examine transitions for event name that start in from state and find one matching condition.
-
#move_to(name, from_state, *conditions) ⇒ Symbol
Find state that this machine can move to.
-
#select_transition(name, from_state, *conditions) ⇒ Transition
Select transition that matches conditions.
-
#state_transitions ⇒ Array[Hash]
Retrieves all state transitions.
-
#states ⇒ Array[Symbol]
Retreive all unique states.
-
#states_for(name) ⇒ Object
Retrieve from states for the event name.
-
#to_s ⇒ String
Return string representation of this map.
Constructor Details
#initialize ⇒ EventsMap
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a EventsMap
24 25 26 |
# File 'lib/finite_machine/events_map.rb', line 24 def initialize @events_map = Concurrent::Map.new end |
Instance Method Details
#add(name, transition) ⇒ nil
Add transition under name
54 55 56 57 58 59 60 61 |
# File 'lib/finite_machine/events_map.rb', line 54 def add(name, transition) if exists?(name) @events_map[name] << transition else @events_map[name] = [transition] end self end |
#can_perform?(name, from_state, *conditions) ⇒ Boolean
Check if event is valid and transition can be performed
131 132 133 |
# File 'lib/finite_machine/events_map.rb', line 131 def can_perform?(name, from_state, *conditions) !match_transition_with(name, from_state, *conditions).nil? end |
#choice_transition?(name, from_state) ⇒ Boolean
Check if event has branching choice transitions or not
150 151 152 |
# File 'lib/finite_machine/events_map.rb', line 150 def choice_transition?(name, from_state) find(name).select { |trans| trans.matches?(from_state) }.size > 1 end |
#clear ⇒ self
Reset map
238 239 240 241 |
# File 'lib/finite_machine/events_map.rb', line 238 def clear @events_map.clear self end |
#events ⇒ Array[Symbol]
Retrieve all event names
88 89 90 |
# File 'lib/finite_machine/events_map.rb', line 88 def events @events_map.keys end |
#exists?(name) ⇒ Boolean
Check if event is present
40 41 42 |
# File 'lib/finite_machine/events_map.rb', line 40 def exists?(name) @events_map.key?(name) end |
#find(name) ⇒ Array[Transition] Also known as: []
Finds transitions for the event name
74 75 76 |
# File 'lib/finite_machine/events_map.rb', line 74 def find(name) @events_map.fetch(name) { [] } end |
#inspect ⇒ String
Inspect map content
264 265 266 |
# File 'lib/finite_machine/events_map.rb', line 264 def inspect "<##{self.class} @events_map=#{self}>" end |
#match_transition(name, from_state) ⇒ Transition?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find transition without checking conditions
166 167 168 |
# File 'lib/finite_machine/events_map.rb', line 166 def match_transition(name, from_state) find(name).find { |trans| trans.matches?(from_state) } end |
#match_transition_with(name, from_state, *conditions) ⇒ Transition
Examine transitions for event name that start in from state and find one matching condition.
183 184 185 186 187 |
# File 'lib/finite_machine/events_map.rb', line 183 def match_transition_with(name, from_state, *conditions) find(name).find do |trans| trans.matches?(from_state) && trans.check_conditions(*conditions) end end |
#move_to(name, from_state, *conditions) ⇒ Symbol
Find state that this machine can move to
227 228 229 230 231 |
# File 'lib/finite_machine/events_map.rb', line 227 def move_to(name, from_state, *conditions) transition = select_transition(name, from_state, *conditions) transition ||= UndefinedTransition.new(name) transition.to_state(from_state) end |
#select_transition(name, from_state, *conditions) ⇒ Transition
Select transition that matches conditions
201 202 203 204 205 206 207 |
# File 'lib/finite_machine/events_map.rb', line 201 def select_transition(name, from_state, *conditions) if choice_transition?(name, from_state) match_transition_with(name, from_state, *conditions) else match_transition(name, from_state) end end |
#state_transitions ⇒ Array[Hash]
Retrieves all state transitions
110 111 112 |
# File 'lib/finite_machine/events_map.rb', line 110 def state_transitions @events_map.values.flatten.map(&:states) end |
#states ⇒ Array[Symbol]
Retreive all unique states
101 102 103 |
# File 'lib/finite_machine/events_map.rb', line 101 def states @events_map.values.flatten.map(&:states).map(&:to_a).flatten.uniq end |
#states_for(name) ⇒ Object
Retrieve from states for the event name
122 123 124 |
# File 'lib/finite_machine/events_map.rb', line 122 def states_for(name) find(name).map(&:states).flat_map(&:keys) end |
#to_s ⇒ String
Return string representation of this map
248 249 250 251 252 253 254 |
# File 'lib/finite_machine/events_map.rb', line 248 def to_s hash = {} @events_map.each_pair do |name, trans| hash[name] = trans end hash.to_s end |