Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ doc/

config/database.yml
TODO
.idea
21 changes: 20 additions & 1 deletion terminator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
require "./terminatorable"

class Terminator

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 []
likes_to_destroy [:john_connor, :sarah_connor]

def protects?(person)
[].include?(person)
end
end
57 changes: 46 additions & 11 deletions terminator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
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")
describe GoodTerminator do
it 'should protect_john_connor' do
subject.protect_john_connor!
subject.current_mission.should == 'protect: john_connor'
end

it 'should protect_sarah_connor' do
subject.protect_sarah_connor!
subject.current_mission.should == 'protect: sarah_connor'
end

it "should protect_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
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 say terminator good if protecting someone' do
subject.protect_john_connor!
subject.current_mission.should eq("protect: john_connor")
subject.should be_good
end
it "should destroy_sarah_connor" do
end


describe BadTerminator do
it 'should destroy_john_connor' do
subject.destroy_john_connor!
subject.current_mission.should == 'destroy: 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 == 'destroy: sarah_connor'
end

it "should protect_sarah_connor" do
subject.protect_sarah_connor!
subject.current_mission.should eq("protect: sarah_connor")
it 'should tell us terminator is bad if destroying someone' do
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
17 changes: 14 additions & 3 deletions terminatorable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ 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

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
Expand Down