Class: StateMachine::StateCollection
- Inherits:
-
NodeCollection
- Object
- NodeCollection
- StateMachine::StateCollection
- Defined in:
- lib/state_machine/state_collection.rb
Overview
Represents a collection of states in a state machine
Instance Method Summary collapse
-
#by_priority ⇒ Object
Gets the order in which states should be displayed based on where they were first referenced.
-
#initialize ⇒ StateCollection
constructor
:nodoc:.
Methods inherited from NodeCollection
#<<, #[], #at, #each, #fetch, #initialize_copy, #keys, #length, #machine=, #update
Methods included from Assertions
#assert_exclusive_keys, #assert_valid_keys
Constructor Details
#initialize ⇒ StateCollection
:nodoc:
6 7 8 |
# File 'lib/state_machine/state_collection.rb', line 6 def initialize #:nodoc: super(:index => [:name, :value]) end |
Instance Method Details
#by_priority ⇒ Object
Gets the order in which states should be displayed based on where they were first referenced. This will order states in the following priority:
-
Initial state
-
Event transitions (:from, :except_from, :to, :except_to options)
-
States with behaviors
-
States referenced via
stateorother_states -
States referenced in callbacks
This order will determine how the GraphViz visualizations are rendered.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/state_machine/state_collection.rb', line 20 def by_priority if first = @nodes.first machine = first.machine order = select {|state| state.initial}.map {|state| state.name} machine.events.each {|event| order += event.known_states} order += select {|state| state.methods.any?}.map {|state| state.name} order += keys(:name) - machine.callbacks.values.flatten.map {|callback| callback.known_states}.flatten order += keys(:name) order.uniq! order.map! {|name| self[name]} order else [] end end |