Merged in SimpleCov from master#
[graph.njae.git] / lib / graph.njae / vertex.rb
index 4a75b6df2c628c0cba8a25fead7aff670a1c1563..def0b66d79a81ff2b0348655d1459cab1720aefe 100644 (file)
@@ -1,24 +1,22 @@
-require 'ostruct'
-
 # A simple graph library
 
 module GraphNjae
   # A vertex in a graph. The edge can have arbitrary attributes,treated as 
   # method names.
   class Vertex < OpenStruct
-    def initialize
-      super
+    def initialize(values = {})
+      super(values)
       self.edges = []
       self
     end
     
     # Connect this vertex to another, creating an Edge to do so, and returning
     # the Edge
-    def connect(other)
-      e = Edge.new
+    def connect(other, edge_attributes = {})
+      e = Edge.new edge_attributes
       e << self << other
-      self.edges << e
-      other.edges << e unless self === other
+      self.edges << e
+      other.edges << e unless self === other
       e
     end
     
@@ -39,5 +37,25 @@ module GraphNjae
                       e.vertices.drop_while {|v| v != self}[1..-1]}.flatten
     end
     
+    def to_s
+      '<V: ' + self.name.to_s + '>'
+    end
+    
+    def to_dot(opts = {})
+      if block_given?
+        yield self
+      else
+        dot = self.object_id.to_s
+        if opts.size > 0
+          dot << ' {'
+          dot << opts.keys.map { |k|
+            (k.to_s + ' = "' + self.instance_eval(opts[k].to_s).to_s) + '"'
+                        }.join(', ')
+          dot << '}'
+        end
+        dot << ';'
+      end
+    end
+    
   end
 end