Fiddling around with labels and boxes
[erd-marker.git] / lib / erd_handler / erd.rb
index b9e6da17d02ba3bbb812e665ac728698617b3b17..2c10364ef90983bbb74009d2d12d46eccea3e142 100644 (file)
@@ -1,8 +1,48 @@
-module Erd
-  class Erd
+module ErdHandler
+  class Erd < Graph
     def initialize(source = nil)
-      doc = Document.new source
-      raise InvalidErdFile unless doc.elements.length == 1 and doc.elements[1].name.downcase == 'drawing'
+      super()
+      read(source) unless source.nil?
+      self
     end
+    
+    def read(source)
+      doc = Document.new(source)
+      raise InvalidErdFile unless doc.elements.to_a.length == 1 and doc.elements[1].name.downcase == 'drawing'
+      self.mark = doc.elements['Drawing'].attributes["mark"].to_f
+      self.name = Label.new doc.elements['Drawing'].attributes["name"]
+      doc.elements.each('Drawing/box') do |box_element|
+        self << Box.new(box_element)
+      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
+    
+    # The minimal meaningful units of an ERD are:
+    #  Each box in isolation
+    #  Each link, with the associated boxes at its ends
+    def mmus
+      mmus = []
+      self.vertices.each do |b| 
+        mmu = Erd.new
+        mmu << b
+        mmus << mmu 
+      end
+      self.edges.each do |l| 
+        mmu = Erd.new
+        l.vertices.each do |b|
+          mmu << b
+        end
+        mmu << l
+        mmus << mmu
+      end
+      mmus
+    end
+    
   end
 end