From b0de0b1d9845935b5d027383f68c7efb514f2afc Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 4 Jan 2010 19:43:15 -0500 Subject: [PATCH] breaks --- classes/Scheduler.rb | 23 ++++++++++++++++++++--- tests/TestScheduler.rb | 11 +++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/classes/Scheduler.rb b/classes/Scheduler.rb index 402130c..71e84d7 100644 --- a/classes/Scheduler.rb +++ b/classes/Scheduler.rb @@ -4,6 +4,7 @@ class Scheduler include Singleton WEEKLY = [ 7 ] + DAILY = [ 1 ] def skip_to_dow(date, dow) if dow.is_a? String @@ -18,24 +19,40 @@ class Scheduler date end + def ok_to_add(date, breaks) + ok = true + breaks.each do |i| + if (i[:from] <= date) && (i[:to] >= date) + ok = false + end + end + ok + end + def schedule(parameters, to_produce) dates = [] if parameters[:start] current = parameters[:start] - 1.upto(to_produce) do |i| + breaks = parameters[:breaks] || [] + + while dates.length < to_produce interval = parameters[:interval].shift case interval.class.to_s when 'String' current = skip_to_dow(current, interval) - dates << current + if ok_to_add(current, breaks) + dates << current + end current += 1 when 'Fixnum' - dates << current + if ok_to_add(current, breaks) + dates << current + end current += interval end diff --git a/tests/TestScheduler.rb b/tests/TestScheduler.rb index f5e5d2d..0e68f6b 100644 --- a/tests/TestScheduler.rb +++ b/tests/TestScheduler.rb @@ -27,6 +27,17 @@ class TestScheduler < Test::Unit::TestCase 2, [ DateTime.parse('2010-01-04'), DateTime.parse('2010-01-11') ] ], + [ + { + :start => DateTime.parse('2010-01-01'), + :interval => Scheduler::DAILY, + :breaks => [ + { :from => DateTime.parse('2010-01-03'), :to => DateTime.parse('2010-01-05') } + ] + }, + 4, + [ DateTime.parse('2010-01-01'), DateTime.parse('2010-01-02'), DateTime.parse('2010-01-06'), DateTime.parse('2010-01-07') ] + ], ].each do |parameters, to_produce, expected_results| assert_equal expected_results, @scheduler.schedule(parameters, to_produce) end