class Chrono::NextTime

Attributes

now[R]
source[R]
time[W]

Public Class Methods

new(options) click to toggle source
# File lib/chrono/next_time.rb, line 18
def initialize(options)
  @now = options[:now]
  @source = options[:source]
end

Public Instance Methods

to_time() click to toggle source
# File lib/chrono/next_time.rb, line 23
def to_time
  # the longest cycle is 4 years (leap year)
  # Note that the combination of day-month and wday is OR
  max_time = time + (365 * 3 + 366).days
  while @time < max_time
    case
    when !scheduled_in_this_month?
      carry_month
    when !scheduled_in_this_day?
      carry_day
    when !scheduled_in_this_hour?
      carry_hour
    when !scheduled_in_this_minute?
      carry_minute
    else
      return @time
    end
  end
  raise ArgumentError, "invalid cron string '#{@source}'"
end

Private Instance Methods

carry_day() click to toggle source
# File lib/chrono/next_time.rb, line 86
def carry_day
  self.time = time.tomorrow.at_beginning_of_day
end
carry_hour() click to toggle source
# File lib/chrono/next_time.rb, line 90
def carry_hour
  self.time = (time + 1.hour).at_beginning_of_hour
end
carry_minute() click to toggle source
# File lib/chrono/next_time.rb, line 94
def carry_minute
  self.time = (time + 1.minute).change(sec: 0)
end
carry_month() click to toggle source
# File lib/chrono/next_time.rb, line 82
def carry_month
  self.time = time.next_month.at_beginning_of_month
end
schedule() click to toggle source
# File lib/chrono/next_time.rb, line 50
def schedule
  @schedule ||= Schedule.new(source)
end
scheduled_in_this_day?() click to toggle source
# File lib/chrono/next_time.rb, line 58
def scheduled_in_this_day?
  if schedule.days?
    if schedule.wdays?
      schedule.days.include?(time.day) || schedule.wdays.include?(time.wday)
    else
      schedule.days.include?(time.day)
    end
  else
    if schedule.wdays?
      schedule.wdays.include?(time.wday)
    else
      true
    end
  end
end
scheduled_in_this_hour?() click to toggle source
# File lib/chrono/next_time.rb, line 74
def scheduled_in_this_hour?
  schedule.hours.include?(time.hour)
end
scheduled_in_this_minute?() click to toggle source
# File lib/chrono/next_time.rb, line 78
def scheduled_in_this_minute?
  schedule.minutes.include?(time.min)
end
scheduled_in_this_month?() click to toggle source
# File lib/chrono/next_time.rb, line 54
def scheduled_in_this_month?
  schedule.months.include?(time.month)
end
time() click to toggle source
# File lib/chrono/next_time.rb, line 46
def time
  @time ||= (now + 1.minute).change(sec: 0)
end