From ae1a87dd7453fbafe86a4d9e5113af7439e5981f Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 22 Sep 2011 09:11:17 +0100 Subject: [PATCH] Tidied up, added comments, created documentation. --- Gemfile | 3 ++- Rakefile | 10 ++++++---- lib/graph/edge.rb | 13 +++++++++++++ lib/graph/edge.rb~ | 1 + lib/graph/graph.rb | 6 ++++++ lib/graph/graph.rb~ | 1 + lib/graph/vertex.rb | 9 +++++++++ lib/graph/vertex.rb~ | 1 + 8 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 8606a14..d6b498f 100644 --- a/Gemfile +++ b/Gemfile @@ -6,8 +6,9 @@ source "http://rubygems.org" # Add dependencies to develop your gem here. # Include everything needed to run rake, tests, features, etc. group :development do - gem "rspec", "~> 2.3.0" + gem "rspec", "~> 2.6.0" gem "bundler", "~> 1.0.0" gem "jeweler", "~> 1.6.2" gem "rcov", ">= 0" + gem "rdoc" end diff --git a/Rakefile b/Rakefile index 07112cf..0c3452e 100644 --- a/Rakefile +++ b/Rakefile @@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem| gem.name = "graph.njae" gem.homepage = "http://github.com/NeilNjae/graph.njae" gem.license = "MIT" - gem.summary = %Q{TODO: one-line summary of your gem} - gem.description = %Q{TODO: longer description of your gem} + gem.summary = %Q{A simple graph library} + gem.description = %Q{A simple graph library} gem.email = "neil.github@njae.me.uk" gem.authors = ["Neil Smith"] # dependencies defined in Gemfile @@ -38,8 +38,10 @@ end task :default => :spec -require 'rake/rdoctask' -Rake::RDocTask.new do |rdoc| +# require 'rake/rdoctask' +# Rake::RDocTask.new do |rdoc| +require 'rdoc/task' +RDoc::Task.new do |rdoc| version = File.exist?('VERSION') ? File.read('VERSION') : "" rdoc.rdoc_dir = 'rdoc' diff --git a/lib/graph/edge.rb b/lib/graph/edge.rb index c579a2f..e783d35 100644 --- a/lib/graph/edge.rb +++ b/lib/graph/edge.rb @@ -1,6 +1,14 @@ require 'ostruct' +# A simple graph library + module Graph + + # An edge (or multiedge) in a graph. The edge can have arbitrary attributes, + # treated as method names. + # + # Each connection is handled by a Graph::Connection object, so that each end + # of the Edge can have it's own attributes. class Edge < OpenStruct def initialize super @@ -8,6 +16,7 @@ module Graph self end + # Connect this edge to a vertex def <<(other) c = Connection.new c.end = other @@ -15,15 +24,19 @@ module Graph self end + # Return the set of vertices this edge connects. def vertices self.connections.map {|c| c.end} end + # Return the connection object that joins this Edge to the specified Vertex def connection_at(vertex) self.connections.select {|c| c.end.equal? vertex}.first end end + # A connection between an Edge and a Vertex.The connection can have arbitrary attributes, + # treated as method names. class Connection < OpenStruct def initialize super diff --git a/lib/graph/edge.rb~ b/lib/graph/edge.rb~ index 10f133e..c579a2f 100644 --- a/lib/graph/edge.rb~ +++ b/lib/graph/edge.rb~ @@ -27,6 +27,7 @@ module Graph class Connection < OpenStruct def initialize super + self end end end diff --git a/lib/graph/graph.rb b/lib/graph/graph.rb index c9d83b1..25c95cc 100644 --- a/lib/graph/graph.rb +++ b/lib/graph/graph.rb @@ -1,6 +1,11 @@ require 'ostruct' +# A simple graph library + module Graph + + # A container for all the parts of a graph. The graph can have arbitrary attributes, + # treated as method names. class Graph < OpenStruct def initialize super @@ -9,6 +14,7 @@ module Graph self end + # Add a Vertex or Edge to the graph. def <<(other) if other.class == Vertex self.vertices << other diff --git a/lib/graph/graph.rb~ b/lib/graph/graph.rb~ index 0201c84..c9d83b1 100644 --- a/lib/graph/graph.rb~ +++ b/lib/graph/graph.rb~ @@ -6,6 +6,7 @@ module Graph super self.edges = Array.new self.vertices = Array.new + self end def <<(other) diff --git a/lib/graph/vertex.rb b/lib/graph/vertex.rb index 0bccb57..cb315e7 100644 --- a/lib/graph/vertex.rb +++ b/lib/graph/vertex.rb @@ -1,6 +1,10 @@ require 'ostruct' +# A simple graph library + module Graph + # A vertex in a graph. The edge can have arbitrary attributes,treated as + # method names. class Vertex < OpenStruct def initialize super @@ -8,6 +12,8 @@ module Graph self end + # Connect this vertex to another, creating an Edge to do so, and returning + # the Edge def connect(other) e = Edge.new e << self << other @@ -16,11 +22,14 @@ module Graph e end + # Connect this vertex to another, creating an Edge to do so, and returning + # this Vertex def <<(other) connect(other) self end + # Return the set of neighbouring vertices def neighbours vertices = self.edges.map {|e| e.vertices}.flatten vertices_to_me = vertices.select {|v| v == self} diff --git a/lib/graph/vertex.rb~ b/lib/graph/vertex.rb~ index 6a85373..0bccb57 100644 --- a/lib/graph/vertex.rb~ +++ b/lib/graph/vertex.rb~ @@ -5,6 +5,7 @@ module Graph def initialize super self.edges = [] + self end def connect(other) -- 2.34.1