class Cyclical::MonthlyRule

holds weekly rule configuration

Public Instance Methods

aligned?(time, base) click to toggle source

check if time is aligned to a base time, including interval check

# File lib/cyclical/rules/monthly_rule.rb, line 8
def aligned?(time, base)
  return false unless ((12 * base.year + base.mon) - (12 * time.year + time.mon)) % @interval == 0
  return false unless [time.hour, time.min, time.sec] == [base.hour, base.min, base.sec] # the shortest filter we support is for days
  return false unless base.day == time.day || monthday_filters

  true
end
step() click to toggle source

default step of the rule

# File lib/cyclical/rules/monthly_rule.rb, line 17
def step
  @interval.months
end

Protected Instance Methods

align(time, base) click to toggle source
# File lib/cyclical/rules/monthly_rule.rb, line 41
def align(time, base)
  time = time.beginning_of_month + (base.day - 1).days unless time.day == base.day || monthday_filters

  # compensate crossing DST barrier (oh my...)
  offset = time.beginning_of_day.utc_offset
  time = time.beginning_of_day + base.hour.hours + base.min.minutes + base.sec.seconds
  time += (offset - time.utc_offset)

  time
end
monthday_filters() click to toggle source
# File lib/cyclical/rules/monthly_rule.rb, line 52
def monthday_filters
  filters(:weekdays) || filters(:monthdays) || filters(:yeardays) || filters(:weeks) || filters(:months)
end
potential_next(current, base) click to toggle source
Calls superclass method Cyclical::Rule#potential_next
# File lib/cyclical/rules/monthly_rule.rb, line 23
def potential_next(current, base)
  candidate = super(current, base)

  rem = ((12 * base.year + base.mon) - (12 * candidate.year + candidate.mon)) % @interval
  return candidate if rem == 0

  (candidate + rem.months).beginning_of_month
end
potential_previous(current, base) click to toggle source
Calls superclass method Cyclical::Rule#potential_previous
# File lib/cyclical/rules/monthly_rule.rb, line 32
def potential_previous(current, base)
  candidate = super(current, base)

  rem = ((12 * base.year + base.mon) - (12 * candidate.year + candidate.mon)) % @interval
  return candidate if rem == 0

  (candidate + rem.months - step).end_of_month
end