diff options
| author | mo khan <mo@mokhan.ca> | 2013-12-10 22:25:07 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-12-10 22:25:07 -0700 |
| commit | 0ca2c647c7f659eec9340023c533ee3e8a066fc0 (patch) | |
| tree | 49c67c12468c8fabcd2bcc6cb8131f5656f7b536 | |
| parent | f70d7dce60d3d561314938755754751e8e82c331 (diff) | |
split out to separate files and move to nasty namespace.
| -rw-r--r-- | lib/nasty.rb | 1 | ||||
| -rw-r--r-- | lib/nasty/command.rb | 20 | ||||
| -rw-r--r-- | lib/nasty/composite_command.rb | 13 | ||||
| -rw-r--r-- | spec/unit/command_spec.rb | 66 |
4 files changed, 53 insertions, 47 deletions
diff --git a/lib/nasty.rb b/lib/nasty.rb index eb0c846..acfdce1 100644 --- a/lib/nasty.rb +++ b/lib/nasty.rb @@ -1,4 +1,5 @@ require "nasty/command" +require "nasty/composite_command" require "nasty/version" module Nasty diff --git a/lib/nasty/command.rb b/lib/nasty/command.rb index 14c97f6..c61cfd1 100644 --- a/lib/nasty/command.rb +++ b/lib/nasty/command.rb @@ -1,17 +1,7 @@ -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(*args) - @first.run(*args) - @last.run(*args) +module Nasty + module Command + def then(next_command) + CompositeCommand.new(self, next_command) + end end end diff --git a/lib/nasty/composite_command.rb b/lib/nasty/composite_command.rb new file mode 100644 index 0000000..6b0c683 --- /dev/null +++ b/lib/nasty/composite_command.rb @@ -0,0 +1,13 @@ +module Nasty + class CompositeCommand + def initialize(first, last) + @first = first + @last = last + end + + def run(*args) + @first.run(*args) + @last.run(*args) + end + end +end diff --git a/spec/unit/command_spec.rb b/spec/unit/command_spec.rb index 6a43be0..a3eddf1 100644 --- a/spec/unit/command_spec.rb +++ b/spec/unit/command_spec.rb @@ -1,45 +1,47 @@ require "spec_helper" -describe Command do - class SimpleCommand - include Command - def run; @ran = true; end - def ran?; @ran; end - end +module Nasty + describe Command do + class SimpleCommand + include Command + def run; @ran = true; end + def ran?; @ran; end + end - class ComplexCommand < SimpleCommand - attr_accessor :arguments + class ComplexCommand < SimpleCommand + attr_accessor :arguments - def run(*args) - super() - self.arguments = args + def run(*args) + super() + self.arguments = args + end end - end - context "without parameters" do - let(:first_command) { SimpleCommand.new } - let(:second_command) { SimpleCommand.new } + 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 + 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 - context "with parameters" do - let(:first_command) { ComplexCommand.new } - let(:second_command) { ComplexCommand.new } + 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) + 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 end |
