Class: AbstractGraph::Composition::UniqueNameCollection
- Inherits:
-
Object
- Object
- AbstractGraph::Composition::UniqueNameCollection
- Defined in:
- lib/abstract_graph/composition/unique_name_collection.rb,
lib/abstract_graph/composition/unique_name_collection/add.rb,
lib/abstract_graph/composition/unique_name_collection/dup.rb,
lib/abstract_graph/composition/unique_name_collection/each.rb,
lib/abstract_graph/composition/unique_name_collection/find.rb,
lib/abstract_graph/composition/unique_name_collection/link.rb,
lib/abstract_graph/composition/unique_name_collection/delete.rb,
lib/abstract_graph/composition/unique_name_collection/rename.rb,
lib/abstract_graph/composition/unique_name_collection/ticket.rb,
lib/abstract_graph/composition/unique_name_collection/initialize.rb,
lib/abstract_graph/composition/unique_name_collection/ticket/get.rb,
lib/abstract_graph/composition/unique_name_collection/ticket/set.rb,
lib/abstract_graph/composition/unique_name_collection/method_missing.rb,
lib/abstract_graph/composition/unique_name_collection/ticket/initialize.rb
Overview
public UniqueNameCollection class Note that the collection of this
class must implement #name
Defined Under Namespace
Classes: Ticket
Constant Summary collapse
- @@namespace =
{}
- @@namespace_counter =
0
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#namespace_ticket ⇒ Object
Returns the value of attribute namespace_ticket.
Instance Method Summary collapse
-
#add(o) ⇒ Object
d: Adds the object to our UNC.
-
#delete(name) ⇒ Object
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.
-
#dup ⇒ Object
d: Deep copies our object.
-
#each(&block) ⇒ Object
d: Iterate through all objects.
-
#find(name) ⇒ Object
d: Finds a stored object given the name.
-
#initialize ⇒ UniqueNameCollection
constructor
d: Initializes the UNC.
-
#link(unc) ⇒ Object
d: Links the UNC so they have unique namespace.
-
#method_missing(m, *args, &block) ⇒ Object
d: Handles everything.
-
#rename(oldname, newname) ⇒ Object
d: Change the name of a vertex in our graph.
Constructor Details
#initialize ⇒ UniqueNameCollection
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
13 14 15 16 17 18 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
d: Handles everything. a: Pass all methods into the hash. t: p: r:
12 13 14 |
# 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 |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
10 11 12 |
# File 'lib/abstract_graph/composition/unique_name_collection.rb', line 10 def collection @collection end |
#namespace_ticket ⇒ Object
Returns the value of attribute namespace_ticket.
11 12 13 |
# File 'lib/abstract_graph/composition/unique_name_collection.rb', line 11 def namespace_ticket @namespace_ticket end |
Instance Method Details
#add(o) ⇒ Object
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
13 14 15 16 17 18 19 |
# 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) ⇒ Object
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
12 13 14 15 16 |
# 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 ⇒ Object
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
13 14 15 16 17 18 19 20 |
# 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) ⇒ Object
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
12 13 14 |
# File 'lib/abstract_graph/composition/unique_name_collection/each.rb', line 12 def each( &block ) @collection.each_value( &block ) end |
#find(name) ⇒ Object
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
12 13 14 |
# File 'lib/abstract_graph/composition/unique_name_collection/find.rb', line 12 def find(name) @collection[name] end |
#link(unc) ⇒ Object
d: Links the UNC so they have unique namespace. a: check the intersection of namespace so that there’s no collision, then
reset the namespace to contain the shared namespaces and update tickets
t: |intersection and size of ticket| p: UniqueNameCollection unc is the other collection we want to link r: false if the two collections are not already mutually unique
UNC if it succeeds
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/abstract_graph/composition/unique_name_collection/link.rb', line 14 def link( unc ) our_ticket = @namespace_ticket.get other_ticket = unc.namespace_ticket.get our_namespace = @@namespace[our_ticket] other_namespace = @@namespace[other_ticket] namespace_intersection = our_namespace[0] & other_namespace[0] return nil unless namespace_intersection.empty? # Create the assumption that |our_namespace| >= |other_namespace| if other_namespace[0].size > our_namespace[0].size our_namespace, other_namespace = other_namespace, our_namespace our_ticket, other_ticket = other_ticket, our_ticket end # Assume it is fine to link now, add to the larger namespace our_namespace[0].merge other_namespace[0] other_namespace[1].map do |ticket| ticket.set our_ticket end our_namespace[1].merge other_namespace[1] @@namespace.delete other_ticket self end |
#rename(oldname, newname) ⇒ Object
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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 |