Parent

Included Modules

StateMachine::NodeCollection

Represents a collection of nodes in a state machine, be it events or states.

Attributes

machine[R]

The machine associated with the nodes

Public Class Methods

new(machine, options = {}) click to toggle source

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

# File lib/state_machine/node_collection.rb, line 19
def initialize(machine, options = {})
  assert_valid_keys(options, :index)
  options = {:index => :name}.merge(options)
  
  @machine = machine
  @nodes = []
  @indices = Array(options[:index]).inject({}) {|indices, attribute| indices[attribute] = {}; indices}
  @default_index = Array(options[:index]).first
end

Public Instance Methods

<<(node) click to toggle source

Adds a new node to the collection. By doing so, this will also add it to the configured indices.

# File lib/state_machine/node_collection.rb, line 59
def <<(node)
  @nodes << node
  @indices.each {|attribute, index| index[value(node, attribute)] = node}
  self
end
[](key, index_name = @default_index) click to toggle source

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.

# File lib/state_machine/node_collection.rb, line 124
def [](key, index_name = @default_index)
  index(index_name)[key]
end
at(index) click to toggle source

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
# File lib/state_machine/node_collection.rb, line 110
def at(index)
  @nodes[index]
end
concat(nodes) click to toggle source

Appends a group of nodes to the collection

# File lib/state_machine/node_collection.rb, line 66
def concat(nodes)
  nodes.each {|node| self << node}
end
each() click to toggle source

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 --
# File lib/state_machine/node_collection.rb, line 97
def each
  @nodes.each {|node| yield node}
  self
end
fetch(key, index_name = @default_index) click to toggle source

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
# File lib/state_machine/node_collection.rb, line 140
def fetch(key, index_name = @default_index)
  self[key, index_name] || raise(IndexError, "#{key.inspect} is an invalid #{index_name}")
end
keys(index_name = @default_index) click to toggle source

Gets the set of unique keys for the given index

# File lib/state_machine/node_collection.rb, line 53
def keys(index_name = @default_index)
  index(index_name).keys
end
length() click to toggle source

Gets the number of nodes in this collection

# File lib/state_machine/node_collection.rb, line 48
def length
  @nodes.length
end
machine=(new_machine) click to toggle source

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

# File lib/state_machine/node_collection.rb, line 42
def machine=(new_machine)
  @machine = new_machine
  each {|node| node.machine = new_machine}
end
update(node) click to toggle source

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.

# File lib/state_machine/node_collection.rb, line 73
def update(node)
  @indices.each do |attribute, index|
    old_key = RUBY_VERSION < '1.9' ? index.index(node) : index.key(node)
    new_key = value(node, attribute)
    
    # Only replace the key if it's changed
    if old_key != new_key
      index.delete(old_key)
      index[new_key] = node
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.