diff --git a/lib/taskmapper/entities/entity.rb b/lib/taskmapper/entities/entity.rb index 772003f..ef2ccc5 100644 --- a/lib/taskmapper/entities/entity.rb +++ b/lib/taskmapper/entities/entity.rb @@ -18,6 +18,13 @@ def validate_presence_of(attribute) end end + def validate_inclusion_of(validation_criteria) + attr = validation_criteria[:attr] + in_values = validation_criteria[:in] + msg = validation_criteria[:msg] + raise Exceptions::InvalidStatus.new(msg, in_values) unless in_values.include? self.__send__ attr + end + def update_attributes(attrs) attrs.each do |key, value| self.send("#{key}=".to_sym, value) diff --git a/lib/taskmapper/entities/task.rb b/lib/taskmapper/entities/task.rb index 7e9be9f..d6f4eb1 100644 --- a/lib/taskmapper/entities/task.rb +++ b/lib/taskmapper/entities/task.rb @@ -15,7 +15,7 @@ class Task protected :requestor=, :project_id=, :factory= - + def initialize(attrs) super attrs self.title = attrs[:title] @@ -38,10 +38,22 @@ def delete end def validate - validate_presence_of :title - validate_presence_of :requestor + validate_presence_of :title + validate_presence_of :requestor + validate_status :in => [:open, :close] + validate_priority end - + + def validate_status(criteria) + in_values = criteria[:in] + raise Exceptions::InvalidStatus.new(in_values) unless in_values.include? self.status + end + + def validate_priority + raise Exceptions::InvalidPriority.new if self.priority.nil? || self.priority < 0 + end + + def create_comment(attrs) comments.create attrs end @@ -62,7 +74,9 @@ def to_hash :description => self.description, :requestor => self.requestor, :assignee => self.assignee, - :project_id => self.project_id + :project_id => self.project_id, + :status => self.status, + :priority => self.priority }) end diff --git a/lib/taskmapper/exceptions.rb b/lib/taskmapper/exceptions.rb index 57950b3..cbfe977 100644 --- a/lib/taskmapper/exceptions.rb +++ b/lib/taskmapper/exceptions.rb @@ -21,5 +21,18 @@ def initialize(provider, entities, method, args) super "Provider #{provider} does not define #{entities}##{method}#{args}" end end + + class InvalidStatus < TaskMapperException + def initialize(*values) + super "Status has to be #{values.join(',')}" + end + end + + class InvalidPriority < TaskMapperException + def initialize + super "Priority has to be a positive number" + end + end + end end diff --git a/spec/task_comments/create_spec.rb b/spec/task_comments/create_spec.rb index e70807f..c9f32a5 100644 --- a/spec/task_comments/create_spec.rb +++ b/spec/task_comments/create_spec.rb @@ -9,7 +9,9 @@ context "Given Task X" do let(:task_x) { project_x.create_task :title => "Task X", - :requestor => "Omar" } + :requestor => "Omar", + :status => :open, + :priority => 1 } context "Given valid attributes" do context "When I create a task comment on Task X" do before :all do diff --git a/spec/task_comments/delete_spec.rb b/spec/task_comments/delete_spec.rb index 35aebd1..983a24e 100644 --- a/spec/task_comments/delete_spec.rb +++ b/spec/task_comments/delete_spec.rb @@ -9,10 +9,10 @@ let(:task) do project.create_task :title => "Task X", :description => "This is task X", - :status => :new, + :status => :open, :priority => 1, :assignee => "Omar", - :requestor => "Ron", + :requestor => "Ron" end context "And 'Task X' have 3 comments" do diff --git a/spec/task_comments/search_spec.rb b/spec/task_comments/search_spec.rb index c4f216e..a80a3c7 100644 --- a/spec/task_comments/search_spec.rb +++ b/spec/task_comments/search_spec.rb @@ -11,7 +11,9 @@ context "And 'learn ukulele' have the 'buy ukulele' task" do let(:buy_ukulele) do learn_ukulele.create_task :title => "Buy Ukulele", - :requestor => "Me" + :requestor => "Me", :status => :open, + :priority => 1 + end context "And 'buy ukulele' have the following comments" do diff --git a/spec/task_comments/update_spec.rb b/spec/task_comments/update_spec.rb index 01bdc78..468d1a6 100644 --- a/spec/task_comments/update_spec.rb +++ b/spec/task_comments/update_spec.rb @@ -13,6 +13,7 @@ :priority => 1, :assignee => "Omar", :requestor => "Ron", + :status => :open end context "And 'Task X' have a comment" do diff --git a/spec/tasks/create_spec.rb b/spec/tasks/create_spec.rb index 1311f61..9a7d673 100644 --- a/spec/tasks/create_spec.rb +++ b/spec/tasks/create_spec.rb @@ -3,10 +3,10 @@ describe "Create Task" do context "Given Project X" do let(:tm) { TaskMapper.new :in_memory, - :user => 'chuck', :password => 'norris' } - + :user => 'chuck', :password => 'norris' } + let(:project) { tm.create_project :name => 'Project X' } - + let(:task) { project.create_task attributes } describe :task! do @@ -14,41 +14,75 @@ it { should respond_to :task! } end + context "When I create a task for Project X" do let(:attributes) {{ :title => 'Test Task', :description => 'This is a test', :requestor => 'Ron Evans', :assignee => 'Omar Rodriguez', + :status => :open, + :priority => 1 }} - + describe :task do subject { task } - + its(:id) { should == 1 } its(:title) { should == 'Test Task' } its(:description) { should == 'This is a test'; } its(:requestor) { should == 'Ron Evans' } its(:assignee) { should == 'Omar Rodriguez' } its(:project_id) { should == 1 } + its(:status) { should == :open } + its(:priority) { should == 1 } + end + end + + context "When I create a task with nil status" do + let(:attributes) { {:title => 'Test Task', :requestor => 'Ron Evans', :status => nil} } + let(:task_without_status) { project.create_task attributes } + let(:error) do + catch_error(TaskMapper::Exceptions::InvalidStatus) { task_without_status } + end + + describe :error do + subject { error } + it { should_not be_nil } + its(:message) { should match /Status has to be/ } end end - - context "When I create a project with nil name" do + + context "When I create a task with nil priority" do + let(:attributes) { { :title => 'Test Task', :requestor => 'Ron Evans', :status => :open, + :priority => nil } } + let(:task_without_priority) { project.create_task attributes } + let(:error) do + catch_error(TaskMapper::Exceptions::InvalidPriority) { task_without_priority } + end + + describe :error do + subject { error } + it { should_not be_nil } + its(:message) { should match /Priority has to be/ } + end + end + + context "When I create a task with nil title" do let(:attributes) {{ :title => nil, :requestor => 'Ron' }} let(:error) { catch_error(TaskMapper::Exceptions::RequiredAttribute) { task } } - + describe :error do subject { error } it { should_not be_nil } its(:message) { should match /Task title is required/ } end end - - context "When I create a project with nil requestor" do + + context "When I create a task with nil requestor" do let(:attributes) {{ :title => "test" }} let(:error) { catch_error(TaskMapper::Exceptions::RequiredAttribute) { task } } - + describe :error do subject { error } it { should_not be_nil } diff --git a/spec/tasks/delete_spec.rb b/spec/tasks/delete_spec.rb index be63630..106532a 100644 --- a/spec/tasks/delete_spec.rb +++ b/spec/tasks/delete_spec.rb @@ -9,11 +9,11 @@ context "And Project X have Tasks X and Y" do before :all do - project_x.task! :title => "Task X", - :requestor => "Me" + project_x.task! :title => "Task X", :priority => 1, + :requestor => "Me", :status => :open - project_x.task! :title => "Task Y", - :requestor => "Me" + project_x.task! :title => "Task Y", :priority => 1, + :requestor => "Me", :status => :open end context "When I delete Task X" do diff --git a/spec/tasks/search_spec.rb b/spec/tasks/search_spec.rb index 5517713..c536264 100644 --- a/spec/tasks/search_spec.rb +++ b/spec/tasks/search_spec.rb @@ -4,58 +4,59 @@ let(:tm) do TaskMapper.new :in_memory, :user => 'mark', :password => 'twain' end - + context "Given the following projects" do let(:secret_project) { tm.project! :name => 'Plan to kill Justin Bieber' } - + let(:learn_ukulele) { tm.project! :name => 'Leard to play ukulele' } - + context "Given the following tasks" do before do - secret_project.create_task :title => "Buy bomb materias", - :description => "Go to hardware store", - :requestor => "Ludwig van Beethoven" - - secret_project.create_task :title => "Contruct a bomb", - :description => "Visit howstuffworks.com", - :requestor => "Ludwig van Beethoven" - - secret_project.create_task :title => "Install the bomb in Justin Bieber's house", - :description => "Go there and do it", - :requestor => "Ludwig van Beethoven" - - secret_project.create_task :title => "Detonate bomb with remote controller", - :description => "Save the world", - :requestor => "Ludwig van Beethoven" - - learn_ukulele.create_task :title => "Buy Ukulele", + secret_project.create_task :title => "Buy bomb materias", + :description => "Go to hardware store", :priority => 1, + :requestor => "Ludwig van Beethoven", :status => :open + + secret_project.create_task :title => "Contruct a bomb", + :description => "Visit howstuffworks.com", :priority => 1, + :requestor => "Ludwig van Beethoven", :status => :open + + secret_project.create_task :title => "Install the bomb in Justin Bieber's house", + :description => "Go there and do it", :priority => 1, + :requestor => "Ludwig van Beethoven", :status => :open + + secret_project.create_task :title => "Detonate bomb with remote controller", + :description => "Save the world", :priority => 1, + :requestor => "Ludwig van Beethoven", :status => :open + + learn_ukulele.create_task :title => "Buy Ukulele", :priority => 1, + :requestor => "Me", :status => :open - :requestor => "Me" - - learn_ukulele.create_task :title => "Buy Ukulele book", - :requestor => "Me" - - learn_ukulele.create_task :title => "Practice hard", - :requestor => "Me" + learn_ukulele.create_task :title => "Buy Ukulele book", :priority => 1, + :requestor => "Me", :status => :open + + learn_ukulele.create_task :title => "Practice hard", :priority => 1, + :requestor => "Me", :status => :open end - + context "Retrieve all tasks" do subject { tm.tasks } - + its(:count) { should == 7 } end - + context "Retrieve Plan to kill Justin Bieber' project tasks" do subject { secret_project.tasks } its(:count) { should == 4 } - + describe :second do subject { secret_project.tasks[1] } its(:id) { should == 2 } its(:title) { should == "Contruct a bomb" } its(:description) { should == "Visit howstuffworks.com" } + its(:status) { should == :open } + its(:priority) { should == 1 } end - + describe :fourth do subject { secret_project.tasks[3] } its(:id) { should == 4 } diff --git a/spec/tasks/update_spec.rb b/spec/tasks/update_spec.rb index 2cd2ccf..ece89ef 100644 --- a/spec/tasks/update_spec.rb +++ b/spec/tasks/update_spec.rb @@ -8,7 +8,7 @@ let(:task) do project.create_task :title => "Task X", :description => "This is task X", - :status => :new, + :status => :open, :priority => 1, :assignee => "Omar", :requestor => "Ron", @@ -21,7 +21,8 @@ :description => "This is task X.1", :status => :in_progress, :priority => 2, - :assignee => "Rafa" + :assignee => "Rafa", + :status => :open end describe :update? do @@ -36,7 +37,7 @@ subject { task } its(:title) { should == "Task X.1" } its(:description) { should == "This is task X.1" } - its(:status) { should == :in_progress } + its(:status) { should == :open } its(:priority) { should == 2 } its(:assignee) { should == "Rafa" } end