-require 'erd_handler/erd'
+require 'bundler/setup'
+
require "rexml/document"
include REXML
+require "graph.njae"
+include GraphNjae
+
+require 'erd_handler/erd'
+require 'erd_handler/box'
+require 'erd_handler/link'
+
-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 = 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
+ self
end
end
-end
+end
\ No newline at end of file
require 'spec_helper'
-module Erd
+module ErdHandler
describe Erd do
let(:input) { double('input').as_null_object }
let(:output) { double('output').as_null_object }
let(:erd) { Erd.new(input, output) }
- describe "#read" do
- it "reads a single box" do
- doc = Document.new File.new("spec/fixtures/single_box_erd.xml")
- erd = Erd.new doc
- erd.mark.should == 6.5
- erd.boxes.length.should == 1
-
+ describe "#initialize" do
+ it "creates an empty ERD" do
+ erd = Erd.new
+ erd.mark.should be_nil
+ erd.should have(0).vertices
+ erd.should have(0).edges
end
- end # #read
+
+ it "reads and creates a single box" do
+ erd = Erd.new(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
+ erd = Erd.new(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
+ end
+
+ it "reads and creates a box with a self-loop"
+ end # #initialize
end
end
\ No newline at end of file
<box id="0" name="Client" mark="0.0">
<location x="33.0" y="43.0"/>
<size width="80.0" height="40.0"/>
- <comment></comment>
+ <comment>A comment</comment>
</box>
</Drawing>
+require 'rexml/document.rb'
+include REXML
+
+require 'graph.njae'
+include GraphNjae
+
require 'erd_handler'
-require "rexml/document"
-include REXML
\ No newline at end of file