diff --git a/classes/Scheduler.rb b/classes/Scheduler.rb index 6ab6365..82854be 100644 --- a/classes/Scheduler.rb +++ b/classes/Scheduler.rb @@ -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 diff --git a/tests/TestScheduler.rb b/tests/TestScheduler.rb index abfed5c..3625110 100644 --- a/tests/TestScheduler.rb +++ b/tests/TestScheduler.rb @@ -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