Skip to content
This repository has been archived by the owner on Mar 28, 2018. It is now read-only.

Commit

Permalink
Merge pull request #9 from ChalkSchools/raise_with_config
Browse files Browse the repository at this point in the history
Change interface for RaiseBomb
  • Loading branch information
QuotableWater7 committed Dec 31, 2014
2 parents 1728715 + 9a4ab37 commit fc26792
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
4 changes: 3 additions & 1 deletion lib/stink_bomb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
require 'stink_bomb/raise_bomb'

module StinkBomb
class StinkyCodeError < StandardError; end

def self.create(datetime, message: nil)
StinkBomb::RaiseBomb.new(datetime, message: message).trigger
StinkBomb::RaiseBomb.new.trigger(datetime, message: message)
end

def self.configuration
Expand Down
17 changes: 10 additions & 7 deletions lib/stink_bomb/raise_bomb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ class RaiseBomb
attr_accessor :time
attr_writer :message

def initialize(time, message: nil)
self.time = parse(time)
self.message = message || StinkBomb.configuration.message
def initialize(error_class: StinkyCodeError)
@error_class = error_class
end

def trigger
fail message if !production? && past_time?
def trigger(time, message: StinkBomb.configuration.message)
self.time = parse(time)

fail error(message) if !production? && past_time?
end

private

def parse(time)
time = Time.parse(time) if time.is_a?(String)
unless time.respond_to?(:to_time)
Expand All @@ -29,8 +32,8 @@ def past_time?
Time.now.utc > time.utc
end

def message
@message.gsub('{time}', time.to_s)
def error(message)
@error_class.new(message.gsub('{time}', time.to_s))
end
end
end
30 changes: 22 additions & 8 deletions spec/lib/stink_bomb/raise_bomb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
module StinkBomb
describe RaiseBomb, '#initialize' do
it 'raises an error if the parameter is not a date or a string' do
it 'accepts an error class to raise' do
expect do
RaiseBomb.new(1)
end.to raise_error('Parameter has to be a Time, Date, or a String')
RaiseBomb.new(error_class: StinkyTestError).trigger(Date.today - 1)
end.to raise_error(StinkyTestError, /Your code stinks!/)
end

it 'raises a StinkyCodeError by default' do
expect do
RaiseBomb.new.trigger(Date.today - 1)
end.to raise_error(StinkyCodeError, /Your code stinks!/)
end
end

describe RaiseBomb, '#trigger' do
let(:raise_bomb) { RaiseBomb.new }

it 'raises an error if the parameter is not a date or a string' do
expect do
raise_bomb.trigger(1)
end.to raise_error('Parameter has to be a Time, Date, or a String')
end

it 'does nothing if tomorrow is given' do
expect do
RaiseBomb.new(Date.today + 1).trigger
raise_bomb.trigger(Date.today + 1)
end.not_to raise_error
end

it 'does nothing if now + epsilon is given' do
expect do
RaiseBomb.new(Time.now.getlocal + 1).trigger
raise_bomb.trigger(Time.now.getlocal + 1)
end.not_to raise_error
end

it 'does not raise an error in production' do
expect(ENV).to receive(:[]).with('RAILS_ENV').and_return('production')

expect do
RaiseBomb.new(Date.today - 1).trigger
raise_bomb.trigger(Date.today - 1)
end.not_to raise_error
end

it 'raises a custom message when specified' do
yesterday = Date.today - 1
expect do
RaiseBomb.new(yesterday, message: 'Smells like poo').trigger
raise_bomb.trigger(yesterday, message: 'Smells like poo')
end.to raise_error(/Smells like poo/)
end

it 'raises an error if the given date is after today' do
yesterday = Date.today - 1
expect do
RaiseBomb.new(yesterday).trigger
raise_bomb.trigger(yesterday)
end.to raise_error(/stinks! It was supposed to be fixed by #{yesterday}/)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/stink_bomb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
describe StinkBomb do
describe '.create' do
it 'creates an instance of Bomb with the parameters' do
receive_expected = receive(:new).with('01/01/2100', message: nil)
expect(StinkBomb::RaiseBomb).to receive_expected.and_call_original
expect_any_instance_of(StinkBomb::RaiseBomb).to receive(:trigger)
bomb_class = StinkBomb::RaiseBomb
receive_expected = receive(:trigger).with('01/01/2100', message: nil)
expect_any_instance_of(bomb_class).to receive_expected.and_call_original
StinkBomb.create('01/01/2100')
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Coveralls::SimpleCov::Formatter
]
end
SimpleCov.minimum_coverage 100
SimpleCov.start { add_filter '/spec/' }

require_relative '../lib/stink_bomb'
Expand All @@ -14,3 +15,5 @@
config.filter_run focus: true
config.run_all_when_everything_filtered = true
end

class StinkyTestError < StandardError; end

0 comments on commit fc26792

Please sign in to comment.