Class: Reprise::Schedule
- Inherits:
-
Object
- Object
- Reprise::Schedule
- Defined in:
- lib/reprise/schedule.rb
Overview
The Reprise::Schedule
class is the primary interface of the Reprise gem.
It offers methods that enable you to:
-
Initialize a new schedule.
-
Add recurring series to the schedule via
#repeat_*
methods. -
Mark specific intervals of time as excluded from the schedule via #add_exclusion and #add_exclusions.
-
Query for the presence of occurrences within intervals of time via #occurs_between?.
-
Generate an array of all of the schedule’s occurrences via #occurrences.
Instance Method Summary collapse
-
#add_exclusion(starts_at:, ends_at:) ⇒ Object
Add a time interval between which no occurrences are valid.
-
#add_exclusions(exclusions) ⇒ void
Add time intervals between which no occurrences are valid.
-
#initialize(starts_at:, ends_at:, time_zone: nil) ⇒ Schedule
constructor
All schedules must be constructed with a valid
starts_at
andends_at
time. -
#occurrences ⇒ Array<Reprise::Core::Occurrence>
Returns an array of occurrences sorted in order of ascending occurrence start time.
-
#occurrences_between(starts_at, ends_at, include_overlapping: false) ⇒ Array<Reprise::Core::Occurrence>
This method efficiently queries your schedule for occurrences that fall within a given interval.
-
#occurs_between?(starts_at, ends_at, include_overlapping: false) ⇒ Boolean
Indicates whether one or more of your schedule’s occurrences fall within the given interval.
- #repeat_annually_by_day(day_number, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
- #repeat_daily(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
- #repeat_hourly(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
- #repeat_minutely(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
- #repeat_monthly_by_day(day_number, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
- #repeat_monthly_by_nth_weekday(weekday, nth_day, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
- #repeat_weekly(weekday, time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
Constructor Details
#initialize(starts_at:, ends_at:, time_zone: nil) ⇒ Schedule
All schedules must be constructed with a valid starts_at
and ends_at
time. Reprise does not support infinitely-recurring schedules, or the bounding of schedules on the basis of a maximum occurrence count.
38 39 40 41 42 43 44 45 |
# File 'lib/reprise/schedule.rb', line 38 def initialize(starts_at:, ends_at:, time_zone: nil) raise InvalidRangeError, "The end time cannot precede the start time" if ends_at < starts_at @starts_at = starts_at @ends_at = ends_at @time_zone = TimeZoneIdentifier.new(time_zone:, datetime_source: starts_at).to_s @default_time_of_day = TimeOfDay.new(starts_at) end |
Instance Method Details
#add_exclusion(starts_at:, ends_at:) ⇒ Object
Add a time interval between which no occurrences are valid. Any occurrences that overlap with an exclusion are removed from the schedule’s occurrences.
254 255 256 257 258 259 |
# File 'lib/reprise/schedule.rb', line 254 def add_exclusion(starts_at:, ends_at:) internal_schedule.add_exclusion( starts_at_unix_timestamp: starts_at.to_i, ends_at_unix_timestamp: ends_at.to_i ) end |
#add_exclusions(exclusions) ⇒ void
This method returns an undefined value.
Add time intervals between which no occurrences are valid. Any occurrences that overlap with an exclusion are removed from the schedule’s occurrences.
271 272 273 274 275 |
# File 'lib/reprise/schedule.rb', line 271 def add_exclusions(exclusions) internal_schedule.add_exclusions( exclusions.map {|e| e.map(&:to_i) } ) end |
#occurrences ⇒ Array<Reprise::Core::Occurrence>
Returns an array of occurrences sorted in order of ascending occurrence start time. This method is not cached; on every call, it will recompute all of the schedule’s occurrences.
50 51 52 |
# File 'lib/reprise/schedule.rb', line 50 def occurrences internal_schedule.occurrences end |
#occurrences_between(starts_at, ends_at, include_overlapping: false) ⇒ Array<Reprise::Core::Occurrence>
This method efficiently queries your schedule for occurrences that fall within a given interval.
297 298 299 300 301 302 303 |
# File 'lib/reprise/schedule.rb', line 297 def occurrences_between(starts_at, ends_at, include_overlapping: false) if include_overlapping internal_schedule.occurrences_overlapping_with_interval(starts_at.to_i, ends_at.to_i) else internal_schedule.occurrences_contained_within_interval(starts_at.to_i, ends_at.to_i) end end |
#occurs_between?(starts_at, ends_at, include_overlapping: false) ⇒ Boolean
Indicates whether one or more of your schedule’s occurrences fall within the given interval.
287 288 289 |
# File 'lib/reprise/schedule.rb', line 287 def occurs_between?(starts_at, ends_at, include_overlapping: false) occurrences_between(starts_at, ends_at, include_overlapping:).any? end |
#repeat_annually_by_day(day_number, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
This method returns an undefined value.
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/reprise/schedule.rb', line 237 def repeat_annually_by_day(day_number, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) internal_schedule.repeat_annually_by_day( day_number, time_of_day: TimeOfDay.new(time_of_day || self.starts_at).to_h, duration_in_seconds:, interval:, starts_at_unix_timestamp: starts_at.presence&.to_i, ends_at_unix_timestamp: ends_at.presence&.to_i, count:, label: ) end |
#repeat_daily(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
This method returns an undefined value.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/reprise/schedule.rb', line 143 def repeat_daily(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) internal_schedule.repeat_daily( time_of_day: TimeOfDay.new(time_of_day || self.starts_at).to_h, duration_in_seconds:, interval:, starts_at_unix_timestamp: starts_at.presence&.to_i, ends_at_unix_timestamp: ends_at.presence&.to_i, count:, label: ) end |
#repeat_hourly(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
This method returns an undefined value.
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/reprise/schedule.rb', line 124 def repeat_hourly(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) internal_schedule.repeat_hourly( time_of_day: TimeOfDay.new(time_of_day || self.starts_at).to_h, duration_in_seconds:, interval:, starts_at_unix_timestamp: starts_at.presence&.to_i, ends_at_unix_timestamp: ends_at.presence&.to_i, count:, label: ) end |
#repeat_minutely(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
This method returns an undefined value.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/reprise/schedule.rb', line 105 def repeat_minutely(time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) internal_schedule.repeat_minutely( time_of_day: TimeOfDay.new(time_of_day || self.starts_at).to_h, duration_in_seconds:, interval:, starts_at_unix_timestamp: starts_at.presence&.to_i, ends_at_unix_timestamp: ends_at.presence&.to_i, count:, label: ) end |
#repeat_monthly_by_day(day_number, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
This method returns an undefined value.
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/reprise/schedule.rb', line 191 def repeat_monthly_by_day(day_number, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) internal_schedule.repeat_monthly_by_day( day_number, time_of_day: TimeOfDay.new(time_of_day || self.starts_at).to_h, duration_in_seconds:, interval:, starts_at_unix_timestamp: starts_at.presence&.to_i, ends_at_unix_timestamp: ends_at.presence&.to_i, count:, label: ) end |
#repeat_monthly_by_nth_weekday(weekday, nth_day, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
This method returns an undefined value.
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/reprise/schedule.rb', line 213 def repeat_monthly_by_nth_weekday(weekday, nth_day, time_of_day:, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) internal_schedule.repeat_monthly_by_nth_weekday( weekday, nth_day, time_of_day: TimeOfDay.new(time_of_day || self.starts_at).to_h, duration_in_seconds:, interval:, starts_at_unix_timestamp: starts_at.presence&.to_i, ends_at_unix_timestamp: ends_at.presence&.to_i, count:, label: ) end |
#repeat_weekly(weekday, time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) ⇒ void
This method returns an undefined value.
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/reprise/schedule.rb', line 168 def repeat_weekly(weekday, time_of_day: nil, duration_in_seconds:, interval: 1, starts_at: nil, ends_at: nil, count: nil, label: nil) internal_schedule.repeat_weekly( weekday, time_of_day: TimeOfDay.new(time_of_day || self.starts_at).to_h, duration_in_seconds:, interval:, starts_at_unix_timestamp: starts_at.presence&.to_i, ends_at_unix_timestamp: ends_at.presence&.to_i, count:, label: ) end |