Reads ERDs
authorNeil Smith <neil.github@njae.me.uk>
Mon, 26 Sep 2011 17:45:11 +0000 (18:45 +0100)
committerNeil Smith <neil.github@njae.me.uk>
Mon, 26 Sep 2011 17:45:11 +0000 (18:45 +0100)
Gemfile
Gemfile.lock
lib/erd_handler/erd.rb
lib/erd_handler/link.rb
spec/erd_handler/erd_spec.rb

diff --git a/Gemfile b/Gemfile
index 6444095e18a891aa94d4ebf28f5ceb29bb29b49c..116c2fcd6ac2abbead5e317c6beedf478dbc8310 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -10,5 +10,5 @@ group :development do
   gem "bundler", "~> 1.0.0"
   gem "rdoc"
   gem "rake"
   gem "bundler", "~> 1.0.0"
   gem "rdoc"
   gem "rake"
-  gem "graph.njae", "0.2.1"
+  gem "graph.njae", "0.2.3"
 end
 end
index dca8027824dd9dd8416dc35041a55fcbf4d452a0..9962be5260c360de0a6f9ffd558cbb804abd9731 100644 (file)
@@ -2,7 +2,7 @@ GEM
   remote: http://rubygems.org/
   specs:
     diff-lcs (1.1.2)
   remote: http://rubygems.org/
   specs:
     diff-lcs (1.1.2)
-    graph.njae (0.2.1)
+    graph.njae (0.2.3)
     rake (0.9.2)
     rdoc (3.7)
     rspec (2.6.0)
     rake (0.9.2)
     rdoc (3.7)
     rspec (2.6.0)
@@ -19,7 +19,7 @@ PLATFORMS
 
 DEPENDENCIES
   bundler (~> 1.0.0)
 
 DEPENDENCIES
   bundler (~> 1.0.0)
-  graph.njae (= 0.2.1)
+  graph.njae (= 0.2.3)
   rake
   rdoc
   rspec (~> 2.6.0)
   rake
   rdoc
   rspec (~> 2.6.0)
index f7e246a0d3581a7c62c747f5575e7956b038d522..142f575d8851044d9299090b069599526aad7480 100644 (file)
@@ -17,6 +17,9 @@ module ErdHandler
       doc.elements.each('Drawing/link') do |link_element|
         self << Link.new(link_element, self.vertices)
       end
       doc.elements.each('Drawing/link') do |link_element|
         self << Link.new(link_element, self.vertices)
       end
+      doc.elements.each('Drawing/selfLink') do |link_element|
+        self << Link.new(link_element, self.vertices)
+      end
       self
     end
   end
       self
     end
   end
index eec4341e50d67a74ef88c09944e94213303e5632..4a7b181c0eb808eb7fcea507af087c21a4ba7743 100644 (file)
@@ -10,18 +10,22 @@ module ErdHandler
       self.id = link_element.attributes["id"].to_i
       self.mark = link_element.attributes["mark"].to_f
       self.name = link_element.elements['moveableName'].attributes['name']
       self.id = link_element.attributes["id"].to_i
       self.mark = link_element.attributes["mark"].to_f
       self.name = link_element.elements['moveableName'].attributes['name']
- puts "vertices = #{vertices}"      
-      box1 = vertices.select {|v| v.id == link_element.elements['box1'].attributes['id']}[0]
- puts "found box1: #{box1}"     
-      box2 = vertices.select {|v| v.id == link_element.elements['box2'].attributes['id']}[0]
- puts "found box2: #{box2}"
+      box1 = vertices.select {|v| v.id == link_element.elements['box1'].attributes['id'].to_i}[0]
+      box2 = vertices.select {|v| v.id == link_element.elements['box2'].attributes['id'].to_i}[0]
       self << box1 << box2
       self << box1 << box2
-      c1 = self.connections.find {|c| c.end == box1}
-      c1.blob = link_element.elements['box1EndAdornments'].attributes['blob']
-      c1.crowsfoot = link_element.elements['box1EndAdornments'].attributes['crowsfoot']
-      c2 = self.connections.find {|c| c.end == box2}
-      c2.blob = link_element.elements['box2EndAdornments'].attributes['blob']
-      c2.crowsfoot = link_element.elements['box2EndAdornments'].attributes['crowsfoot']
+      box1.edges << self
+      box2.edges << self unless box1 == box2
+      if box1 == box2
+        c1 = self.connections[0]
+        c2 = self.connections[1]
+      else
+        c1 = self.connections.find {|c| c.end == box1}
+        c2 = self.connections.find {|c| c.end == box2}
+      end
+      c1.blob = link_element.elements['box1EndAdornments'].attributes['blob'].downcase.intern
+      c1.crowsfoot = link_element.elements['box1EndAdornments'].attributes['crowsfoot'].downcase.intern
+      c2.blob = link_element.elements['box2EndAdornments'].attributes['blob'].downcase.intern
+      c2.crowsfoot = link_element.elements['box2EndAdornments'].attributes['crowsfoot'].downcase.intern
     end
   end
 end
     end
   end
 end
index 12ef23972e4674b8580e2b03eb6cb40c69797243..0c32a815805b1c7afcd93be5809f4ac88b5ba8f8 100644 (file)
@@ -20,18 +20,120 @@ module ErdHandler
         erd.should have(1).vertices
         erd.should have(0).edges
       end
         erd.should have(1).vertices
         erd.should have(0).edges
       end
