Class: AbstractGraph::Composition::UniqueNameCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_graph/composition/uniquenamecollection.rb,
lib/abstract_graph/composition/uniquenamecollection/add.rb,
lib/abstract_graph/composition/uniquenamecollection/dup.rb,
lib/abstract_graph/composition/uniquenamecollection/link.rb,
lib/abstract_graph/composition/uniquenamecollection/rename.rb,
lib/abstract_graph/composition/uniquenamecollection/initialize.rb,
lib/abstract_graph/composition/uniquenamecollection/method_missing.rb

Overview

public UniqueNameCollection class Note that the collection of this

class must implement #name

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUniqueNameCollection

Returns a new instance of UniqueNameCollection.



7
8
9
10
# File 'lib/abstract_graph/composition/uniquenamecollection/initialize.rb', line 7

def initialize
  @collection = Hash.new
  @otherUnique = [self]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

pass all methods into the hash



8
9
10
# File 'lib/abstract_graph/composition/uniquenamecollection/method_missing.rb', line 8

def method_missing( m, *args, &block )
  @collection.send( m.to_sym, *args, &block )
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



10
11
12
# File 'lib/abstract_graph/composition/uniquenamecollection.rb', line 10

def collection
  @collection
end

#otherUniqueObject

Returns the value of attribute otherUnique.



11
12
13
# File 'lib/abstract_graph/composition/uniquenamecollection.rb', line 11

def otherUnique
  @otherUnique
end

Instance Method Details

#add(o) ⇒ Object

adds an object o into the collection p: Object o that implements #name



9
10
11
12
13
14
15
# File 'lib/abstract_graph/composition/uniquenamecollection/add.rb', line 9

def add( o )
  # note that otherUnique includes ourself
  @otherUnique.each do |unc|
    raise IndexError if unc[o.name]
  end
  @collection[o.name] = o
end

#dupObject

does a deep copy of the object, in otherwords

copies every object in the collection


9
10
11
12
13
14
15
16
# File 'lib/abstract_graph/composition/uniquenamecollection/dup.rb', line 9

def dup
  other = self.class.new
  # copy each object in our collection over
  @collection.each_value do |o|
    other.add o
  end
  other
end

links two collections together so that their names

will be mutually unique

p: UniqueNameCollection unc is the other collection

we want to link

r: false if the two collections are not already

mutually unique


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/abstract_graph/composition/uniquenamecollection/link.rb', line 13

def link( unc )
  # note that either otherUnique may be alot
  combinedUnique = @otherUnique | unc.otherUnique

  # check if there are already any duplicates
  allNames = []
  combinedUnique.each do |tunc|
    return nil if not ( tunc.keys & allNames ).empty?
    allNames |= tunc.keys
  end

  # set each one to have the same uniquespace
  combinedUnique.each do |tunc|
    tunc.otherUnique = combinedUnique
  end
end

#rename(oldname, newname) ⇒ Object

Change the name of a vertex in our graph p: String oldname represents the current vertex’s name

String newname represents the new name of our vertex

note: the object needs to implement #name=



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/abstract_graph/composition/uniquenamecollection/rename.rb', line 11

def rename( oldname, newname )
  return nil if @collection[oldname].nil?

  @otherUnique.each do |unc|
    throw Exception if unc[newname]
  end

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