Scheduler Code

From DojoWiki

Jump to: navigation, search
#
# schedule.rb - simple scheduler
# 
# An example scheduler used as a TDD example.
# 
require 'rubygems'
require 'active_support'

class Schedule

  attr_reader :frequency
  
  DEFAULT_TIME = 0
  DEFAULT_FREQUENCY = 1.day
  
  def initialize(options = {})
    @time_options = options[:start_time] || {}
    @time_options[:hour] ||= DEFAULT_TIME
    @time_options[:min] ||= DEFAULT_TIME
    @time_options[:sec] ||= DEFAULT_TIME
    @frequency = DEFAULT_FREQUENCY
  end
  
  def next_after(time)
    if (options_before?(time)) 
      time = time.tomorrow
    end
    time.change(@time_options)
  end
  
  
  def self.every
    ScheduleBuilder.new
  end
  
  def start_time
    DEFAULT_TIME
  end
  
  def ==(arg)
    return false unless nil != arg
    return @time_options == arg.time_options
  end
  
  protected
  
  def time_options
    @time_options
  end
  
  private 

  def options_before?(time)
      optime = @time_options[:hour].hours 
      + @time_options[:min].minutes + @time_options[:sec].minutes
    return optime <= time.seconds_since_midnight
  end
    
end

class ScheduleBuilder

  def day
    Schedule.new
  end
end

Back to discussion

Personal tools