Commit ab0718b

mo k <mo@mokhan.ca>
2012-11-07 04:15:47
use dynamic lookup to instantiate new instructions.
1 parent 45f2f6a
lib/console.rb
@@ -1,6 +1,6 @@
-require 'navigate_rover'
-require 'command_processor'
-require 'interceptor'
+Dir[File.dirname(__FILE__) + "/**/*.rb"].each do |file|
+  require file
+end
 
 class Console
   def initialize(input, output, processor = CommandProcessor.new)
lib/navigate_rover.rb
@@ -15,9 +15,7 @@ class NavigateRover
   def run
     rover = create_plateau(@plateau_size).deploy_rover_to(@heading, @x.to_i, @y.to_i)
     @instructions.each do |instruction|
-      commands_for(rover).each do |item|
-        item.run(instruction)
-      end
+      eval("#{instruction.upcase}Command").new.run(rover)
     end
     rover.to_s
   end
@@ -28,16 +26,4 @@ class NavigateRover
     x,y = plateau_size.split(' ')
     Plateau.new(x,y)
   end
-
-  def commands_for(rover)
-    [
-      create_command_for(rover, eval("MoveForward"), 'M'),
-      create_command_for(rover, eval("TurnLeft"), 'L'),
-      create_command_for(rover, eval("TurnRight"), 'R')
-    ]
-  end
-
-  def create_command_for(rover, command, instruction)
-    FilteredCommand.new(command.new(rover), Specification.new(lambda { |x| instruction == x.upcase }))
-  end
 end
lib/rover_commands.rb
@@ -0,0 +1,15 @@
+class RCommand
+  def run(rover)
+    rover.rotate(90)
+  end
+end
+class LCommand
+  def run(rover)
+    rover.rotate(-90)
+  end
+end
+class MCommand
+  def run(rover)
+    rover.drive
+  end
+end