# 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
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
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'
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
self
end
+ # Connect this edge to a vertex
def <<(other)
c = Connection.new
c.end = other
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
class Connection < OpenStruct
def initialize
super
+ self
end
end
end
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
self
end
+ # Add a Vertex or Edge to the graph.
def <<(other)
if other.class == Vertex
self.vertices << other
super
self.edges = Array.new
self.vertices = Array.new
+ self
end
def <<(other)
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
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
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}
def initialize
super
self.edges = []
+ self
end
def connect(other)