Skip to content

Commit

Permalink
The passage of time made me realize that my initial implementation wa…
Browse files Browse the repository at this point in the history
…s littered with errors. Fix weekend sending issues
  • Loading branch information
brennandunn committed Jul 13, 2012
1 parent f05f893 commit d46c27b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
15 changes: 9 additions & 6 deletions lib/dripper.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 ||= []
Expand All @@ -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={})
Expand Down
12 changes: 6 additions & 6 deletions spec/dripper_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit d46c27b

Please sign in to comment.