Commit f70d7dc

mo khan <mo@mokhan.ca>
2013-12-11 05:22:48
forward parameters through command chain.
1 parent cdb0166
Changed files (2)
lib/nasty/command.rb
@@ -10,8 +10,8 @@ class CompositeCommand
     @last = last
   end
 
-  def run
-    @first.run
-    @last.run
+  def run(*args)
+    @first.run(*args)
+    @last.run(*args)
   end
 end
spec/unit/command_spec.rb
@@ -7,6 +7,15 @@ describe Command do
     def ran?; @ran; end
   end
 
+  class ComplexCommand < SimpleCommand
+    attr_accessor :arguments
+
+    def run(*args)
+      super()
+      self.arguments = args
+    end
+  end
+
   context "without parameters" do
     let(:first_command) { SimpleCommand.new }
     let(:second_command) { SimpleCommand.new }
@@ -18,4 +27,19 @@ describe Command do
       second_command.ran?.should be_true
     end
   end
+
+  context "with parameters" do
+    let(:first_command) { ComplexCommand.new }
+    let(:second_command) { ComplexCommand.new }
+
+    it "forwards the input to each command" do
+      result = first_command.then(second_command)
+      result.run("hello world", 29)
+      first_command.ran?.should be_true
+      first_command.arguments.should include("hello world")
+      first_command.arguments.should include(29)
+      second_command.arguments.should include("hello world")
+      second_command.arguments.should include(29)
+    end
+  end
 end