Class: StateMachine::NodeCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Assertions
Defined in:
lib/state_machine/node_collection.rb

Overview

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

Direct Known Subclasses

EventCollection, StateCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assert_exclusive_keys, #assert_valid_keys

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



19
20
21
22
23
24
25
26
27
28
# 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
  @contexts = []
end

Instance Attribute Details

#machineObject

The machine associated with the nodes



10
11
12
# File 'lib/state_machine/node_collection.rb', line 10

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.



80
81
82
83
84
85
# File 'lib/state_machine/node_collection.rb', line 80

def <<(node)
  @nodes << node
  @indices.each {|attribute, index| index[value(node, attribute)] = 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.



146
147
148
149
150
151
152
153
# File 'lib/state_machine/node_collection.rb', line 146

def [](key, index_name = @default_index)
  index = self.index(index_name)
  if index.include?(key)
    index[key]
  elsif @indices.include?(:"#{index_name}_to_s")
    self[key.to_s, :"#{index_name}_to_s"]
  end
end

#at(index) ⇒ Object

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


132
133
134
# File 'lib/state_machine/node_collection.rb', line 132

def at(index)
  @nodes[index]
end

#concat(nodes) ⇒ Object

Appends a group of nodes to the collection



88
89
90
# File 'lib/state_machine/node_collection.rb', line 88

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.



67
68
69
70
71
72
73
74
75
# File 'lib/state_machine/node_collection.rb', line 67

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

#eachObject

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 --


119
120
121
122
# File 'lib/state_machine/node_collection.rb', line 119

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


167
168
169
# File 'lib/state_machine/node_collection.rb', line 167

def fetch(key, index_name = @default_index)
  self[key, index_name] || raise(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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/state_machine/node_collection.rb', line 32

def initialize_copy(orig) #:nodoc:
  super
  
  nodes = @nodes
  contexts = @contexts
  @nodes = []
  @contexts = []
  @indices = @indices.inject({}) {|indices, (name, index)| 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



60
61
62
# File 'lib/state_machine/node_collection.rb', line 60

def keys(index_name = @default_index)
  index(index_name).keys
end

#lengthObject

Gets the number of nodes in this collection



55
56
57
# File 'lib/state_machine/node_collection.rb', line 55

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.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/state_machine/node_collection.rb', line 95

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