-      
+    end # #initialize
+    
+    describe "#read" do
+      it "reads and creates a single box" do
+        erd = Erd.new
+        erd.read(File.new("spec/fixtures/single_box_erd.xml"))
+        erd.mark.should == 6.5
+        erd.should have(1).vertices
+        erd.should have(0).edges
+      end
+
       it "reads and creates two boxes with an edge joining them" do
       it "reads and creates two boxes with an edge joining them" do
-        erd = Erd.new(File.new("spec/fixtures/two_boxes_one_link_erd.xml"))
+        erd = Erd.new
+        erd.read(File.new("spec/fixtures/two_boxes_one_link_erd.xml"))
         erd.mark.should == 4.5
         erd.should have(2).vertices
         erd.should have(1).edges
         erd.mark.should == 4.5
         erd.should have(2).vertices
         erd.should have(1).edges
-        
+
+        erd.vertices[0].should have(1).neighbours
+        erd.vertices[1].should have(1).neighbours
         erd.vertices[0].neighbours.should include(erd.vertices[1])
         erd.vertices[0].neighbours.should include(erd.vertices[1])
-        erd.vertices[1].neighbours.should include(erd.vertices[0])        
+        erd.vertices[1].neighbours.should include(erd.vertices[0])
+        
+        link = erd.edges[0]
+        b0 = erd.vertices.find {|v| v.id == 0}
+        b1 = erd.vertices.find {|v| v.id == 1}
+        link.connection_at(b0).blob.should be(:closed)
+        link.connection_at(b0).crowsfoot.should be(:yes)
+        link.connection_at(b1).blob.should be(:open)
+        link.connection_at(b1).crowsfoot.should be(:no)
       end
       
       end
       
-      it "reads and creates a box with a self-loop"
-    end # #initialize
+      it "reads and creates a box with a self-loop" do
+        erd = Erd.new
+        erd.read(File.new("spec/fixtures/single_box_self_loop_erd.xml"))
+        erd.mark.should == 4.5
+        erd.should have(1).vertices
+        erd.should have(1).edges
+
+        erd.vertices[0].should have(1).neighbours
+        erd.vertices[0].neighbours.should include(erd.vertices[0])
+        
+        link = erd.edges[0]
+        box = erd.vertices[0]
+        c1 = link.connections.find {|c| c.blob == :closed}
+        c1.crowsfoot.should be(:yes)
+        c2 = link.connections.find {|c| c.blob == :open}
+        c2.crowsfoot.should be(:no)
+        c1.should_not == c2
+      end
+
+      it "reads and creates full diagram" do
+        erd = Erd.new
+        erd.read(File.new("spec/fixtures/complex_erd.xml"))
+        erd.mark.should == 4.0
+        erd.should have(5).vertices
+        erd.should have(6).edges
+        
+        b0 = erd.vertices.find {|b| b.id == 0}
+        b0.name.should == "Unit"
+        b0.should have(2).neighbours
+        
+        b1 = erd.vertices.find {|b| b.id == 1}
+        b1.name.should == "Employee"
+        b1.should have(2).neighbours
+        
+        b2 = erd.vertices.find {|b| b.id == 2}
+        b2.name.should == "Course"
+        b2.should have(3).neighbours
+
+        b3 = erd.vertices.find {|b| b.id == 3}
+        b3.name.should == "Presentation"
+        b3.should have(3).neighbours
+        
+        b4 = erd.vertices.find {|b| b.id == 4}
+        b4.name.should == "Client"
+        b4.should have(1).neighbours
+
+        l0 = erd.edges.find {|e| e.id == 0}
+        l0.connections.find {|c| c.end == b0}.blob.should be :closed
+        l0.connections.find {|c| c.end == b0}.crowsfoot.should be :yes
+        l0.connections.find {|c| c.end == b2}.blob.should be :closed
+        l0.connections.find {|c| c.end == b2}.crowsfoot.should be :no
+
+        l1 = erd.edges.find {|e| e.id == 1}
+        l1.connections.find {|c| c.end == b0}.blob.should be :open
+        l1.connections.find {|c| c.end == b0}.crowsfoot.should be :yes
+        l1.connections.find {|c| c.end == b1}.blob.should be :closed
+        l1.connections.find {|c| c.end == b1}.crowsfoot.should be :no
+
+        l2 = erd.edges.find {|e| e.id == 2}
+        l2.connections.find {|c| c.end == b1}.blob.should be :closed
+        l2.connections.find {|c| c.end == b1}.crowsfoot.should be :no
+        l2.connections.find {|c| c.end == b3}.blob.should be :closed
+        l2.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
+
+        l3 = erd.edges.find {|e| e.id == 3}
+        l3.connections.find {|c| c.end == b2}.blob.should be :open
+        l3.connections.find {|c| c.end == b2}.crowsfoot.should be :no
+        l3.connections.find {|c| c.end == b3}.blob.should be :closed
+        l3.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
+
+        l4 = erd.edges.find {|e| e.id == 4}
+        l4.connections.find {|c| c.end == b3}.blob.should be :closed
+        l4.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
+        l4.connections.find {|c| c.end == b4}.blob.should be :closed
+        l4.connections.find {|c| c.end == b4}.crowsfoot.should be :no
+
+        l5 = erd.edges.find {|e| e.id == 5}
+        l5.connections.find {|c| c.crowsfoot == :yes}.blob.should be :open
+        l5.connections.find {|c| c.crowsfoot == :no}.blob.should be :open
+        l5.connections.find {|c| c.crowsfoot == :yes}.end.should be b2
+        l5.connections.find {|c| c.crowsfoot == :no}.end.should be b2
+      end
+    end # #read
   end
 end
   end
 end