Class: StateMachine::PathCollection
- Inherits:
-
Array
- Object
- Array
- StateMachine::PathCollection
- Includes:
- Assertions
- Defined in:
- lib/state_machine/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
-
#from_name ⇒ Object
readonly
The initial state to start each path from.
-
#machine ⇒ Object
readonly
The state machine these path are walking.
-
#object ⇒ Object
readonly
The object whose state machine is being walked.
-
#to_name ⇒ Object
readonly
The target state for each path.
Instance Method Summary collapse
-
#events ⇒ Object
Lists all of the events that can be fired through the paths in this collection.
-
#from_states ⇒ Object
Lists all of the states that can be transitioned from through the paths in this collection.
-
#initialize(object, machine, options = {}) ⇒ PathCollection
constructor
Creates a new collection of paths with the given requirements.
-
#to_states ⇒ Object
Lists all of the states that can be transitioned to through the paths in this collection.
Methods included from Assertions
#assert_exclusive_keys, #assert_valid_keys
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
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/state_machine/path_collection.rb', line 29 def initialize(object, machine, = {}) = {:deep => false, :from => machine.states.match!(object).name}.merge() assert_valid_keys(, :from, :to, :deep, :guard) @object = object @machine = machine @from_name = machine.states.fetch([:from]).name @to_name = [:to] && machine.states.fetch([:to]).name @guard = [:guard] @deep = [:deep] initial_paths.each {|path| walk(path)} end |
Instance Attribute Details
#from_name ⇒ Object (readonly)
The initial state to start each path from
16 17 18 |
# File 'lib/state_machine/path_collection.rb', line 16 def from_name @from_name end |
#machine ⇒ Object (readonly)
The state machine these path are walking
13 14 15 |
# File 'lib/state_machine/path_collection.rb', line 13 def machine @machine end |
#object ⇒ Object (readonly)
The object whose state machine is being walked
10 11 12 |
# File 'lib/state_machine/path_collection.rb', line 10 def object @object end |
#to_name ⇒ Object (readonly)
The target state for each path
19 20 21 |
# File 'lib/state_machine/path_collection.rb', line 19 def to_name @to_name end |
Instance Method Details
#events ⇒ Object
Lists all of the events that can be fired through the paths in this collection.
For example,
paths.events # => [:park, :ignite, :shift_up, ...]
69 70 71 |
# File 'lib/state_machine/path_collection.rb', line 69 def events map {|path| path.events}.flatten.uniq end |
#from_states ⇒ Object
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, ...]
49 50 51 |
# File 'lib/state_machine/path_collection.rb', line 49 def from_states map {|path| path.from_states}.flatten.uniq end |
#to_states ⇒ Object
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, ...]
59 60 61 |
# File 'lib/state_machine/path_collection.rb', line 59 def to_states map {|path| path.to_states}.flatten.uniq end |