Class: StateMachines::NodeCollection
- Inherits:
-
Object
- Object
- StateMachines::NodeCollection
- Includes:
- Enumerable
- Defined in:
- lib/state_machines/node_collection.rb
Overview
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#machine ⇒ Object
The machine associated with the nodes.
Instance Method Summary collapse
-
#<<(node) ⇒ Object
Adds a new node to the collection.
-
#[](key, index_name = @default_index) ⇒ Object
Gets the node indexed by the given key.
-
#at(index) ⇒ Object
Gets the node at the given index.
-
#concat(nodes) ⇒ Object
Appends a group of nodes to the collection.
-
#context(nodes, &block) ⇒ Object
Tracks a context that should be evaluated for any nodes that get added which match the given set of nodes.
-
#each ⇒ Object
Calls the block once for each element in self, passing that element as a parameter.
-
#fetch(key, index_name = @default_index) ⇒ Object
Gets the node indexed by the given key.
-
#initialize(machine, options = {}) ⇒ NodeCollection
constructor
Creates a new collection of nodes for the given state machine.
-
#initialize_copy(orig) ⇒ Object
Creates a copy of this collection such that modifications don’t affect the original collection.
-
#keys(index_name = @default_index) ⇒ Object
Gets the set of unique keys for the given index.
-
#length ⇒ Object
Gets the number of nodes in this collection.
-
#update(node) ⇒ Object
Updates the indexed keys for the given node.
Constructor Details
#initialize(machine, options = {}) ⇒ NodeCollection
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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/state_machines/node_collection.rb', line 18 def initialize(machine, = {}) .assert_valid_keys(:index) = { index: :name }.merge() @machine = machine @nodes = [] @index_names = Array([:index]) @indices = @index_names.reduce({}) do |indices, name| indices[name] = {} indices[:"#{name}_to_s"] = {} indices[:"#{name}_to_sym"] = {} indices end @default_index = Array([:index]).first @contexts = [] end |
Instance Attribute Details
#machine ⇒ Object
The machine associated with the nodes
9 10 11 |
# File 'lib/state_machines/node_collection.rb', line 9 def machine @machine end |
Instance Method Details
#<<(node) ⇒ Object
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.
85 86 87 88 89 90 |
# File 'lib/state_machines/node_collection.rb', line 85 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) ⇒ Object
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.
142 143 144 145 146 147 |
# File 'lib/state_machines/node_collection.rb', line 142 def [](key, index_name = @default_index) index(index_name)[key] || index(:"#{index_name}_to_s")[key.to_s] || to_sym?(key) && index(:"#{index_name}_to_sym")[:"#{key}"] || nil end |
#at(index) ⇒ Object
Gets the node at the given index.
states = StateMachines::NodeCollection.new
states << StateMachines::State.new(machine, :parked)
states << StateMachines::State.new(machine, :idling)
states.at(0).name # => :parked
states.at(1).name # => :idling
128 129 130 |
# File 'lib/state_machines/node_collection.rb', line 128 def at(index) @nodes[index] end |
#concat(nodes) ⇒ Object
Appends a group of nodes to the collection
93 94 95 |
# File 'lib/state_machines/node_collection.rb', line 93 def concat(nodes) nodes.each { |node| self << node } end |
#context(nodes, &block) ⇒ Object
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.
72 73 74 75 76 77 78 79 80 |
# File 'lib/state_machines/node_collection.rb', line 72 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 ⇒ Object
Calls the block once for each element in self, passing that element as a parameter.
states = StateMachines::NodeCollection.new
states << StateMachines::State.new(machine, :parked)
states << StateMachines::State.new(machine, :idling)
states.each {|state| puts state.name, ' -- '}
…produces:
parked -- idling --
115 116 117 118 |
# File 'lib/state_machines/node_collection.rb', line 115 def each @nodes.each { |node| yield node } self end |
#fetch(key, index_name = @default_index) ⇒ Object
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
161 162 163 |
# File 'lib/state_machines/node_collection.rb', line 161 def fetch(key, index_name = @default_index) self[key, index_name] || fail(IndexError, "#{key.inspect} is an invalid #{index_name}") end |
#initialize_copy(orig) ⇒ Object
Creates a copy of this collection such that modifications don’t affect the original collection
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/state_machines/node_collection.rb', line 37 def initialize_copy(orig) #:nodoc: super nodes = @nodes contexts = @contexts @nodes = [] @contexts = [] @indices = @indices.reduce({}) { |indices, (name, *)| indices[name] = {}; indices } # Add nodes *prior* to copying over the contexts so that they don't get # evaluated multiple times concat(nodes.map { |n| n.dup }) @contexts = contexts.dup end |
#keys(index_name = @default_index) ⇒ Object
Gets the set of unique keys for the given index
65 66 67 |
# File 'lib/state_machines/node_collection.rb', line 65 def keys(index_name = @default_index) index(index_name).keys end |
#length ⇒ Object
Gets the number of nodes in this collection
60 61 62 |
# File 'lib/state_machines/node_collection.rb', line 60 def length @nodes.length end |
#update(node) ⇒ Object
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.
100 101 102 |
# File 'lib/state_machines/node_collection.rb', line 100 def update(node) @index_names.each { |name| update_index(name, node) } end |