X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=lib%2Fgraph.njae%2Fedge.rb;fp=lib%2Fgraph.njae%2Fedge.rb;h=6581033d2a885bbe4fe8beddf1eddfb38879b50a;hb=c34def40508c16cfc815fa02c8743d071491af7b;hp=0000000000000000000000000000000000000000;hpb=0908114d61c0effab6a568dd47053244df92cde8;p=graph.njae.git

diff --git a/lib/graph.njae/edge.rb b/lib/graph.njae/edge.rb
new file mode 100644
index 0000000..6581033
--- /dev/null
+++ b/lib/graph.njae/edge.rb
@@ -0,0 +1,46 @@
+require 'ostruct'
+
+# A simple graph library
+
+module GraphNjae
+  
+  # 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.connections = []
+      self
+    end
+    
+    # Connect this edge to a vertex
+    def <<(other)
+      c = Connection.new
+      c.end = other
+      self.connections << c
+      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
+      self
+    end
+  end
+end