at_index breaks
This commit is contained in:
parent
e99e08f3e7
commit
46856b7b05
@ -19,11 +19,19 @@ class Scheduler
|
||||
date
|
||||
end
|
||||
|
||||
def ok_to_add(date, breaks)
|
||||
def ok_to_add(date, index, prior, breaks)
|
||||
ok = true
|
||||
breaks.each do |i|
|
||||
if (i['from'] <= date) && (i['to'] >= date)
|
||||
ok = false
|
||||
if i['from'] && i['to']
|
||||
if (i['from'] <= date) && (i['to'] >= date)
|
||||
ok = false
|
||||
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
|
||||
@ -34,9 +42,11 @@ class Scheduler
|
||||
|
||||
if parameters['start']
|
||||
current = parameters['start']
|
||||
prior = nil
|
||||
|
||||
breaks = parameters['breaks'] || []
|
||||
|
||||
index = 0
|
||||
while dates.length < to_produce
|
||||
interval = parameters['interval'].shift
|
||||
|
||||
@ -44,14 +54,18 @@ class Scheduler
|
||||
when 'String'
|
||||
current = skip_to_dow(current, interval)
|
||||
|
||||
if ok_to_add(current, breaks)
|
||||
if ok_to_add(current, index, prior, breaks)
|
||||
dates << current
|
||||
prior = current
|
||||
index += 1
|
||||
end
|
||||
|
||||
current += 1
|
||||
when 'Fixnum'
|
||||
if ok_to_add(current, breaks)
|
||||
if ok_to_add(current, index, prior, breaks)
|
||||
dates << current
|
||||
prior = current
|
||||
index += 1
|
||||
end
|
||||
|
||||
current += interval
|
||||
|
@ -38,6 +38,20 @@ class TestScheduler < Test::Unit::TestCase
|
||||
4,
|
||||
[ 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|
|
||||
assert_equal expected_results, @scheduler.schedule(parameters, to_produce)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user