class StateMachine::PathCollection

Represents a collection of paths that are generated based on a set of requirements regarding what states to start and end on

Attributes

from_name[R]

The initial state to start each path from

machine[R]

The state machine these path are walking

object[R]

The object whose state machine is being walked

to_name[R]

The target state for each path

Public Class Methods

new(object, machine, options = {}) click to toggle source

Creates a new collection of paths with the given requirements.

Configuration options:

  • :from - The initial state to start from

  • :to - The target end state

  • :deep - Whether to enable deep searches for the target state.

  • :guard - Whether to guard transitions with the if/unless conditionals defined for each one

   # File lib/state_machine/path_collection.rb
29 def initialize(object, machine, options = {})
30   options = {:deep => false, :from => machine.states.match!(object).name}.merge(options)
31   assert_valid_keys(options, :from, :to, :deep, :guard)
32   
33   @object = object
34   @machine = machine
35   @from_name = machine.states.fetch(options[:from]).name
36   @to_name = options[:to] && machine.states.fetch(options[:to]).name
37   @guard = options[:guard]
38   @deep = options[:deep]
39   
40   initial_paths.each {|path| walk(path)}
41 end

Public Instance Methods

events() click to toggle source

Lists all of the events that can be fired through the paths in this collection.

For example,

paths.events  # => [:park, :ignite, :shift_up, ...]
   # File lib/state_machine/path_collection.rb
69 def events
70   map {|path| path.events}.flatten.uniq
71 end
from_states() click to toggle source

Lists all of the states that can be transitioned from through the paths in this collection.

For example,

paths.from_states # => [:parked, :idling, :first_gear, ...]
   # File lib/state_machine/path_collection.rb
49 def from_states
50   map {|path| path.from_states}.flatten.uniq
51 end
to_states() click to toggle source

Lists all of the states that can be transitioned to through the paths in this collection.

For example,

paths.to_states # => [:idling, :first_gear, :second_gear, ...]
   # File lib/state_machine/path_collection.rb
59 def to_states
60   map {|path| path.to_states}.flatten.uniq
61 end

Private Instance Methods

initial_paths() click to toggle source

Gets the initial set of paths to walk

   # File lib/state_machine/path_collection.rb
75 def initial_paths
76   machine.events.transitions_for(object, :from => from_name, :guard => @guard).map do |transition|
77     path = Path.new(object, machine, :target => to_name, :guard => @guard)
78     path << transition
79     path
80   end
81 end
walk(path) click to toggle source

Walks down the given path. Each new path that matches the configured requirements will be added to this collection.

   # File lib/state_machine/path_collection.rb
85 def walk(path)
86   self << path if path.complete?
87   path.walk {|next_path| walk(next_path)} unless to_name && path.complete? && !@deep
88 end