per-day scheduling
This commit is contained in:
parent
cceb3b63bd
commit
a58bcf00e3
@ -5,6 +5,19 @@ class Scheduler
|
||||
|
||||
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)
|
||||
dates = []
|
||||
|
||||
@ -15,14 +28,18 @@ class Scheduler
|
||||
interval = parameters[:interval].shift
|
||||
|
||||
case interval.class.to_s
|
||||
when 'Symbol'
|
||||
when 'String'
|
||||
current = skip_to_dow(current, interval)
|
||||
|
||||
dates << current
|
||||
|
||||
current += 1
|
||||
when 'Fixnum'
|
||||
dates << current
|
||||
|
||||
current += interval
|
||||
end
|
||||
|
||||
dates << current
|
||||
|
||||
current += interval
|
||||
parameters[:interval] << interval
|
||||
end
|
||||
end
|
||||
|
@ -22,7 +22,7 @@ class TestScheduler < Test::Unit::TestCase
|
||||
[
|
||||
{
|
||||
:start => DateTime.parse('2010-01-01'),
|
||||
:interval => [ :mondays ]
|
||||
:interval => [ 'monday' ]
|
||||
},
|
||||
2,
|
||||
[ DateTime.parse('2010-01-04'), DateTime.parse('2010-01-11') ]
|
||||
@ -32,4 +32,8 @@ class TestScheduler < Test::Unit::TestCase
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user