class AbstractGraph::Composition::UniqueNameCollection

public UniqueNameCollection class Note that the collection of this

class must implement #name

Attributes

collection[RW]
namespace_ticket[RW]

Public Class Methods

new() click to toggle source

d: Initializes the UNC. a: Assign itself a namespace_ticket and start the collection by adding

the namespace a set of names and backreference of tickets

t: constant p: r: new UNC

# File lib/abstract_graph/composition/unique_name_collection/initialize.rb, line 13
def initialize
  @collection = Hash.new
  @@namespace_counter += 1
  @namespace_ticket = Ticket.new(@@namespace_counter)
  @@namespace[@@namespace_counter] = [Set.new, Set.new([@namespace_ticket])]
end

Public Instance Methods

add( o ) click to toggle source

d: Adds the object to our UNC. a: Ensure that our namespace doesn’t already have the object name and

then add the object to namespace and collection.

t: constant p: Object o that implements name r: The object o itself

# File lib/abstract_graph/composition/unique_name_collection/add.rb, line 13
def add( o )
  set = @@namespace[@namespace_ticket.get][0]
  raise IndexError if set.include? o.name
  set << o.name
  @collection[o.name] = o
  self
end
delete( name ) click to toggle source

d: Delete the object from the collection a: Delete the object from the collection and from the nameserver t: constant p: name of object we want to delete r: object that was deleted

# File lib/abstract_graph/composition/unique_name_collection/delete.rb, line 12
def delete( name )
  return nil unless @collection[name]
  @@namespace[@namespace_ticket.get][0].delete name
  @collection.delete name
end
dup() click to toggle source

d: Deep copies our object. a: Does a deep copy of the object, in otherwords

copies every object in the collection.

t: |collection| p: r: the copy of the UNC

# File lib/abstract_graph/composition/unique_name_collection/dup.rb, line 13
def dup
  other = self.class.new
  # copy each object in our collection over
  @collection.each_value do |o|
    other.add o.dup
  end
  other
end
each( &block ) click to toggle source

d: Iterate through all objects. a: Enumerates through every object. t: constant p: block that we pass in each object r: Enumerable if no block, collection if yes block

# File lib/abstract_graph/composition/unique_name_collection/each.rb, line 12
def each( &block )
  @collection.each_value( &block )
end
find(name) click to toggle source

d: Finds a stored object given the name. a: Goes into collection and retrieves based on key. t: constant p: name of object r: object

# File lib/abstract_graph/composition/unique_name_collection/find.rb, line 12
def find(name)
  @collection[name]
end
method_missing( m, *args, &block ) click to toggle source

d: Handles everything. a: Pass all methods into the hash. t: p: r:

# File lib/abstract_graph/composition/unique_name_collection/method_missing.rb, line 12
def method_missing( m, *args, &block )
  @collection.values.send( m.to_sym, *args, &block )
end
rename( oldname, newname ) click to toggle source

d: Change the name of a vertex in our graph. a: Check the namespace otherwise, set collection and add to namespace t: constant p: String oldname represents the current vertex’s name

String newname represents the new name of our vertex

r: UNC itself

# File lib/abstract_graph/composition/unique_name_collection/rename.rb, line 13
def rename( oldname, newname )
  return nil unless @collection.has_key? oldname

  nameserver = @@namespace[@namespace_ticket.get][0]
  throw Exception if nameserver.include? newname

  # change the nameserver
  nameserver.delete oldname
  nameserver << newname

  # rename the object itself
  @collection[oldname].name = newname
  # remap the name
  @collection[newname] = @collection[oldname]
  # clear the old name
  @collection.delete oldname
  self
end