From 4f2c2f6f7795c9337796c16beb8126dfc42a940c Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 25 Sep 2011 19:35:03 +0100 Subject: [PATCH] Another commit --- lib/erd_handler.rb | 10 +++++++++- lib/erd_handler/erd.rb | 25 +++++++++++++++++++----- spec/erd_handler/erd_spec.rb | 33 +++++++++++++++++++++++--------- spec/fixtures/single_box_erd.xml | 2 +- spec/spec_helper.rb | 8 ++++++-- 5 files changed, 60 insertions(+), 18 deletions(-) diff --git a/lib/erd_handler.rb b/lib/erd_handler.rb index 501d016..6524d31 100644 --- a/lib/erd_handler.rb +++ b/lib/erd_handler.rb @@ -1,4 +1,12 @@ -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' + diff --git a/lib/erd_handler/erd.rb b/lib/erd_handler/erd.rb index b9e6da1..a0c94e4 100644 --- a/lib/erd_handler/erd.rb +++ b/lib/erd_handler/erd.rb @@ -1,8 +1,23 @@ -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 diff --git a/spec/erd_handler/erd_spec.rb b/spec/erd_handler/erd_spec.rb index ec75afe..164a91e 100644 --- a/spec/erd_handler/erd_spec.rb +++ b/spec/erd_handler/erd_spec.rb @@ -1,19 +1,34 @@ 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 diff --git a/spec/fixtures/single_box_erd.xml b/spec/fixtures/single_box_erd.xml index 45e7118..e339b2b 100644 --- a/spec/fixtures/single_box_erd.xml +++ b/spec/fixtures/single_box_erd.xml @@ -5,6 +5,6 @@ - + A comment diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3c4ea53..e82322e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,7 @@ +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 -- 2.34.1