Skip to content
Browse files

CommandBuilder modules are now conditionally loading.

  • Loading branch information...
1 parent b960f71 commit a45b073bcc937348fa9aca5e20db407c45433190 @theunraveler committed
View
22 lib/taskwarrior-web/command_builder.rb
@@ -0,0 +1,22 @@
+require 'taskwarrior-web/config'
+
+module TaskwarriorWeb
+ module CommandBuilder
+ def self.included(class_name)
+ class_name.class_eval do
+ case TaskwarriorWeb::Config.task_version
+ when /^2/
+ require 'taskwarrior-web/command_builders/v2'
+ include TaskwarriorWeb::CommandBuilder::V2
+ when /^1/
+ require 'taskwarrior-web/command_builders/v1'
+ include TaskwarriorWeb::CommandBuilder::V1
+ else
+ raise TaskwarriorWeb::UnrecognizedTaskVersion
+ end
+ end
+ end
+ end
+
+ class UnrecognizedTaskVersion < Exception; end
+end
View
1 lib/taskwarrior-web/command_builders/v1.rb
@@ -0,0 +1 @@
+module TaskwarriorWeb::CommandBuilder::V1; end
View
1 lib/taskwarrior-web/command_builders/v2.rb
@@ -0,0 +1 @@
+module TaskwarriorWeb::CommandBuilder::V2; end
View
7 lib/taskwarrior-web/config.rb
@@ -4,12 +4,7 @@ module TaskwarriorWeb
class Config
def self.task_version
- unless @task_version
- version_line = `task version | grep '^task '`
- @task_version = version_line.split.at(1)
- end
-
- @task_version
+ @task_version ||= `task version | grep '^task '`.split.at(1)
end
def self.file
View
11 lib/taskwarrior-web/runners/v1.rb
@@ -1,11 +0,0 @@
-module TaskwarriorWeb
- module Runner
- module Version1
-
- def self.run
- puts 'Called versioned run method.'
- end
-
- end
- end
-end
View
7 lib/taskwarrior-web/runners/v2.rb
@@ -1,7 +0,0 @@
-module TaskwarriorWeb
- module Runner
- module Version2
-
- end
- end
-end
View
37 spec/models/command_builder_spec.rb
@@ -0,0 +1,37 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'taskwarrior-web/command_builder'
+
+RSpec::Mocks::setup(TaskwarriorWeb::Config)
+
+describe TaskwarriorWeb::CommandBuilder do
+ describe '.included' do
+ context 'when v2 is reported' do
+ it 'should include CommandBuilder V2 module' do
+ TaskwarriorWeb::Config.should_receive(:task_version).and_return('2.0.1')
+ TestCommandClass.class_eval { include TaskwarriorWeb::CommandBuilder }
+ TestCommandClass.included_modules.should include(TaskwarriorWeb::CommandBuilder::V2)
+ end
+ end
+
+ context 'when v1 is reported' do
+ it 'should include CommandBuilder V1 module' do
+ TaskwarriorWeb::Config.should_receive(:task_version).and_return('1.9.4')
+ TestCommandClass.class_eval { include TaskwarriorWeb::CommandBuilder }
+ TestCommandClass.included_modules.should include(TaskwarriorWeb::CommandBuilder::V1)
+ end
+ end
+
+ context 'when an invalid version number is reported' do
+ it 'should throw an UnrecognizedTaskVersion exception' do
+ TaskwarriorWeb::Config.should_receive(:task_version).and_return('95.583.3')
+ expect {
+ TestCommandClass.class_eval { include TaskwarriorWeb::CommandBuilder }
+ }.should raise_exception(TaskwarriorWeb::UnrecognizedTaskVersion)
+ end
+ end
+ end
+end
+
+class TestCommandClass; end
+
+module TaskwarriorWeb::CommandBuilder::V2; end
View
1 spec/models/command_spec.rb
@@ -34,6 +34,5 @@
command = TaskwarriorWeb::Command.new(nil, 5)
expect { command.run }.to raise_error(TaskwarriorWeb::MissingCommandError)
end
-
end
end
View
6 spec/models/runner_spec.rb
@@ -7,7 +7,7 @@
end
describe '.run' do
- context 'valid, without a command' do
+ context 'when valid, without a command' do
before do
TaskwarriorWeb::Runner.should_receive(:`).and_return('{}')
end
@@ -22,14 +22,14 @@
end
end
- context 'invalid' do
+ context 'when given an invalid command' do
it 'should throw an exception if the command is not valid' do
@command.command = :test
expect { TaskwarriorWeb::Runner.run(@command) }.to raise_error(TaskwarriorWeb::InvalidCommandError)
end
end
- context 'with a given command' do
+ context 'when given a valid command' do
it 'should execute the given command' do
TaskwarriorWeb::Runner.should_receive(:`).with('task add').and_return('{}')
TaskwarriorWeb::Runner.run(@command)

0 comments on commit a45b073

Please sign in to comment.
Something went wrong with that request. Please try again.