| 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: @contexts = []
28: end
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 80
80: def <<(node)
81: @nodes << node
82: @indices.each {|attribute, index| index[value(node, attribute)] = node}
83: @contexts.each {|context| eval_context(context, node)}
84: self
85: 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 146
146: def [](key, index_name = @default_index)
147: index = self.index(index_name)
148: if index.include?(key)
149: index[key]
150: elsif @indices.include?("#{index_name}_to_s""#{index_name}_to_s")
151: self[key.to_s, "#{index_name}_to_s""#{index_name}_to_s"]
152: end
153: 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 132
132: def at(index)
133: @nodes[index]
134: end
Appends a group of nodes to the collection
# File lib/state_machine/node_collection.rb, line 88
88: def concat(nodes)
89: nodes.each {|node| self << node}
90: end
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 67
67: def context(nodes, &block)
68: nodes = nodes.first.is_a?(Matcher) ? nodes.first : WhitelistMatcher.new(nodes)
69: @contexts << context = {:nodes => nodes, :block => block}
70:
71: # Evaluate the new context for existing nodes
72: each {|node| eval_context(context, node)}
73:
74: context
75: 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 119
119: def each
120: @nodes.each {|node| yield node}
121: self
122: 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 167
167: def fetch(key, index_name = @default_index)
168: self[key, index_name] || raise(IndexError, "#{key.inspect} is an invalid #{index_name}")
169: end
Gets the number of nodes in this collection
# File lib/state_machine/node_collection.rb, line 55
55: def length
56: @nodes.length
57: 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 49
49: def machine=(new_machine)
50: @machine = new_machine
51: each {|node| node.machine = new_machine}
52: 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 95
95: def update(node)
96: @indices.each do |attribute, index|
97: old_key = RUBY_VERSION < '1.9' ? index.index(node) : index.key(node)
98: new_key = value(node, attribute)
99:
100: # Only replace the key if it's changed
101: if old_key != new_key
102: index.delete(old_key)
103: index[new_key] = node
104: end
105: end
106: end
Gets the given index. If the index does not exist, then an ArgumentError is raised.
# File lib/state_machine/node_collection.rb, line 174
174: def index(name)
175: raise ArgumentError, 'No indices configured' unless @indices.any?
176: @indices[name] || raise(ArgumentError, "Invalid index: #{name.inspect}")
177: end