Class: IceCube::Schedule
- Inherits:
-
Object
- Object
- IceCube::Schedule
- Extended by:
- Deprecated
- Defined in:
- lib/ice_cube/schedule.rb
Instance Attribute Summary collapse
-
#end_time ⇒ Object
Get the end time.
-
#start_time ⇒ Object
Get the start time.
Class Method Summary collapse
- .dump(schedule) ⇒ Object
-
.from_hash(original_hash, options = {}) ⇒ Object
Load the schedule from a hash.
-
.from_ical(ical, options = {}) ⇒ Object
Load the schedule from ical.
-
.from_yaml(yaml, options = {}) ⇒ Object
Load the schedule from yaml.
- .load(yaml) ⇒ Object
Instance Method Summary collapse
-
#add_exception_rule(rule) ⇒ Object
(also: #exrule)
Add an exception rule to the schedule.
-
#add_exception_time(time) ⇒ Object
(also: #extime)
Add an exception time to the schedule.
-
#add_recurrence_rule(rule) ⇒ Object
(also: #rrule)
Add a recurrence rule to the schedule.
-
#add_recurrence_time(time) ⇒ Object
(also: #rtime)
Add a recurrence time to the schedule.
-
#all_occurrences ⇒ Object
All of the occurrences.
-
#all_occurrences_enumerator ⇒ Object
Emit an enumerator based on the start time.
-
#conflicts_with?(other_schedule, closing_time = nil) ⇒ Boolean
Determine if this schedule conflicts with another schedule.
- #duration ⇒ Object
- #duration=(seconds) ⇒ Object
-
#each_occurrence(&block) ⇒ Object
Iterate forever.
-
#exception_rules ⇒ Object
(also: #exrules)
Get the exception rules.
-
#exception_times ⇒ Object
(also: #extimes)
Get the exception times that are on the schedule.
-
#first(n = nil) ⇒ Object
Get the first n occurrences, or the first occurrence if n is skipped.
-
#initialize(start_time = nil, options = {}) {|_self| ... } ⇒ Schedule
constructor
Create a new schedule.
-
#last(n = nil) ⇒ Object
Get the final n occurrences of a terminating schedule or the final one if no n is given.
-
#max_duration ⇒ Object
Return the maximum value between schedule’s duration and the durations of all the recurrence and exception rules included in this schedule.
-
#next_occurrence(from = nil, options = {}) ⇒ Object
The next occurrence after now (overridable).
-
#next_occurrences(num, from = nil, options = {}) ⇒ Object
The next n occurrences after now.
-
#occurrences(closing_time) ⇒ Object
Get all of the occurrences from the start_time up until a given Time.
-
#occurrences_between(begin_time, closing_time, options = {}) ⇒ Object
Occurrences between two times.
-
#occurring_at?(time) ⇒ Boolean
Determine if the schedule is occurring at a given time.
-
#occurring_between?(opening_time, closing_time) ⇒ Boolean
Return a boolean indicating if an occurrence is occurring between two times, inclusive of its duration.
-
#occurs_at?(time) ⇒ Boolean
Determine if the schedule occurs at a specific time.
-
#occurs_between?(begin_time, closing_time, options = {}) ⇒ Boolean
Return a boolean indicating if an occurrence falls between two times.
-
#occurs_on?(date) ⇒ Boolean
Return a boolean indicating if an occurrence falls on a certain date.
-
#previous_occurrence(from) ⇒ Object
The previous occurrence from a given time.
-
#previous_occurrences(num, from) ⇒ Object
The previous n occurrences before a given time.
-
#recurrence_rules ⇒ Object
(also: #rrules)
Get the recurrence rules.
-
#recurrence_times ⇒ Object
(also: #rtimes)
Get the recurrence times that are on the schedule.
-
#remaining_occurrences(from = nil, options = {}) ⇒ Object
The remaining occurrences (same requirements as all_occurrences).
-
#remaining_occurrences_enumerator(from = nil, options = {}) ⇒ Object
Returns an enumerator for all remaining occurrences.
-
#remove_exception_rule(rule) ⇒ Object
Remove an exception rule.
-
#remove_exception_time(time) ⇒ Object
(also: #remove_extime)
Remove an exception time.
-
#remove_recurrence_rule(rule) ⇒ Object
Remove a recurrence rule.
-
#remove_recurrence_time(time) ⇒ Object
(also: #remove_rtime)
Remove a recurrence time.
-
#terminating? ⇒ Boolean
Determine if the schedule will end.
-
#to_hash ⇒ Object
Convert the schedule to a hash.
-
#to_ical(force_utc = false) ⇒ Object
Serialize this schedule to_ical.
-
#to_s ⇒ Object
String serialization.
-
#to_yaml(*args) ⇒ Object
Convert the schedule to yaml.
Methods included from Deprecated
deprecated, deprecated_alias, schedule_options
Constructor Details
#initialize(start_time = nil, options = {}) {|_self| ... } ⇒ Schedule
Create a new schedule
18 19 20 21 22 23 24 25 |
# File 'lib/ice_cube/schedule.rb', line 18 def initialize(start_time = nil, = {}) self.start_time = start_time || TimeUtil.now self.end_time = self.start_time + [:duration] if [:duration] self.end_time = [:end_time] if [:end_time] @all_recurrence_rules = [] @all_exception_rules = [] yield self if block_given? end |
Instance Attribute Details
#end_time ⇒ Object
Get the end time
14 15 16 |
# File 'lib/ice_cube/schedule.rb', line 14 def end_time @end_time end |
#start_time ⇒ Object
Get the start time
10 11 12 |
# File 'lib/ice_cube/schedule.rb', line 10 def start_time @start_time end |
Class Method Details
.dump(schedule) ⇒ Object
402 403 404 405 |
# File 'lib/ice_cube/schedule.rb', line 402 def self.dump(schedule) return schedule if schedule.nil? || schedule == "" schedule.to_yaml end |
.from_hash(original_hash, options = {}) ⇒ Object
Load the schedule from a hash
389 390 391 392 393 394 |
# File 'lib/ice_cube/schedule.rb', line 389 def self.from_hash(original_hash, = {}) HashParser.new(original_hash).to_schedule do |schedule| Deprecated.(schedule, ) yield schedule if block_given? end end |
.from_ical(ical, options = {}) ⇒ Object
Load the schedule from ical
352 353 354 |
# File 'lib/ice_cube/schedule.rb', line 352 def self.from_ical(ical, = {}) IcalParser.schedule_from_ical(ical, ) end |
.from_yaml(yaml, options = {}) ⇒ Object
Load the schedule from yaml
362 363 364 365 366 367 |
# File 'lib/ice_cube/schedule.rb', line 362 def self.from_yaml(yaml, = {}) YamlParser.new(yaml).to_schedule do |schedule| Deprecated.(schedule, ) yield schedule if block_given? end end |
.load(yaml) ⇒ Object
407 408 409 410 |
# File 'lib/ice_cube/schedule.rb', line 407 def self.load(yaml) return yaml if yaml.nil? || yaml == "" from_yaml(yaml) end |
Instance Method Details
#add_exception_rule(rule) ⇒ Object Also known as: exrule
Add an exception rule to the schedule
92 93 94 |
# File 'lib/ice_cube/schedule.rb', line 92 def add_exception_rule(rule) @all_exception_rules << rule unless @all_exception_rules.include?(rule) end |
#add_exception_time(time) ⇒ Object Also known as: extime
Add an exception time to the schedule
69 70 71 72 73 74 |
# File 'lib/ice_cube/schedule.rb', line 69 def add_exception_time(time) return nil if time.nil? rule = SingleOccurrenceRule.new(time) add_exception_rule rule time end |
#add_recurrence_rule(rule) ⇒ Object Also known as: rrule
Add a recurrence rule to the schedule
80 81 82 |
# File 'lib/ice_cube/schedule.rb', line 80 def add_recurrence_rule(rule) @all_recurrence_rules << rule unless @all_recurrence_rules.include?(rule) end |
#add_recurrence_time(time) ⇒ Object Also known as: rtime
Add a recurrence time to the schedule
58 59 60 61 62 63 |
# File 'lib/ice_cube/schedule.rb', line 58 def add_recurrence_time(time) return nil if time.nil? rule = SingleOccurrenceRule.new(time) add_recurrence_rule rule time end |
#all_occurrences ⇒ Object
All of the occurrences
162 163 164 165 |
# File 'lib/ice_cube/schedule.rb', line 162 def all_occurrences require_terminating_rules enumerate_occurrences(start_time).to_a end |
#all_occurrences_enumerator ⇒ Object
Emit an enumerator based on the start time
168 169 170 |
# File 'lib/ice_cube/schedule.rb', line 168 def all_occurrences_enumerator enumerate_occurrences(start_time) end |
#conflicts_with?(other_schedule, closing_time = nil) ⇒ Boolean
Determine if this schedule conflicts with another schedule
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/ice_cube/schedule.rb', line 270 def conflicts_with?(other_schedule, closing_time = nil) closing_time = TimeUtil.ensure_time(closing_time) unless terminating? || other_schedule.terminating? || closing_time raise ArgumentError, "One or both schedules must be terminating to use #conflicts_with?" end # Pick the terminating schedule, and other schedule # No need to reverse if terminating? or there is a closing time terminating_schedule = self unless terminating? || closing_time terminating_schedule, other_schedule = other_schedule, terminating_schedule end # Go through each occurrence of the terminating schedule and determine # if the other occurs at that time # last_time = nil terminating_schedule.each_occurrence do |time| if closing_time && time > closing_time last_time = closing_time break end last_time = time return true if other_schedule.occurring_at?(time) end # Due to durations, we need to walk up to the end time, and verify in the # other direction if last_time last_time += terminating_schedule.duration other_schedule.each_occurrence do |time| break if time > last_time return true if terminating_schedule.occurring_at?(time) end end # No conflict, return false false end |
#duration ⇒ Object
39 40 41 |
# File 'lib/ice_cube/schedule.rb', line 39 def duration end_time ? end_time - start_time : 0 end |
#duration=(seconds) ⇒ Object
43 44 45 |
# File 'lib/ice_cube/schedule.rb', line 43 def duration=(seconds) @end_time = start_time + seconds end |
#each_occurrence(&block) ⇒ Object
Iterate forever
173 174 175 176 |
# File 'lib/ice_cube/schedule.rb', line 173 def each_occurrence(&block) enumerate_occurrences(start_time, &block).to_a self end |
#exception_rules ⇒ Object Also known as: exrules
Get the exception rules
110 111 112 |
# File 'lib/ice_cube/schedule.rb', line 110 def exception_rules @all_exception_rules.reject { |r| r.is_a?(SingleOccurrenceRule) } end |
#exception_times ⇒ Object Also known as: extimes
Get the exception times that are on the schedule
136 137 138 |
# File 'lib/ice_cube/schedule.rb', line 136 def exception_times @all_exception_rules.select { |r| r.is_a?(SingleOccurrenceRule) }.map(&:time) end |
#first(n = nil) ⇒ Object
Get the first n occurrences, or the first occurrence if n is skipped
312 313 314 315 |
# File 'lib/ice_cube/schedule.rb', line 312 def first(n = nil) occurrences = enumerate_occurrences(start_time).take(n || 1) n.nil? ? occurrences.first : occurrences end |
#last(n = nil) ⇒ Object
Get the final n occurrences of a terminating schedule or the final one if no n is given
319 320 321 322 323 |
# File 'lib/ice_cube/schedule.rb', line 319 def last(n = nil) require_terminating_rules occurrences = enumerate_occurrences(start_time).to_a n.nil? ? occurrences.last : occurrences[-n..-1] end |
#max_duration ⇒ Object
Return the maximum value between schedule’s duration and the durations of all the recurrence and exception rules included in this schedule
50 51 52 53 54 55 |
# File 'lib/ice_cube/schedule.rb', line 50 def max_duration all_durations = [duration] all_durations += @all_recurrence_rules.map { |r| r.duration }.compact all_durations += @all_exception_rules.map { |r| r.duration }.compact all_durations.max end |
#next_occurrence(from = nil, options = {}) ⇒ Object
The next occurrence after now (overridable)
185 186 187 188 189 190 |
# File 'lib/ice_cube/schedule.rb', line 185 def next_occurrence(from = nil, = {}) from = TimeUtil.match_zone(from, start_time) || TimeUtil.now(start_time) enumerate_occurrences(from + 1, nil, ).next rescue StopIteration nil end |
#next_occurrences(num, from = nil, options = {}) ⇒ Object
The next n occurrences after now
179 180 181 182 |
# File 'lib/ice_cube/schedule.rb', line 179 def next_occurrences(num, from = nil, = {}) from = TimeUtil.match_zone(from, start_time) || TimeUtil.now(start_time) enumerate_occurrences(from + 1, nil, ).take(num) end |
#occurrences(closing_time) ⇒ Object
Get all of the occurrences from the start_time up until a given Time
157 158 159 |
# File 'lib/ice_cube/schedule.rb', line 157 def occurrences(closing_time) enumerate_occurrences(start_time, closing_time).to_a end |
#occurrences_between(begin_time, closing_time, options = {}) ⇒ Object
Occurrences between two times
221 222 223 |
# File 'lib/ice_cube/schedule.rb', line 221 def occurrences_between(begin_time, closing_time, = {}) enumerate_occurrences(begin_time, closing_time, ).to_a end |
#occurring_at?(time) ⇒ Boolean
Determine if the schedule is occurring at a given time
251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/ice_cube/schedule.rb', line 251 def occurring_at?(time) time = TimeUtil.match_zone(time, start_time) or raise ArgumentError, "Time required, got #{time.inspect}" if max_duration > 0 return false if exception_time?(time) begin enumerate_occurrences(time, time, :spans => true).next true rescue StopIteration false end else occurs_at?(time) end end |
#occurring_between?(opening_time, closing_time) ⇒ Boolean
Return a boolean indicating if an occurrence is occurring between two times, inclusive of its duration. This counts zero-length occurrences that intersect the start of the range and within the range, but not occurrences at the end of the range since none of their duration intersects the range.
238 239 240 |
# File 'lib/ice_cube/schedule.rb', line 238 def occurring_between?(opening_time, closing_time) occurs_between?(opening_time, closing_time, :spans => true) end |
#occurs_at?(time) ⇒ Boolean
Determine if the schedule occurs at a specific time
307 308 309 |
# File 'lib/ice_cube/schedule.rb', line 307 def occurs_at?(time) occurs_between?(time, time) end |
#occurs_between?(begin_time, closing_time, options = {}) ⇒ Boolean
Return a boolean indicating if an occurrence falls between two times
226 227 228 229 230 231 |
# File 'lib/ice_cube/schedule.rb', line 226 def occurs_between?(begin_time, closing_time, = {}) enumerate_occurrences(begin_time, closing_time, ).next true rescue StopIteration false end |
#occurs_on?(date) ⇒ Boolean
Return a boolean indicating if an occurrence falls on a certain date
243 244 245 246 247 248 |
# File 'lib/ice_cube/schedule.rb', line 243 def occurs_on?(date) date = TimeUtil.ensure_date(date) begin_time = TimeUtil.beginning_of_date(date, start_time) closing_time = TimeUtil.end_of_date(date, start_time) occurs_between?(begin_time, closing_time) end |
#previous_occurrence(from) ⇒ Object
The previous occurrence from a given time
193 194 195 196 197 |
# File 'lib/ice_cube/schedule.rb', line 193 def previous_occurrence(from) from = TimeUtil.match_zone(from, start_time) or raise ArgumentError, "Time required, got #{time.inspect}" return nil if from <= start_time enumerate_occurrences(start_time, from - 1).to_a.last end |
#previous_occurrences(num, from) ⇒ Object
The previous n occurrences before a given time
200 201 202 203 204 205 |
# File 'lib/ice_cube/schedule.rb', line 200 def previous_occurrences(num, from) from = TimeUtil.match_zone(from, start_time) or raise ArgumentError, "Time required, got #{time.inspect}" return [] if from <= start_time a = enumerate_occurrences(start_time, from - 1).to_a a.size > num ? a[-1*num,a.size] : a end |
#recurrence_rules ⇒ Object Also known as: rrules
Get the recurrence rules
104 105 106 |
# File 'lib/ice_cube/schedule.rb', line 104 def recurrence_rules @all_recurrence_rules.reject { |r| r.is_a?(SingleOccurrenceRule) } end |
#recurrence_times ⇒ Object Also known as: rtimes
Get the recurrence times that are on the schedule
116 117 118 |
# File 'lib/ice_cube/schedule.rb', line 116 def recurrence_times @all_recurrence_rules.select { |r| r.is_a?(SingleOccurrenceRule) }.map(&:time) end |
#remaining_occurrences(from = nil, options = {}) ⇒ Object
The remaining occurrences (same requirements as all_occurrences)
208 209 210 211 212 |
# File 'lib/ice_cube/schedule.rb', line 208 def remaining_occurrences(from = nil, = {}) require_terminating_rules from ||= TimeUtil.now(@start_time) enumerate_occurrences(from, nil, ).to_a end |
#remaining_occurrences_enumerator(from = nil, options = {}) ⇒ Object
Returns an enumerator for all remaining occurrences
215 216 217 218 |
# File 'lib/ice_cube/schedule.rb', line 215 def remaining_occurrences_enumerator(from = nil, = {}) from ||= TimeUtil.now(@start_time) enumerate_occurrences(from, nil, ) end |
#remove_exception_rule(rule) ⇒ Object
Remove an exception rule
98 99 100 101 |
# File 'lib/ice_cube/schedule.rb', line 98 def remove_exception_rule(rule) res = @all_exception_rules.delete(rule) res.nil? ? [] : [res] end |
#remove_exception_time(time) ⇒ Object Also known as: remove_extime
Remove an exception time
144 145 146 147 148 149 150 |
# File 'lib/ice_cube/schedule.rb', line 144 def remove_exception_time(time) found = false @all_exception_rules.delete_if do |rule| found = true if rule.is_a?(SingleOccurrenceRule) && rule.time == time end time if found end |
#remove_recurrence_rule(rule) ⇒ Object
Remove a recurrence rule
86 87 88 89 |
# File 'lib/ice_cube/schedule.rb', line 86 def remove_recurrence_rule(rule) res = @all_recurrence_rules.delete(rule) res.nil? ? [] : [res] end |
#remove_recurrence_time(time) ⇒ Object Also known as: remove_rtime
Remove a recurrence time
124 125 126 127 128 129 130 |
# File 'lib/ice_cube/schedule.rb', line 124 def remove_recurrence_time(time) found = false @all_recurrence_rules.delete_if do |rule| found = true if rule.is_a?(SingleOccurrenceRule) && rule.time == time end time if found end |
#terminating? ⇒ Boolean
Determine if the schedule will end
398 399 400 |
# File 'lib/ice_cube/schedule.rb', line 398 def terminating? recurrence_rules.empty? || recurrence_rules.all?(&:terminating?) end |
#to_hash ⇒ Object
Convert the schedule to a hash
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/ice_cube/schedule.rb', line 370 def to_hash data = {} data[:start_time] = TimeUtil.serialize_time(start_time) data[:start_date] = data[:start_time] if IceCube.compatibility <= 11 data[:end_time] = TimeUtil.serialize_time(end_time) if end_time data[:rrules] = recurrence_rules.map(&:to_hash) if IceCube.compatibility <= 11 && exception_rules.any? data[:exrules] = exception_rules.map(&:to_hash) end data[:rtimes] = recurrence_times.map do |rt| TimeUtil.serialize_time(rt) end data[:extimes] = exception_times.map do |et| TimeUtil.serialize_time(et) end data end |
#to_ical(force_utc = false) ⇒ Object
Serialize this schedule to_ical
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/ice_cube/schedule.rb', line 340 def to_ical(force_utc = false) pieces = [] pieces << "DTSTART#{IcalBuilder.ical_format(start_time, force_utc)}" pieces.concat recurrence_rules.map { |r| "RRULE:#{r.to_ical}" } pieces.concat exception_rules.map { |r| "EXRULE:#{r.to_ical}" } pieces.concat recurrence_times_without_start_time.map { |t| "RDATE#{IcalBuilder.ical_format(t, force_utc)}" } pieces.concat exception_times.map { |t| "EXDATE#{IcalBuilder.ical_format(t, force_utc)}" } pieces << "DTEND#{IcalBuilder.ical_format(end_time, force_utc)}" if end_time pieces.join("\n") end |
#to_s ⇒ Object
String serialization
326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/ice_cube/schedule.rb', line 326 def to_s pieces = [] rd = recurrence_times_with_start_time - extimes pieces.concat rd.sort.map { |t| IceCube::I18n.l(t, format: IceCube.to_s_time_format) } pieces.concat rrules.map { |t| t.to_s } pieces.concat exrules.map { |t| IceCube::I18n.t('ice_cube.not', target: t.to_s) } pieces.concat extimes.sort.map { |t| target = IceCube::I18n.l(t, format: IceCube.to_s_time_format) IceCube::I18n.t('ice_cube.not_on', target: target) } pieces.join(IceCube::I18n.t('ice_cube.pieces_connector')) end |
#to_yaml(*args) ⇒ Object
Convert the schedule to yaml
357 358 359 |
# File 'lib/ice_cube/schedule.rb', line 357 def to_yaml(*args) YAML::dump(to_hash, *args) end |