Class: StateMachines::Path
- Inherits:
-
Array
- Object
- Array
- StateMachines::Path
- Defined in:
- lib/state_machines/path.rb
Overview
A path represents a sequence of transitions that can be run for a particular object. Paths can walk to new transitions, revealing all of the possible branches that can be encountered in the object’s state machine.
Instance Attribute Summary collapse
-
#machine ⇒ Object
readonly
The state machine this path is walking.
-
#object ⇒ Object
readonly
The object whose state machine is being walked.
Instance Method Summary collapse
-
#complete? ⇒ Boolean
Determines whether or not this path has completed.
-
#events ⇒ Object
Lists all of the events that can be fired through this path.
-
#from_name ⇒ Object
The initial state name for this path.
-
#from_states ⇒ Object
Lists all of the from states that can be reached through this path.
-
#initialize(object, machine, options = {}) ⇒ Path
constructor
Creates a new transition path for the given object.
-
#initialize_copy(orig) ⇒ Object
:nodoc:.
-
#to_name ⇒ Object
The end state name for this path.
-
#to_states ⇒ Object
Lists all of the to states that can be reached through this path.
-
#walk ⇒ Object
Walks down the next transitions at the end of this path.
Constructor Details
#initialize(object, machine, options = {}) ⇒ Path
Creates a new transition path for the given object. Initially this is an empty path. In order to start walking the path, it must be populated with an initial transition.
Configuration options:
-
:target
- The target state to end the path on -
:guard
- Whether to guard transitions with the if/unless conditionals defined for each one
22 23 24 25 26 27 28 29 |
# File 'lib/state_machines/path.rb', line 22 def initialize(object, machine, = {}) .assert_valid_keys(:target, :guard) @object = object @machine = machine @target = [:target] @guard = [:guard] end |
Instance Attribute Details
#machine ⇒ Object (readonly)
The state machine this path is walking
12 13 14 |
# File 'lib/state_machines/path.rb', line 12 def machine @machine end |
#object ⇒ Object (readonly)
The object whose state machine is being walked
9 10 11 |
# File 'lib/state_machines/path.rb', line 9 def object @object end |
Instance Method Details
#complete? ⇒ Boolean
Determines whether or not this path has completed. A path is considered complete when one of the following conditions is met:
-
The last transition in the path ends on the target state
-
There are no more transitions remaining to walk and there is no target state
85 86 87 |
# File 'lib/state_machines/path.rb', line 85 def complete? !empty? && (@target ? to_name == @target : transitions.empty?) end |
#events ⇒ Object
Lists all of the events that can be fired through this path.
For example,
path.events # => [:park, :ignite, :shift_up, ...]
70 71 72 |
# File 'lib/state_machines/path.rb', line 70 def events map { |transition| transition.event }.uniq end |
#from_name ⇒ Object
The initial state name for this path
37 38 39 |
# File 'lib/state_machines/path.rb', line 37 def from_name first&.from_name end |
#from_states ⇒ Object
Lists all of the from states that can be reached through this path.
For example,
path.to_states # => [:parked, :idling, :first_gear, ...]
46 47 48 |
# File 'lib/state_machines/path.rb', line 46 def from_states map { |transition| transition.from_name }.uniq end |
#initialize_copy(orig) ⇒ Object
:nodoc:
31 32 33 34 |
# File 'lib/state_machines/path.rb', line 31 def initialize_copy(orig) #:nodoc: super @transitions = nil end |
#to_name ⇒ Object
The end state name for this path. If a target state was specified for the path, then that will be returned if the path is complete.
52 53 54 |
# File 'lib/state_machines/path.rb', line 52 def to_name last&.to_name end |
#to_states ⇒ Object
Lists all of the to states that can be reached through this path.
For example,
path.to_states # => [:parked, :idling, :first_gear, ...]
61 62 63 |
# File 'lib/state_machines/path.rb', line 61 def to_states map { |transition| transition.to_name }.uniq end |
#walk ⇒ Object
Walks down the next transitions at the end of this path. This will only walk down paths that are considered valid.
76 77 78 |
# File 'lib/state_machines/path.rb', line 76 def walk transitions.each { |transition| yield dup.push(transition) } end |