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.

Methods

<<   []   at   concat   each   fetch   keys   length   machine=   new   update  

Included Modules

Enumerable Assertions

Attributes

machine  [R]  The machine associated with the nodes

Public Class methods

Creates a new collection of nodes for the given state machine. By default, the collection is empty.

Configuration options:

  • :index - One or more attributes to automatically generate hashed indices for in order to perform quick lookups. Default is to index by the :name attribute

Public Instance methods

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

Appends a group of nodes to the collection

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

Gets the set of unique keys for the given index

Gets the number of nodes in this collection

Changes the current machine associated with the collection. In turn, this will change the state machine associated with each node in the collection.

Updates the indexed keys for the given node. If the node‘s attribute has changed since it was added to the collection, the old indexed keys will be replaced with the updated ones.

[Validate]