Commit 63b7b31

mo khan <mo@mokhan.ca>
2015-04-08 21:13:35
draw a rectangle with a path.
1 parent f94c815
Changed files (2)
lib
scale
shapes
spec
lib/scale/shapes/path.rb
@@ -4,23 +4,33 @@ module Scale
     attribute :d, String
 
     def move_to(x:, y:)
-      self.d="M#{x} #{y}"
+      command("M#{x} #{y}")
     end
 
     def line_to(x:, y:)
-      self.d = "#{self.d} L #{x} #{y}"
+      command("L #{x} #{y}")
     end
 
     def horizontal(n)
-      self.d = "#{self.d} H #{n}"
+      command("H #{n}")
     end
 
     def vertical(n)
-      self.d = "#{self.d} V #{n}"
+      command("V #{n}")
     end
 
     def xml_tag
       :path
     end
+
+    private
+
+    def command(instructions)
+      if self.d.nil?
+        self.d = instructions
+      else
+        self.d = "#{self.d} #{instructions}"
+      end
+    end
   end
 end
spec/shapes/path_spec.rb
@@ -45,4 +45,21 @@ describe Scale::Path do
       expect(subject.to_xml).to eql(expected)
     end
   end
+
+  describe "drawing a rectangle" do
+    it 'draws the proper rectangle' do
+      subject.move_to(x: 10, y: 10)
+      subject.horizontal(90)
+      subject.vertical(90)
+      subject.horizontal(10)
+      subject.line_to(x: 10, y: 10)
+      
+      expected = <<-XML
+<?xml version="1.0"?>
+<path d="M10 10 H 90 V 90 H 10 L 10 10"/>
+      XML
+      expect(subject.to_xml).to eql(expected)
+    end
+  end
+
 end