per-day scheduling

This commit is contained in:
John Bintz 2010-01-04 19:33:58 -05:00
parent cceb3b63bd
commit a58bcf00e3
2 changed files with 26 additions and 5 deletions

View File

@ -5,6 +5,19 @@ class Scheduler
WEEKLY = [ 7 ] WEEKLY = [ 7 ]
def skip_to_dow(date, dow)
if dow.is_a? String
dow = Date::DAYNAMES.collect { |d| d.downcase }.index(dow.downcase)
end
if dow.is_a? Fixnum
while date.wday != dow
date += 1
end
end
date
end
def schedule(parameters, to_produce) def schedule(parameters, to_produce)
dates = [] dates = []
@ -15,14 +28,18 @@ class Scheduler
interval = parameters[:interval].shift interval = parameters[:interval].shift
case interval.class.to_s case interval.class.to_s
when 'Symbol' when 'String'
current = skip_to_dow(current, interval)
dates << current
current += 1
when 'Fixnum' when 'Fixnum'
dates << current
current += interval
end end
dates << current
current += interval
parameters[:interval] << interval parameters[:interval] << interval
end end
end end

View File

@ -22,7 +22,7 @@ class TestScheduler < Test::Unit::TestCase
[ [
{ {
:start => DateTime.parse('2010-01-01'), :start => DateTime.parse('2010-01-01'),
:interval => [ :mondays ] :interval => [ 'monday' ]
}, },
2, 2,
[ DateTime.parse('2010-01-04'), DateTime.parse('2010-01-11') ] [ DateTime.parse('2010-01-04'), DateTime.parse('2010-01-11') ]
@ -32,4 +32,8 @@ class TestScheduler < Test::Unit::TestCase
end end
end end
def test_skip_to_dow
assert_equal DateTime.parse('2010-01-02'), @scheduler.skip_to_dow(DateTime.parse('2010-01-01'), 6)
assert_equal DateTime.parse('2010-01-02'), @scheduler.skip_to_dow(DateTime.parse('2010-01-01'), 'saturday')
end
end end