Class: StateMachines::Path

Inherits:
Array
  • Object
show all
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

Instance Method Summary collapse

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



24
25
26
27
28
29
30
31
# File 'lib/state_machines/path.rb', line 24

def initialize(object, machine, options = {})
  options.assert_valid_keys(:target, :guard)

  @object = object
  @machine = machine
  @target = options[:target]
  @guard = options[:guard]
end

Instance Attribute Details

#machineObject (readonly)

The state machine this path is walking



14
15
16
# File 'lib/state_machines/path.rb', line 14

def machine
  @machine
end

#objectObject (readonly)

The object whose state machine is being walked



11
12
13
# File 'lib/state_machines/path.rb', line 11

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

Returns:

  • (Boolean)


87
88
89
# File 'lib/state_machines/path.rb', line 87

def complete?
  !empty? && (@target ? to_name == @target : transitions.empty?)
end

#eventsObject

Lists all of the events that can be fired through this path.

For example,

path.events # => [:park, :ignite, :shift_up, ...]


72
73
74
# File 'lib/state_machines/path.rb', line 72

def events
  map { |transition| transition.event }.uniq
end

#from_nameObject

The initial state name for this path



39
40
41
# File 'lib/state_machines/path.rb', line 39

def from_name
  first&.from_name
end

#from_statesObject

Lists all of the from states that can be reached through this path.

For example,

path.to_states  # => [:parked, :idling, :first_gear, ...]


48
49
50
# File 'lib/state_machines/path.rb', line 48

def from_states
  map { |transition| transition.from_name }.uniq
end

#initialize_copy(orig) ⇒ Object

:nodoc:



33
34
35
36
# File 'lib/state_machines/path.rb', line 33

def initialize_copy(orig) #:nodoc:
  super
  @transitions = nil
end

#to_nameObject

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.



54
55
56
# File 'lib/state_machines/path.rb', line 54

def to_name
  last&.to_name
end

#to_statesObject

Lists all of the to states that can be reached through this path.

For example,

path.to_states  # => [:parked, :idling, :first_gear, ...]


63
64
65
# File 'lib/state_machines/path.rb', line 63

def to_states
  map { |transition| transition.to_name }.uniq
end

#walkObject

Walks down the next transitions at the end of this path. This will only walk down paths that are considered valid.



78
79
80
# File 'lib/state_machines/path.rb', line 78

def walk
  transitions.each { |transition| yield dup.push(transition) }
end