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:
# File lib/state_machine/node_collection.rb, line 19 19: def initialize(machine, options = {}) 20: assert_valid_keys(options, :index) 21: options = {:index => :name}.merge(options) 22: 23: @machine = machine 24: @nodes = [] 25: @indices = Array(options[:index]).inject({}) {|indices, attribute| indices[attribute] = {}; indices} 26: @default_index = Array(options[:index]).first 27: end
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 124: def [](key, index_name = @default_index) 125: index(index_name)[key] 126: end
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 110: def at(index) 111: @nodes[index] 112: end
Appends a group of nodes to the collection
# File lib/state_machine/node_collection.rb, line 66 66: def concat(nodes) 67: nodes.each {|node| self << node} 68: end
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 97: def each 98: @nodes.each {|node| yield node} 99: self 100: end
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 140: def fetch(key, index_name = @default_index) 141: self[key, index_name] || raise(IndexError, "#{key.inspect} is an invalid #{index_name}") 142: end
Gets the number of nodes in this collection
# File lib/state_machine/node_collection.rb, line 48 48: def length 49: @nodes.length 50: end
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 42: def machine=(new_machine) 43: @machine = new_machine 44: each {|node| node.machine = new_machine} 45: end
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 73: def update(node) 74: @indices.each do |attribute, index| 75: old_key = RUBY_VERSION < '1.9' ? index.index(node) : index.key(node) 76: new_key = value(node, attribute) 77: 78: # Only replace the key if it's changed 79: if old_key != new_key 80: index.delete(old_key) 81: index[new_key] = node 82: end 83: end 84: end