Skip to content

Commit

Permalink
Allow graphs to add vertices. Part of vertex operations for #3 and cl…
Browse files Browse the repository at this point in the history
…oses #1
  • Loading branch information
D4L committed Jan 2, 2013
1 parent 2fc6869 commit 96b3d43
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/abstract_graph/composition.rb
Expand Up @@ -2,6 +2,7 @@

require "abstract_graph/composition/vertex"
require "abstract_graph/composition/edge"
require "abstract_graph/composition/uniquenamecollection"

module AbstractGraph
module Composition
Expand Down
17 changes: 17 additions & 0 deletions 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"
19 changes: 19 additions & 0 deletions 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
13 changes: 13 additions & 0 deletions 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
13 changes: 10 additions & 3 deletions lib/abstract_graph/graph.rb
Expand Up @@ -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"
16 changes: 16 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions lib/abstract_graph/graph/initialize.rb
Expand Up @@ -6,6 +6,8 @@ class Graph
# public constructor
def initialize

@vertices = UniqueNameCollection.new

end

end
Expand Down
40 changes: 40 additions & 0 deletions 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
@@ -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
8 changes: 8 additions & 0 deletions spec/abstract_graph/composition/uniquenamecollection_spec.rb
@@ -0,0 +1,8 @@
require 'spec_helper'

module AbstractGraph
module Composition
describe UniqueNameCollection do
end
end
end
29 changes: 29 additions & 0 deletions 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
4 changes: 4 additions & 0 deletions spec/dummy_helper.rb
@@ -0,0 +1,4 @@
# simple class that implements #name
class Dummy
attr_accessor :name
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -1,3 +1,4 @@
require 'dummy_helper'
require 'rspec'
require 'abstract_graph'

Expand Down

0 comments on commit 96b3d43

Please sign in to comment.