From ac115385d31d11f560702c320267c0ece8671ce8 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Fri, 7 Feb 2014 15:58:44 -0500 Subject: [PATCH] panda, tiger, eagle --- spec/terminator_spec.rb | 64 +++++++++++++++++++++++++++++++++++++++++ spec_helper.rb | 15 ++++++++++ terminator.rb | 9 ++++++ terminator_spec.rb | 23 --------------- terminatorable.rb | 29 +++++++++++++++---- 5 files changed, 112 insertions(+), 28 deletions(-) create mode 100644 spec/terminator_spec.rb create mode 100644 spec_helper.rb delete mode 100644 terminator_spec.rb diff --git a/spec/terminator_spec.rb b/spec/terminator_spec.rb new file mode 100644 index 0000000..2487a5a --- /dev/null +++ b/spec/terminator_spec.rb @@ -0,0 +1,64 @@ +require './spec_helper' +require "./terminator" + +describe BadTerminator do + it "should destroy_john_connor" do + subject.destroy_john_connor! + subject.current_mission.should eq("destroy: john_connor") + end + + it "should destroy_sarah_connor" do + subject.destroy_sarah_connor! + subject.current_mission.should eq("destroy: sarah_connor") + end + + it 'should not protect john' do + expect(subject.protects?('John Connor')).to be_false + end + + it 'should not protect Sarah' do + expect(subject.protects?('Sarah Connor')).to be_false + end + + it 'should not protect Bob' do + expect(subject.protects?('Bob')).to be_false + puts 'poor bob' + end + + it 'should not be good if it is trying to destory someone' do + subject.destroy_sarah_connor! + expect(subject.good?).to be_false + end + +end + +describe GoodTerminator do + it "should protect_john_connor" do + subject.protect_john_connor! + subject.current_mission.should eq("protect: john_connor") + end + + + it "should protect_sarah_connor" do + subject.protect_sarah_connor! + subject.current_mission.should eq("protect: sarah_connor") + end + + it 'should protect john' do + expect(subject.protects?('John Connor')).to be_true + end + + it 'should protect Sarah' do + expect(subject.protects?('Sarah Connor')).to be_true + end + + it 'should not protect Bob' do + expect(subject.protects?('Bob')).to be_false + puts 'poor bob' + end + + it 'should be good if it is not trying to destory someone' do + subject.protect_sarah_connor! + expect(subject.good?).to be_true + end +end diff --git a/spec_helper.rb b/spec_helper.rb new file mode 100644 index 0000000..845893e --- /dev/null +++ b/spec_helper.rb @@ -0,0 +1,15 @@ +require 'rspec' + + + + +RSpec.configure do |config| + # Use color in STDOUT + config.color_enabled = true + + # Use color not only in STDOUT but also in pagers and files + config.tty = true + + # Use the specified formatter + config.formatter = :documentation # :progress, :html, :textmate +end diff --git a/terminator.rb b/terminator.rb index 8bd4a43..2dc4e4c 100644 --- a/terminator.rb +++ b/terminator.rb @@ -2,6 +2,15 @@ class Terminator include Terminatorable +end + +class GoodTerminator < Terminator likes_to_protect [:john_connor, :sarah_connor] + likes_to_destroy [] +end + +class BadTerminator < Terminator + likes_to_protect [] + likes_to_destroy [:john_connor, :sarah_connor] end diff --git a/terminator_spec.rb b/terminator_spec.rb deleted file mode 100644 index 749dae3..0000000 --- a/terminator_spec.rb +++ /dev/null @@ -1,23 +0,0 @@ -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 - 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") - end - - it "should protect_sarah_connor" do - subject.protect_sarah_connor! - subject.current_mission.should eq("protect: sarah_connor") - end -end diff --git a/terminatorable.rb b/terminatorable.rb index 378afd6..b8d09a2 100644 --- a/terminatorable.rb +++ b/terminatorable.rb @@ -1,19 +1,38 @@ 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 + + def likes_to_destroy(people=[]) + people.each do |person| + define_method "destroy_#{person}!" do + @current_mission = "destroy: #{person}" end end - end end + + end def self.included(klass) - attr_reader :current_mission + attr_reader :current_mission, :good klass.extend Terminatorable::ClassMethods end + + def protects?(person) + methodize = "protect_#{person.downcase!.sub(' ','_')}!" + self.respond_to?(methodize) + end + + def good? + @current_mission[0..7] == 'protect:' + end + end