Class | StateMachine::NodeCollection |
In: |
lib/state_machine/node_collection.rb
|
Parent: | Object |
Represents a collection of nodes in a state machine, be it events or states.
machine | [R] | The machine associated with the nodes |
Creates a new collection of nodes for the given state machine. By default, the collection is empty.
Configuration options:
Adds a new node to the collection. By doing so, this will also add it to the configured indices.
Gets the node indexed by the given key. By default, this will look up the key in the first index configured for the collection. A custom index can be specified like so:
collection['parked', :value]
The above will look up the "parked" key in a hash indexed by each node‘s value attribute.
If the key cannot be found, then nil will be returned.
Gets the node at the given index.
states = StateMachine::NodeCollection.new states << StateMachine::State.new(machine, :parked) states << StateMachine::State.new(machine, :idling) states.at(0).name # => :parked states.at(1).name # => :idling
Calls the block once for each element in self, passing that element as a parameter.
states = StateMachine::NodeCollection.new states << StateMachine::State.new(machine, :parked) states << StateMachine::State.new(machine, :idling) states.each {|state| puts state.name, ' -- '}
…produces:
parked -- idling --
Gets the node indexed by the given key. By default, this will look up the key in the first index configured for the collection. A custom index can be specified like so:
collection['parked', :value]
The above will look up the "parked" key in a hash indexed by each node‘s value attribute.
If the key cannot be found, then an IndexError exception will be raised:
collection['invalid', :value] # => IndexError: "invalid" is an invalid value
Changes the current machine associated with the collection. In turn, this will change the state machine associated with each node in the collection.