Version bump to 0.2.0
[graph.njae.git] / lib / graph / edge.rb
1 require 'ostruct'
2
3 # A simple graph library
4
5 module Graph
6
7 # An edge (or multiedge) in a graph. The edge can have arbitrary attributes,
8 # treated as method names.
9 #
10 # Each connection is handled by a Graph::Connection object, so that each end
11 # of the Edge can have it's own attributes.
12 class Edge < OpenStruct
13 def initialize
14 super
15 self.connections = []
16 self
17 end
18
19 # Connect this edge to a vertex
20 def <<(other)
21 c = Connection.new
22 c.end = other
23 self.connections << c
24 self
25 end
26
27 # Return the set of vertices this edge connects.
28 def vertices
29 self.connections.map {|c| c.end}
30 end
31
32 # Return the connection object that joins this Edge to the specified Vertex
33 def connection_at(vertex)
34 self.connections.select {|c| c.end.equal? vertex}.first
35 end
36 end
37
38 # A connection between an Edge and a Vertex.The connection can have arbitrary attributes,
39 # treated as method names.
40 class Connection < OpenStruct
41 def initialize
42 super
43 self
44 end
45 end
46 end