### Home Classes Methods

### In Files

- lib/state_machine/node_collection.rb

### Parent

Object

### Methods

- ::new
- #<<
- #[]
- #add_to_index
- #at
- #concat
- #context
- #each
- #eval_context
- #fetch
- #index
- #keys
- #length
- #machine=
- #remove_from_index
- #to_sym?
- #update
- #update_index
- #value

### Included Modules

- Enumerable
- StateMachine::Assertions

### Files

- LICENSE

### Class/Module Index

Quicksearch

- StateMachine
- StateMachine::AllMatcher
- StateMachine::Assertions
- StateMachine::AttributeTransitionCollection
- StateMachine::BlacklistMatcher
- StateMachine::Branch
- StateMachine::Callback
- StateMachine::ClassMethods
- StateMachine::Error
- StateMachine::EvalHelpers
- StateMachine::Event
- StateMachine::EventCollection
- StateMachine::Graph
- StateMachine::InstanceMethods
- StateMachine::IntegrationNotFound
- StateMachine::Integrations
- StateMachine::Integrations::ActiveModel
- StateMachine::Integrations::ActiveModel::Observer
- StateMachine::Integrations::ActiveModel::ObserverUpdate
- StateMachine::Integrations::ActiveRecord
- StateMachine::Integrations::Base
- StateMachine::Integrations::Base::ClassMethods
- StateMachine::Integrations::DataMapper
- StateMachine::Integrations::DataMapper::Observer
- StateMachine::Integrations::MongoMapper
- StateMachine::Integrations::Mongoid
- StateMachine::Integrations::Sequel
- StateMachine::InvalidEvent
- StateMachine::InvalidParallelTransition
- StateMachine::InvalidTransition
- StateMachine::LoopbackMatcher
- StateMachine::Machine
- StateMachine::MachineCollection
- StateMachine::MacroMethods
- StateMachine::Matcher
- StateMachine::MatcherHelpers
- StateMachine::NodeCollection
- StateMachine::Path
- StateMachine::PathCollection
- StateMachine::RailsEngine
- StateMachine::State
- StateMachine::StateCollection
- StateMachine::StateContext
- StateMachine::Transition
- StateMachine::TransitionCollection
- StateMachine::WhitelistMatcher
- StateMachine::YARD
- StateMachine::YARD::Handlers
- StateMachine::YARD::Handlers::Base
- StateMachine::YARD::Handlers::Event
- StateMachine::YARD::Handlers::Machine
- StateMachine::YARD::Handlers::State
- StateMachine::YARD::Handlers::Transition
- Object

No matching classes.

# StateMachine::NodeCollection

Represents a collection of nodes in a state machine, be it events or states. Nodes will not differentiate between the String and Symbol versions of the values being indexed.

### 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 21
    def initialize(machine, options = {})
      assert_valid_keys(options, :index)
      options = {:index => :name}.merge(options)

      @machine = machine
      @nodes = []
      @index_names = Array(options[:index])
      @indices = @index_names.inject({}) do |indices, name|
        indices[name] = {}
        indices[:"#{name}_to_s"] = {}
        indices[:"#{name}_to_sym"] = {}
        indices
      end
      @default_index = Array(options[:index]).first
      @contexts = []
    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. This will also evaluate any existings contexts that match the new node.

    # File lib/state_machine/node_collection.rb, line 88
    def <<(node)
      @nodes << node
      @index_names.each {|name| add_to_index(name, value(node, name), node)}
      @contexts.each {|context| eval_context(context, 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 145
    def [](key, index_name = @default_index)
      self.index(index_name)[key] ||
      self.index(:"#{index_name}_to_s")[key.to_s] ||
      to_sym?(key) && self.index(:"#{index_name}_to_sym")[:"#{key}"] ||
      nil
    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 131
    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 96
    def concat(nodes)
      nodes.each {|node| self << node}
    end

context(nodes, &block) click to toggle source

Tracks a context that should be evaluated for any nodes that get added which match the given set of nodes. Matchers can be used so that the context can get added once and evaluated after multiple adds.

    # File lib/state_machine/node_collection.rb, line 75
    def context(nodes, &block)
      nodes = nodes.first.is_a?(Matcher) ? nodes.first : WhitelistMatcher.new(nodes)
      @contexts << context = {:nodes => nodes, :block => block}

      # Evaluate the new context for existing nodes
      each {|node| eval_context(context, node)}

      context
    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 118
    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 164
    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 68
    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 63
    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 57
    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 103
    def update(node)
      @index_names.each {|name| update_index(name, node)}
    end

### Protected Instance Methods

add_to_index(name, key, node) click to toggle source

Adds the given key / node combination to an index, including the string and symbol versions of the index

    # File lib/state_machine/node_collection.rb, line 183
    def add_to_index(name, key, node)
      index(name)[key] = node
      index(:"#{name}_to_s")[key.to_s] = node
      index(:"#{name}_to_sym")[:"#{key}"] = node if to_sym?(key)
    end

eval_context(context, node) click to toggle source

Evaluates the given context for a particular node. This will only evaluate the context if the node matches.

    # File lib/state_machine/node_collection.rb, line 218
    def eval_context(context, node)
      node.context(&context[:block]) if context[:nodes].matches?(node.name)
    end

index(name) click to toggle source

Gets the given index. If the index does not exist, then an ArgumentError is raised.

    # File lib/state_machine/node_collection.rb, line 171
    def index(name)
      raise ArgumentError, 'No indices configured' unless @indices.any?
      @indices[name] || raise(ArgumentError, "Invalid index: #{name.inspect}")
    end

remove_from_index(name, key) click to toggle source

Removes the given key from an index, including the string and symbol versions of the index

    # File lib/state_machine/node_collection.rb, line 191
    def remove_from_index(name, key)
      index(name).delete(key)
      index(:"#{name}_to_s").delete(key.to_s)
      index(:"#{name}_to_sym").delete(:"#{key}") if to_sym?(key)
    end

to_sym?(value) click to toggle source

Determines whether the given value can be converted to a symbol

    # File lib/state_machine/node_collection.rb, line 212
    def to_sym?(value)
      "#{value}" != ''
    end

update_index(name, node) click to toggle source

Updates the node for the given index, including the string and symbol versions of the index

    # File lib/state_machine/node_collection.rb, line 199
    def update_index(name, node)
      index = self.index(name)
      old_key = RUBY_VERSION < '1.9' ? index.index(node) : index.key(node)
      new_key = value(node, name)

      # Only replace the key if it's changed
      if old_key != new_key
        remove_from_index(name, old_key)
        add_to_index(name, new_key, node)
      end
    end

value(node, attribute) click to toggle source

Gets the value for the given attribute on the node

    # File lib/state_machine/node_collection.rb, line 177
    def value(node, attribute)
      node.send(attribute)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.