Commit 5827b28

mo k <mo@mokhan.ca>
2012-11-07 04:34:56
bring back turn_right, turn_left using a Module.
1 parent a65ecff
lib/console.rb
@@ -1,3 +1,4 @@
+require 'rotation'
 Dir[File.dirname(__FILE__) + "/**/*.rb"].each do |file|
   require file
 end
lib/east.rb
@@ -1,4 +1,5 @@
 class East
+  include Rotation
   def rotate(degrees)
     degrees > 0 ? South.new : North.new
   end
lib/north.rb
@@ -1,4 +1,6 @@
 class North
+  include Rotation
+
   def rotate(degrees)
     degrees > 0 ? East.new : West.new
   end
lib/rotation.rb
@@ -0,0 +1,8 @@
+module Rotation
+  def turn_right
+    rotate(90)
+  end
+  def turn_left
+    rotate(-90)
+  end
+end
lib/south.rb
@@ -1,4 +1,5 @@
 class South
+  include Rotation
   def rotate(degrees)
     degrees > 0 ? West.new : East.new
   end
@@ -12,3 +13,4 @@ class South
     'S'
   end
 end
+
lib/west.rb
@@ -1,4 +1,5 @@
 class West
+  include Rotation
   def rotate(degrees)
     degrees > 0 ? North.new : South.new
   end
spec/unit/east_spec.rb
@@ -15,12 +15,12 @@ describe East do
   end
   context "when turning right" do
     it "should face South" do
-      sut.rotate(90).should be_an_instance_of(South)
+      sut.turn_right.should be_an_instance_of(South)
     end
   end
   context "when turning left" do
     it "should face North" do
-      sut.rotate(-90).should be_an_instance_of(North)
+      sut.turn_left.should be_an_instance_of(North)
     end
   end
   context "when displayed" do
spec/unit/north_spec.rb
@@ -14,12 +14,12 @@ describe North do
   end
   context "when turning right" do
     it "should face east" do
-      sut.rotate(90).should be_an_instance_of(East)
+      sut.turn_right.should be_an_instance_of(East)
     end
   end
   context "when turning left" do
     it "should face west" do
-      sut.rotate(-90).should be_an_instance_of(West)
+      sut.turn_left.should be_an_instance_of(West)
     end
   end
   context "when displayed" do
spec/unit/south_spec.rb
@@ -14,12 +14,12 @@ describe South do
   end
   context "when turning left" do
     it "should face east" do
-      sut.rotate(-90).should be_an_instance_of(East)
+      sut.turn_left.should be_an_instance_of(East)
     end
   end
   context "when turning right" do
     it "should face west" do
-      sut.rotate(90).should be_an_instance_of(West)
+      sut.turn_right.should be_an_instance_of(West)
     end
   end
   context "when displayed" do
spec/unit/west_spec.rb
@@ -15,12 +15,12 @@ describe West do
   end
   context "when turning right" do
     it "should face North" do
-      sut.rotate(90).should be_an_instance_of(North)
+      sut.turn_right.should be_an_instance_of(North)
     end
   end
   context "when turning left" do
     it "should face South" do
-      sut.rotate(-90).should be_an_instance_of(South)
+      sut.turn_left.should be_an_instance_of(South)
     end
   end
   context "when displayed" do
spec/spec_helper.rb
@@ -3,6 +3,7 @@ require "rspec-fakes"
 require 'simplecov'
 SimpleCov.start
 
+require 'rotation'
 Dir[File.dirname(__FILE__) + "/../lib/**/*.rb"].each do |file|
   require file
 end