Class: IceCube::WeeklyRule

Inherits:
ValidatedRule show all
Includes:
Validations::WeeklyInterval
Defined in:
lib/ice_cube/rules/weekly_rule.rb

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 inherited from ValidatedRule

#base_interval_type, #base_interval_validation, #clobber_base_validations, #dst_adjust?, #next_time, #other_interval_validations, #replace_validations_for, #reset, #skipped_for_dst, #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::DayOfYear

#day_of_year

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::DayOfMonth

#day_of_month

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 included from Validations::ScheduleLock

#schedule_lock

Methods inherited from Rule

#==, daily, from_hash, from_ical, from_yaml, #full_required?, #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.



9
10
11
12
13
14
# File 'lib/ice_cube/rules/weekly_rule.rb', line 9

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.



7
8
9
# File 'lib/ice_cube/rules/weekly_rule.rb', line 7

def week_start
  @week_start
end

Instance Method Details

#realign(step_time, start_time) ⇒ Object

Calculate the effective start time for when the given start time is later in the week than one of the weekday validations, such that times could be missed by a 7-day jump using the weekly interval, or when selecting from a date that is misaligned from the schedule interval.



21
22
23
24
25
26
# File 'lib/ice_cube/rules/weekly_rule.rb', line 21

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

#wday_offset(step_time, start_time) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ice_cube/rules/weekly_rule.rb', line 28

def wday_offset(step_time, start_time)
  wday_validations = other_interval_validations.select { |v| v.type == :wday }
  return if wday_validations.none?

  days = (step_time - start_time).to_i / ONE_DAY
  interval = base_interval_validation.validate(step_time, start_time).to_i
  min_wday = TimeUtil.normalize_wday(wday_validations.min_by(&:day).day, week_start)
  step_wday = TimeUtil.normalize_wday(step_time.wday, week_start)

  days + interval - step_wday + min_wday
end