Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow graphs to add vertices. Part of vertex operations for #3 and cl…

…oses #1
  • Loading branch information...
commit 96b3d43cb4725767253432395a1a5c8b910d5a2a 1 parent 2fc6869
@D4L authored
View
1  lib/abstract_graph/composition.rb
@@ -2,6 +2,7 @@
require "abstract_graph/composition/vertex"
require "abstract_graph/composition/edge"
+require "abstract_graph/composition/uniquenamecollection"
module AbstractGraph
module Composition
View
17 lib/abstract_graph/composition/uniquenamecollection.rb
@@ -0,0 +1,17 @@
+# required in "abstract_graph/composition"
+
+module AbstractGraph
+ module Composition
+
+ # public UniqueNameCollection class
+ # Note that the collection of this
+ # class must implement #name
+ class UniqueNameCollection
+ attr_accessor :collection
+
+ end
+ end
+end
+
+require "abstract_graph/composition/uniquenamecollection/initialize"
+require "abstract_graph/composition/uniquenamecollection/add"
View
19 lib/abstract_graph/composition/uniquenamecollection/add.rb
@@ -0,0 +1,19 @@
+# required in "abstract_graph/composition/uniquenamecollection"
+
+module AbstractGraph
+ module Composition
+ class UniqueNameCollection
+
+ # adds an object o into the collection
+ # p: Object o that implements #name
+ def add( o )
+ if @collection[o.name]
+ raise IndexError
+ else
+ @collection[o.name] = o
+ end
+ end
+
+ end
+ end
+end
View
13 lib/abstract_graph/composition/uniquenamecollection/initialize.rb
@@ -0,0 +1,13 @@
+# required in "abstract_graph/composition/uniquenamecollection"
+
+module AbstractGraph
+ module Composition
+ class UniqueNameCollection
+
+ def initialize
+ @collection = Hash.new
+ end
+
+ end
+ end
+end
View
13 lib/abstract_graph/graph.rb
@@ -2,10 +2,17 @@
module AbstractGraph
- # public Graph class
- class Graph
- end
+ # public Graph class
+ class Graph
+
+ # Add the vertex and edge classes
+ include Composition
+
+ attr_accessor :vertices
+
+ end
end
require "abstract_graph/graph/initialize"
+require "abstract_graph/graph/add_vertex"
View
16 lib/abstract_graph/graph/add_vertex.rb
@@ -0,0 +1,16 @@
+# required in "abstract_graph/graph"
+
+module AbstractGraph
+ class Graph
+
+ # add a vertex named s to the graph
+ # p: String s represents the name of the wanted vertex
+ def add_vertex( s )
+ # create the vertex
+ vertex = Vertex.new s
+ @vertices.add vertex
+ self
+ end
+
+ end
+end
View
2  lib/abstract_graph/graph/initialize.rb
@@ -6,6 +6,8 @@ class Graph
# public constructor
def initialize
+ @vertices = UniqueNameCollection.new
+
end
end
View
40 spec/abstract_graph/composition/uniquenamecollection/add_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe UniqueNameCollection do
+
+ before(:each) do
+
+ @collection = UniqueNameCollection.new
+ end
+
+ describe "#add(Object)" do
+
+ it "tracks an object that has implemented #name" do
+ dummy = Dummy.new
+ dummy.name = "Hello"
+ @collection.add dummy
+ end
+
+ it "doesn't allow us to add the same object twice" do
+ dummy = Dummy.new
+ dummy.name = "Hello"
+ @collection.add dummy
+ expect { @collection.add dummy }.to raise_error
+ end
+
+ it "doesn't allow two objects to have the same name" do
+ dummy = Dummy.new
+ dummy2 = Dummy.new
+ dummy.name = "Hello"
+ dummy2.name = "Hello"
+ @collection.add dummy
+ expect { @collection.add dummy2 }.to raise_error
+ end
+
+ end
+
+ end
+ end
+end
View
21 spec/abstract_graph/composition/uniquenamecollection/initialize_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe UniqueNameCollection do
+
+ before (:each) do
+ @collection = UniqueNameCollection.new
+ end
+
+ describe "#new" do
+
+ it "returns an object of class UniqueNameCollection" do
+ @collection.should be_an_instance_of(UniqueNameCollection)
+ end
+
+ end
+
+ end
+ end
+end
View
8 spec/abstract_graph/composition/uniquenamecollection_spec.rb
@@ -0,0 +1,8 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe UniqueNameCollection do
+ end
+ end
+end
View
29 spec/abstract_graph/graph/add_vertex_spec.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+module AbstractGraph
+ describe Graph do
+
+ before :each do
+ @graph = Graph.new
+ end
+
+ describe "#add_vertex(String)" do
+
+ it "returns an object of class Graph" do
+ @graph.add_vertex( "MyVertex" ).should be_an_instance_of(Graph)
+ end
+
+ it "throws an exception when adding vertex of existing name" do
+ @graph.add_vertex( "MyVertex" )
+ expect { @graph.add_vertex( "MyVertex" ) }.to raise_error
+ end
+
+ it "allows two different graphs to have the same names" do
+ graph2 = Graph.new
+ @graph.add_vertex( "MyVertex" )
+ expect { graph2.add_vertex( "MyVertex" ) }.to_not raise_error
+ end
+
+ end
+ end
+end
View
4 spec/dummy_helper.rb
@@ -0,0 +1,4 @@
+# simple class that implements #name
+class Dummy
+ attr_accessor :name
+end
View
1  spec/spec_helper.rb
@@ -1,3 +1,4 @@
+require 'dummy_helper'
require 'rspec'
require 'abstract_graph'
Please sign in to comment.
Something went wrong with that request. Please try again.