From a6233a261dd522d3f81030f2643cca56f7c1f7b6 Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Mon, 18 Feb 2013 21:00:07 -0800 Subject: [PATCH 1/3] added methods good? and protects? --- .gitignore | 1 + terminator.rb | 5 ++++- terminator_spec.rb | 27 +++++++++++++++++++++++++++ terminatorable.rb | 4 ++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 61a293a..de633b8 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ doc/ config/database.yml TODO +.idea diff --git a/terminator.rb b/terminator.rb index 8bd4a43..4af3d26 100644 --- a/terminator.rb +++ b/terminator.rb @@ -3,5 +3,8 @@ class Terminator include Terminatorable likes_to_protect [:john_connor, :sarah_connor] -end + def protects?(person) + [:john_connor, :sarah_connor].include?(person) + end +end diff --git a/terminator_spec.rb b/terminator_spec.rb index 749dae3..4d597cd 100644 --- a/terminator_spec.rb +++ b/terminator_spec.rb @@ -1,6 +1,7 @@ require 'rspec' require "./terminator" + describe Terminator do it "should destroy_john_connor" do subject.destroy_john_connor! @@ -20,4 +21,30 @@ subject.protect_sarah_connor! subject.current_mission.should eq("protect: sarah_connor") end + + describe "#protects?" do + it 'should return true for john_connor' do + subject.protects?(:john_connor).should be_true + end + + it 'should return true for sarah_connor' do + subject.protects?(:sarah_connor).should be_true + end + + it 'should return false for random unknown person' do + subject.protects?(:random_person).should be_false + end + end + + it 'should tell us terminator is bad if destroying someone' do + subject.destroy_john_connor! + #subject.good?.should be_false + subject.should_not be_good + end + + it 'should tell us terminator is good if protecting someone' do + subject.protect_sarah_connor! + #subject.good?.should be_true + subject.should be_good + end end diff --git a/terminatorable.rb b/terminatorable.rb index 378afd6..b4dfa66 100644 --- a/terminatorable.rb +++ b/terminatorable.rb @@ -9,6 +9,10 @@ def likes_to_protect(people=[]) end end end + + define_method :good? do + ['protect: john_connor','protect: sarah_connor'].include?(@current_mission) + end end end From 2da459636cc831e476da840421a7db3814ba8db4 Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Mon, 18 Feb 2013 21:13:00 -0800 Subject: [PATCH 2/3] separated into good and bad terminator classes --- terminator.rb | 8 +++++++- terminator_spec.rb | 51 +++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/terminator.rb b/terminator.rb index 4af3d26..5160c74 100644 --- a/terminator.rb +++ b/terminator.rb @@ -1,6 +1,7 @@ require "./terminatorable" -class Terminator + +class GoodTerminator include Terminatorable likes_to_protect [:john_connor, :sarah_connor] @@ -8,3 +9,8 @@ def protects?(person) [:john_connor, :sarah_connor].include?(person) end end + +class BadTerminator + include Terminatorable + likes_to_protect [:john_connor, :sarah_connor] +end diff --git a/terminator_spec.rb b/terminator_spec.rb index 4d597cd..af500b1 100644 --- a/terminator_spec.rb +++ b/terminator_spec.rb @@ -1,33 +1,23 @@ require 'rspec' require "./terminator" - -describe Terminator do - it "should destroy_john_connor" do - subject.destroy_john_connor! - subject.current_mission.should eq("destroy: john_connor") - end - - it "should protect_john_connor" do +describe GoodTerminator do + it 'should protect_john_connor' do subject.protect_john_connor! - subject.current_mission.should eq("protect: john_connor") - end - it "should destroy_sarah_connor" do - subject.destroy_sarah_connor! - subject.current_mission.should eq("destroy: sarah_connor") + subject.current_mission.should == 'protect: john_connor' end - it "should protect_sarah_connor" do + it 'should protect_sarah_connor' do subject.protect_sarah_connor! - subject.current_mission.should eq("protect: sarah_connor") + subject.current_mission.should == 'protect: sarah_connor' end - describe "#protects?" do - it 'should return true for john_connor' do + describe '#protects?' do + it 'should return true for john connor' do subject.protects?(:john_connor).should be_true end - it 'should return true for sarah_connor' do + it 'should return true for sarah connor' do subject.protects?(:sarah_connor).should be_true end @@ -36,15 +26,26 @@ end end - it 'should tell us terminator is bad if destroying someone' do + it 'should say terminator good if protecting someone' do + subject.protect_john_connor! + subject.should be_good + end +end + + +describe BadTerminator do + it 'should destroy_john_connor' do subject.destroy_john_connor! - #subject.good?.should be_false - subject.should_not be_good + subject.current_mission.should == 'destroy: john_connor' end - it 'should tell us terminator is good if protecting someone' do - subject.protect_sarah_connor! - #subject.good?.should be_true - subject.should be_good + it 'should destroy_sarah_connor' do + subject.destroy_sarah_connor! + subject.current_mission.should == 'destroy: sarah_connor' + end + + it 'should tell us terminator is bad if destroying someone' do + subject.destroy_john_connor! + subject.should_not be_good end end From c81de976a1fdca4978c1d7fd0325175a6d3bb2b6 Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Mon, 18 Feb 2013 21:41:44 -0800 Subject: [PATCH 3/3] split likes_to_destroy and likes_to_protect methods --- terminator.rb | 12 +++++++++++- terminator_spec.rb | 7 +++++++ terminatorable.rb | 15 +++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/terminator.rb b/terminator.rb index 5160c74..7512195 100644 --- a/terminator.rb +++ b/terminator.rb @@ -3,14 +3,24 @@ class GoodTerminator include Terminatorable + likes_to_protect [:john_connor, :sarah_connor] + likes_to_destroy [] def protects?(person) [:john_connor, :sarah_connor].include?(person) end end + + class BadTerminator include Terminatorable - likes_to_protect [:john_connor, :sarah_connor] + + likes_to_protect [] + likes_to_destroy [:john_connor, :sarah_connor] + + def protects?(person) + [].include?(person) + end end diff --git a/terminator_spec.rb b/terminator_spec.rb index af500b1..8cde852 100644 --- a/terminator_spec.rb +++ b/terminator_spec.rb @@ -48,4 +48,11 @@ subject.destroy_john_connor! subject.should_not be_good end + + describe '#protects?' do + it 'should not protect john_connor' do + subject.destroy_john_connor! + subject.protects?(:john_connor).should be_false + end + end end diff --git a/terminatorable.rb b/terminatorable.rb index b4dfa66..c261be3 100644 --- a/terminatorable.rb +++ b/terminatorable.rb @@ -2,18 +2,25 @@ module Terminatorable module ClassMethods def likes_to_protect(people=[]) - ["destroy", "protect"].each do |mission_type| people.each do |person| - define_method "#{mission_type}_#{person}!" do - @current_mission = "#{mission_type}: #{person}" + define_method "protect_#{person}!" do + @current_mission = "protect: #{person}" end end - end define_method :good? do ['protect: john_connor','protect: sarah_connor'].include?(@current_mission) end end + + + def likes_to_destroy(people=[]) + people.each do |person| + define_method "destroy_#{person}!" do + @current_mission = "destroy: #{person}" + end + end + end end def self.included(klass)