minicomic-backend/classes/Scheduler.rb

50 lines
860 B
Ruby
Raw Normal View History

2010-01-04 04:43:58 +00:00
require 'singleton'
class Scheduler
include Singleton
WEEKLY = [ 7 ]
2010-01-05 00:33:58 +00:00
def skip_to_dow(date, dow)
if dow.is_a? String
dow = Date::DAYNAMES.collect { |d| d.downcase }.index(dow.downcase)
end
if dow.is_a? Fixnum
while date.wday != dow
date += 1
end
end
date
end
2010-01-04 04:43:58 +00:00
def schedule(parameters, to_produce)
dates = []
if parameters[:start]
current = parameters[:start]
1.upto(to_produce) do |i|
interval = parameters[:interval].shift
case interval.class.to_s
2010-01-05 00:33:58 +00:00
when 'String'
current = skip_to_dow(current, interval)
dates << current
2010-01-04 04:43:58 +00:00
2010-01-05 00:33:58 +00:00
current += 1
2010-01-04 04:43:58 +00:00
when 'Fixnum'
2010-01-05 00:33:58 +00:00
dates << current
2010-01-04 04:43:58 +00:00
2010-01-05 00:33:58 +00:00
current += interval
end
2010-01-04 04:43:58 +00:00
parameters[:interval] << interval
end
end
dates
end
end