Class: StateMachines::PathCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/state_machines/path_collection.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, machine, options = {}) ⇒ PathCollection

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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/state_machines/path_collection.rb', line 27

def initialize(object, machine, options = {})
  options = {:deep => false, :from => machine.states.match!(object).name}.merge(options)
  options.assert_valid_keys( :from, :to, :deep, :guard)
  
  @object = object
  @machine = machine
  @from_name = machine.states.fetch(options[:from]).name
  @to_name = options[:to] && machine.states.fetch(options[:to]).name
  @guard = options[:guard]
  @deep = options[:deep]
  
  initial_paths.each {|path| walk(path)}
end

Instance Attribute Details

#from_nameObject (readonly)

The initial state to start each path from



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

def from_name
  @from_name
end

#machineObject (readonly)

The state machine these path are walking



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

def machine
  @machine
end

#objectObject (readonly)

The object whose state machine is being walked



8
9
10
# File 'lib/state_machines/path_collection.rb', line 8

def object
  @object
end

#to_nameObject (readonly)

The target state for each path



17
18
19
# File 'lib/state_machines/path_collection.rb', line 17

def to_name
  @to_name
end

Instance Method Details

#eventsObject

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

For example,

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


67
68
69
# File 'lib/state_machines/path_collection.rb', line 67

def events
  flat_map(&:events).uniq
end

#from_statesObject

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, ...]


47
48
49
# File 'lib/state_machines/path_collection.rb', line 47

def from_states
  flat_map(&:from_states).uniq
end

#to_statesObject

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, ...]


57
58
59
# File 'lib/state_machines/path_collection.rb', line 57

def to_states
  flat_map(&:to_states).uniq
end