at_index breaks

This commit is contained in:
John Bintz 2010-01-04 21:34:57 -05:00
parent e99e08f3e7
commit 46856b7b05
2 changed files with 33 additions and 5 deletions

View File

@ -19,13 +19,21 @@ class Scheduler
date date
end end
def ok_to_add(date, breaks) def ok_to_add(date, index, prior, breaks)
ok = true ok = true
breaks.each do |i| breaks.each do |i|
if i['from'] && i['to']
if (i['from'] <= date) && (i['to'] >= date) if (i['from'] <= date) && (i['to'] >= date)
ok = false ok = false
end end
end end
if i['at_index'] && i['for_days'] && prior
if i['at_index'] == index
ok = (date > (prior + i['for_days']))
end
end
end
ok ok
end end
@ -34,9 +42,11 @@ class Scheduler
if parameters['start'] if parameters['start']
current = parameters['start'] current = parameters['start']
prior = nil
breaks = parameters['breaks'] || [] breaks = parameters['breaks'] || []
index = 0
while dates.length < to_produce while dates.length < to_produce
interval = parameters['interval'].shift interval = parameters['interval'].shift
@ -44,14 +54,18 @@ class Scheduler
when 'String' when 'String'
current = skip_to_dow(current, interval) current = skip_to_dow(current, interval)
if ok_to_add(current, breaks) if ok_to_add(current, index, prior, breaks)
dates << current dates << current
prior = current
index += 1
end end
current += 1 current += 1
when 'Fixnum' when 'Fixnum'
if ok_to_add(current, breaks) if ok_to_add(current, index, prior, breaks)
dates << current dates << current
prior = current
index += 1
end end
current += interval current += interval

View File

@ -38,6 +38,20 @@ class TestScheduler < Test::Unit::TestCase
4, 4,
[ DateTime.parse('2010-01-01'), DateTime.parse('2010-01-02'), DateTime.parse('2010-01-06'), DateTime.parse('2010-01-07') ] [ DateTime.parse('2010-01-01'), DateTime.parse('2010-01-02'), DateTime.parse('2010-01-06'), DateTime.parse('2010-01-07') ]
], ],
[
{
'start' => DateTime.parse('2010-01-01'),
'interval' => [ 'monday', 'wednesday', 'friday' ],
'breaks' => [
{ 'at_index' => 3, 'for_days' => 7 }
]
},
6,
[
DateTime.parse('2010-01-04'), DateTime.parse('2010-01-06'), DateTime.parse('2010-01-08'),
DateTime.parse('2010-01-18'), DateTime.parse('2010-01-20'), DateTime.parse('2010-01-22')
]
],
].each do |parameters, to_produce, expected_results| ].each do |parameters, to_produce, expected_results|
assert_equal expected_results, @scheduler.schedule(parameters, to_produce) assert_equal expected_results, @scheduler.schedule(parameters, to_produce)
end end