From d46c27b564c922d918bf7557fdd7de46aacf65da Mon Sep 17 00:00:00 2001 From: Brennan Dunn Date: Thu, 12 Jul 2012 20:02:58 -0400 Subject: [PATCH] The passage of time made me realize that my initial implementation was littered with errors. Fix weekend sending issues --- lib/dripper.rb | 15 +++++++++------ spec/dripper_spec.rb | 12 ++++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/dripper.rb b/lib/dripper.rb index f05e29d..73a26b0 100644 --- a/lib/dripper.rb +++ b/lib/dripper.rb @@ -19,10 +19,13 @@ def starting_time def offset_time(offset) calculated_time = starting_time + offset - if calculated_time.wday == 6 # Saturday - calculated_time = calculated_time - 1.day - elsif calculated_time.wday == 0 # Sunday - calculated_time = calculated_time + 1.day + options = self.class.send_at_options + unless options[:weekends] + if calculated_time.wday == 6 # Saturday + calculated_time = calculated_time - 1.day + elsif calculated_time.wday == 0 # Sunday + calculated_time = calculated_time + 1.day + end end calculated_time end @@ -50,7 +53,7 @@ def enqueue(time) module ClassMethods def send_at_offset ; @send_at_offset ; end - def send_at_options ; @send_at_options ; end + def send_at_options ; @send_at_options || { :weekends => true } ; end def after_blocks @after_blocks ||= [] @@ -62,7 +65,7 @@ def after(offset, &block) def send_at(offset_array, options={}) @send_at_offset = offset_array - @send_at_options = options + @send_at_options = { :weekends => true }.merge(options) end def perform(options={}) diff --git a/spec/dripper_spec.rb b/spec/dripper_spec.rb index 4f48f0c..9fe2835 100644 --- a/spec/dripper_spec.rb +++ b/spec/dripper_spec.rb @@ -43,17 +43,17 @@ it 'sends on a Friday for Saturday events' do subject.after(5.days) {} - instance.should_receive(:created_at).and_return { Time.now.beginning_of_week } # Monday - drip = subject.new(instance) - drip.scheduled_times.should == [(instance.created_at + 4.days).beginning_of_day + 9.hours] # Friday, 9am + new_instance = stub(id: 1, created_at: Time.now.beginning_of_week) + drip = subject.new(new_instance) + drip.scheduled_times.should == [(new_instance.created_at + 4.days).beginning_of_day + 9.hours] # Friday, 9am end it 'sends on a Monday for Sunday events' do subject.after(6.days) {} - instance.should_receive(:created_at).and_return { Time.now.beginning_of_week } # Monday - drip = subject.new(instance) - drip.scheduled_times.should == [(instance.created_at + 7.days).beginning_of_day + 9.hours] # Monday, 9am + new_instance = stub(id: 1, created_at: Time.now.beginning_of_week) + drip = subject.new(new_instance) + drip.scheduled_times.should == [(new_instance.created_at + 7.days).beginning_of_day + 9.hours] # Monday, 9am end end