Commit cdb0166

mo khan <mo@mokhan.ca>
2013-12-11 05:18:53
create composite command.
1 parent 7cbc886
lib/nasty/command.rb
@@ -0,0 +1,17 @@
+module Command
+  def then(next_command)
+    CompositeCommand.new(self, next_command)
+  end
+end
+
+class CompositeCommand
+  def initialize(first, last)
+    @first = first
+    @last = last
+  end
+
+  def run
+    @first.run
+    @last.run
+  end
+end
lib/nasty.rb
@@ -1,3 +1,4 @@
+require "nasty/command"
 require "nasty/version"
 
 module Nasty
spec/unit/command_spec.rb
@@ -0,0 +1,21 @@
+require "spec_helper"
+
+describe Command do
+  class SimpleCommand
+    include Command
+    def run; @ran = true; end
+    def ran?; @ran; end
+  end
+
+  context "without parameters" do
+    let(:first_command) { SimpleCommand.new }
+    let(:second_command) { SimpleCommand.new }
+
+    it "chains two commands together" do
+      result = first_command.then(second_command)
+      result.run
+      first_command.ran?.should be_true
+      second_command.ran?.should be_true
+    end
+  end
+end
spec/spec_helper.rb
@@ -0,0 +1,3 @@
+require 'simplecov'
+SimpleCov.start
+require 'nasty'