Class: IceCube::WeeklyRule

Constant Summary

Constants inherited from ValidatedRule

ValidatedRule::VALIDATION_ORDER

Constants inherited from Rule

Rule::INTERVAL_TYPES

Instance Attribute Summary collapse

Attributes inherited from ValidatedRule

#validations

Attributes inherited from Rule

#uses

Instance Method Summary collapse

Methods included from Validations::WeeklyInterval

#interval

Methods included from Validations::MonthOfYear

#month_of_year

Methods included from Validations::Day

#day

Methods included from Validations::DayOfWeek

#day_of_week

Methods included from Validations::SecondOfMinute

#second_of_minute

Methods included from Validations::MinuteOfHour

#minute_of_hour

Methods included from Validations::HourOfDay

#hour_of_day

Methods inherited from ValidatedRule

#base_interval_validation, #clobber_base_validations, #full_required?, #next_time, #other_interval_validations, #replace_validations_for, #reset, #to_hash, #to_ical, #to_s, #validations_for

Methods included from Validations::Until

#until, #until_time

Methods included from Deprecated

#deprecated, #deprecated_alias, schedule_options

Methods included from Validations::Count

#count, #occurrence_count

Methods included from Validations::ScheduleLock

#schedule_lock

Methods inherited from Rule

#==, daily, from_hash, from_ical, from_yaml, #hash, hourly, minutely, monthly, #next_time, #on?, #reset, secondly, #terminating?, #to_hash, #to_ical, #to_yaml, weekly, yearly

Constructor Details

#initialize(interval = 1, week_start = :sunday) ⇒ WeeklyRule

Returns a new instance of WeeklyRule.



16
17
18
19
20
21
# File 'lib/ice_cube/rules/weekly_rule.rb', line 16

def initialize(interval = 1, week_start = :sunday)
  super(interval)
  interval(interval, week_start)
  schedule_lock(:wday, :hour, :min, :sec)
  reset
end

Instance Attribute Details

#week_startObject (readonly)

Returns the value of attribute week_start.



14
15
16
# File 'lib/ice_cube/rules/weekly_rule.rb', line 14

def week_start
  @week_start
end

Instance Method Details

#realign(step_time, start_time) ⇒ Object

Move the effective start time to correct for when the schedule has validations earlier in the week than the selected start time, e.g.

Schedule.new(wednesday).weekly(2).day(:monday)

The effective start time gets realigned to the second next Monday, jumping over the gap week for the interval (2). Without realignment, the correct Monday occurrence would be missed when the schedule performs a 7-day jump into the next interval week, arriving on the Wednesday. This corrects any selections from dates that are misaligned to the schedule interval.



34
35
36
37
38
39
# File 'lib/ice_cube/rules/weekly_rule.rb', line 34

def realign(step_time, start_time)
  time = TimeUtil::TimeWrapper.new(start_time)
  offset = wday_offset(step_time, start_time)
  time.add(:day, offset)
  super step_time, time.to_time
end

#wday_offset(step_time, start_time) ⇒ Object

Calculate how many days to the first wday validation in the correct interval week. This may move backwards within the week if starting in an interval week with earlier validations.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ice_cube/rules/weekly_rule.rb', line 45

def wday_offset(step_time, start_time)
  return 0 if step_time == start_time

  wday_validations = other_interval_validations.select { |v| v.type == :wday }
  return 0 if wday_validations.none?

  days = step_time.to_date - start_time.to_date
  interval = base_interval_validation.validate(step_time, start_time).to_i
  min_wday = wday_validations.map { |v| TimeUtil.normalize_wday(v.day, week_start) }.min
  step_wday = TimeUtil.normalize_wday(step_time.wday, week_start)

  days + interval - step_wday + min_wday
end