Handles subclasses of Vertex and Edge
authorNeil Smith <neil.github@njae.me.uk>
Fri, 23 Sep 2011 21:25:10 +0000 (22:25 +0100)
committerNeil Smith <neil.github@njae.me.uk>
Fri, 23 Sep 2011 21:25:10 +0000 (22:25 +0100)
lib/graph.njae/graph.rb
spec/graph/graph_spec.rb

index 1da5297d1f74861b24ce417dc2ac6c0ff3181111..e08a1ad8544d12aec4cacf3a095ca5e1e5df6aea 100644 (file)
@@ -16,9 +16,9 @@ module GraphNjae
     
     # Add a Vertex or Edge to the graph.
     def <<(other)
-      if other.class == Vertex
+      if other.class.ancestors.include? Vertex
         self.vertices << other
-      elsif
+      elsif other.class.ancestors.include? Edge
         self.edges << other
       end
       self
index ba7bea13cf1bcc87ddc3975736eb30f646e0223e..09cc6a433cddd36d28d78fa4c09fc9f2542450da 100644 (file)
@@ -1,6 +1,13 @@
 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
 
 module GraphNjae
+
+  class SVertex < Vertex
+  end
+  
+  class SEdge < Edge
+  end
+  
   describe Graph do
     let (:g) { Graph.new }
     describe "#initialize" do
@@ -54,6 +61,16 @@ module GraphNjae
         g.edges.should include(e1)
         g.edges.should include(e2)
       end
+      
+      it "adds a subclass of Vertex" do
+        g.vertices.should be_empty
+        v1 = SVertex.new
+        v2 = SVertex.new
+        g << v1 << v2
+        g.should have(2).vertices
+        g.vertices.should include(v1)
+        g.vertices.should include(v2)        
+      end
     end # #<<
 
     describe "connect" do