breaks
This commit is contained in:
parent
a58bcf00e3
commit
b0de0b1d98
@ -4,6 +4,7 @@ class Scheduler
|
|||||||
include Singleton
|
include Singleton
|
||||||
|
|
||||||
WEEKLY = [ 7 ]
|
WEEKLY = [ 7 ]
|
||||||
|
DAILY = [ 1 ]
|
||||||
|
|
||||||
def skip_to_dow(date, dow)
|
def skip_to_dow(date, dow)
|
||||||
if dow.is_a? String
|
if dow.is_a? String
|
||||||
@ -18,24 +19,40 @@ class Scheduler
|
|||||||
date
|
date
|
||||||
end
|
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)
|
def schedule(parameters, to_produce)
|
||||||
dates = []
|
dates = []
|
||||||
|
|
||||||
if parameters[:start]
|
if parameters[:start]
|
||||||
current = parameters[:start]
|
current = parameters[:start]
|
||||||
|
|
||||||
1.upto(to_produce) do |i|
|
breaks = parameters[:breaks] || []
|
||||||
|
|
||||||
|
while dates.length < to_produce
|
||||||
interval = parameters[:interval].shift
|
interval = parameters[:interval].shift
|
||||||
|
|
||||||
case interval.class.to_s
|
case interval.class.to_s
|
||||||
when 'String'
|
when 'String'
|
||||||
current = skip_to_dow(current, interval)
|
current = skip_to_dow(current, interval)
|
||||||
|
|
||||||
dates << current
|
if ok_to_add(current, breaks)
|
||||||
|
dates << current
|
||||||
|
end
|
||||||
|
|
||||||
current += 1
|
current += 1
|
||||||
when 'Fixnum'
|
when 'Fixnum'
|
||||||
dates << current
|
if ok_to_add(current, breaks)
|
||||||
|
dates << current
|
||||||
|
end
|
||||||
|
|
||||||
current += interval
|
current += interval
|
||||||
end
|
end
|
||||||
|
@ -27,6 +27,17 @@ class TestScheduler < Test::Unit::TestCase
|
|||||||
2,
|
2,
|
||||||
[ DateTime.parse('2010-01-04'), DateTime.parse('2010-01-11') ]
|
[ 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|
|
].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
|
||||||
|
Loading…
Reference in New Issue
Block a user