ice_cube 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/ice_cube/builders/string_builder.rb +15 -23
- data/lib/ice_cube/deprecated.rb +33 -23
- data/lib/ice_cube/enumerator.rb +1 -3
- data/lib/ice_cube/errors/count_exceeded.rb +1 -1
- data/lib/ice_cube/errors/until_exceeded.rb +1 -1
- data/lib/ice_cube/flexible_hash.rb +23 -7
- data/lib/ice_cube/formatters/string.rb +155 -0
- data/lib/ice_cube/hash_input.rb +71 -0
- data/lib/ice_cube/occurrence.rb +2 -1
- data/lib/ice_cube/parsers/hash_parser.rb +87 -0
- data/lib/ice_cube/parsers/yaml_parser.rb +21 -0
- data/lib/ice_cube/rule.rb +5 -7
- data/lib/ice_cube/rules/daily_rule.rb +1 -1
- data/lib/ice_cube/rules/hourly_rule.rb +1 -1
- data/lib/ice_cube/rules/minutely_rule.rb +1 -1
- data/lib/ice_cube/rules/monthly_rule.rb +1 -1
- data/lib/ice_cube/rules/secondly_rule.rb +1 -1
- data/lib/ice_cube/rules/yearly_rule.rb +1 -1
- data/lib/ice_cube/schedule.rb +76 -64
- data/lib/ice_cube/string_helpers.rb +10 -0
- data/lib/ice_cube/time_util.rb +24 -8
- data/lib/ice_cube/validated_rule.rb +13 -6
- data/lib/ice_cube/validations/count.rb +8 -6
- data/lib/ice_cube/validations/daily_interval.rb +17 -18
- data/lib/ice_cube/validations/day.rb +8 -4
- data/lib/ice_cube/validations/day_of_month.rb +12 -10
- data/lib/ice_cube/validations/day_of_week.rb +28 -26
- data/lib/ice_cube/validations/day_of_year.rb +15 -12
- data/lib/ice_cube/validations/hour_of_day.rb +13 -11
- data/lib/ice_cube/validations/hourly_interval.rb +18 -21
- data/lib/ice_cube/validations/lock.rb +35 -14
- data/lib/ice_cube/validations/minute_of_hour.rb +13 -11
- data/lib/ice_cube/validations/minutely_interval.rb +16 -19
- data/lib/ice_cube/validations/month_of_year.rb +8 -4
- data/lib/ice_cube/validations/monthly_interval.rb +19 -17
- data/lib/ice_cube/validations/schedule_lock.rb +12 -11
- data/lib/ice_cube/validations/second_of_minute.rb +9 -5
- data/lib/ice_cube/validations/secondly_interval.rb +13 -16
- data/lib/ice_cube/validations/until.rb +16 -12
- data/lib/ice_cube/validations/weekly_interval.rb +24 -23
- data/lib/ice_cube/validations/yearly_interval.rb +14 -11
- data/lib/ice_cube/version.rb +1 -1
- data/lib/ice_cube.rb +13 -11
- data/spec/spec_helper.rb +2 -0
- metadata +24 -34
data/lib/ice_cube/schedule.rb
CHANGED
|
@@ -4,7 +4,7 @@ module IceCube
|
|
|
4
4
|
|
|
5
5
|
class Schedule
|
|
6
6
|
|
|
7
|
-
extend
|
|
7
|
+
extend Deprecated
|
|
8
8
|
|
|
9
9
|
# Get the start time
|
|
10
10
|
attr_reader :start_time
|
|
@@ -145,60 +145,81 @@ module IceCube
|
|
|
145
145
|
# Get all of the occurrences from the start_time up until a
|
|
146
146
|
# given Time
|
|
147
147
|
def occurrences(closing_time)
|
|
148
|
-
|
|
148
|
+
enumerate_occurrences(start_time, closing_time).to_a
|
|
149
149
|
end
|
|
150
150
|
|
|
151
151
|
# All of the occurrences
|
|
152
152
|
def all_occurrences
|
|
153
153
|
require_terminating_rules
|
|
154
|
-
|
|
154
|
+
enumerate_occurrences(start_time).to_a
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Emit an enumerator based on the start time
|
|
158
|
+
def all_occurrences_enumerator
|
|
159
|
+
enumerate_occurrences(start_time)
|
|
155
160
|
end
|
|
156
161
|
|
|
157
162
|
# Iterate forever
|
|
158
163
|
def each_occurrence(&block)
|
|
159
|
-
|
|
164
|
+
enumerate_occurrences(start_time, &block).to_a
|
|
160
165
|
self
|
|
161
166
|
end
|
|
162
167
|
|
|
163
168
|
# The next n occurrences after now
|
|
164
169
|
def next_occurrences(num, from = nil)
|
|
165
170
|
from ||= TimeUtil.now(@start_time)
|
|
166
|
-
|
|
171
|
+
enumerate_occurrences(from + 1, nil).take(num)
|
|
167
172
|
end
|
|
168
173
|
|
|
169
174
|
# The next occurrence after now (overridable)
|
|
170
175
|
def next_occurrence(from = nil)
|
|
171
176
|
from ||= TimeUtil.now(@start_time)
|
|
172
|
-
|
|
177
|
+
begin
|
|
178
|
+
enumerate_occurrences(from + 1, nil).next()
|
|
179
|
+
rescue StopIteration
|
|
180
|
+
nil
|
|
181
|
+
end
|
|
173
182
|
end
|
|
174
183
|
|
|
175
184
|
# The previous occurrence from a given time
|
|
176
185
|
def previous_occurrence(from)
|
|
177
186
|
return nil if from <= start_time
|
|
178
|
-
|
|
187
|
+
enumerate_occurrences(start_time, from - 1).to_a.last
|
|
179
188
|
end
|
|
180
189
|
|
|
181
190
|
# The previous n occurrences before a given time
|
|
182
191
|
def previous_occurrences(num, from)
|
|
183
192
|
return [] if from <= start_time
|
|
184
|
-
|
|
193
|
+
a = enumerate_occurrences(start_time, from - 1).to_a
|
|
194
|
+
a.size > num ? a[-1*num,a.size] : a
|
|
185
195
|
end
|
|
186
196
|
|
|
187
197
|
# The remaining occurrences (same requirements as all_occurrences)
|
|
188
198
|
def remaining_occurrences(from = nil)
|
|
189
199
|
require_terminating_rules
|
|
190
200
|
from ||= TimeUtil.now(@start_time)
|
|
191
|
-
|
|
201
|
+
enumerate_occurrences(from).to_a
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Returns an enumerator for all remaining occurrences
|
|
205
|
+
def remaining_occurrences_enumerator(from = nil)
|
|
206
|
+
from ||= TimeUtil.now(@start_time)
|
|
207
|
+
enumerate_occurrences(from)
|
|
192
208
|
end
|
|
193
209
|
|
|
194
210
|
# Occurrences between two times
|
|
195
211
|
def occurrences_between(begin_time, closing_time)
|
|
196
|
-
|
|
212
|
+
enumerate_occurrences(begin_time, closing_time).to_a
|
|
197
213
|
end
|
|
198
214
|
|
|
199
215
|
# Return a boolean indicating if an occurrence falls between two times
|
|
200
216
|
def occurs_between?(begin_time, closing_time)
|
|
201
|
-
|
|
217
|
+
begin
|
|
218
|
+
enumerate_occurrences(begin_time, closing_time).next()
|
|
219
|
+
true
|
|
220
|
+
rescue StopIteration
|
|
221
|
+
false
|
|
222
|
+
end
|
|
202
223
|
end
|
|
203
224
|
|
|
204
225
|
# Return a boolean indicating if an occurrence is occurring between two
|
|
@@ -237,7 +258,7 @@ module IceCube
|
|
|
237
258
|
def conflicts_with?(other_schedule, closing_time = nil)
|
|
238
259
|
closing_time = TimeUtil.ensure_time closing_time
|
|
239
260
|
unless terminating? || other_schedule.terminating? || closing_time
|
|
240
|
-
raise ArgumentError
|
|
261
|
+
raise ArgumentError, "One or both schedules must be terminating to use #conflicts_with?"
|
|
241
262
|
end
|
|
242
263
|
# Pick the terminating schedule, and other schedule
|
|
243
264
|
# No need to reverse if terminating? or there is a closing time
|
|
@@ -247,6 +268,7 @@ module IceCube
|
|
|
247
268
|
end
|
|
248
269
|
# Go through each occurrence of the terminating schedule and determine
|
|
249
270
|
# if the other occurs at that time
|
|
271
|
+
#
|
|
250
272
|
last_time = nil
|
|
251
273
|
terminating_schedule.each_occurrence do |time|
|
|
252
274
|
if closing_time && time > closing_time
|
|
@@ -276,7 +298,7 @@ module IceCube
|
|
|
276
298
|
|
|
277
299
|
# Get the first n occurrences, or the first occurrence if n is skipped
|
|
278
300
|
def first(n = nil)
|
|
279
|
-
occurrences =
|
|
301
|
+
occurrences = enumerate_occurrences(start_time).take(n || 1)
|
|
280
302
|
n.nil? ? occurrences.first : occurrences
|
|
281
303
|
end
|
|
282
304
|
|
|
@@ -284,7 +306,7 @@ module IceCube
|
|
|
284
306
|
# or the final one if no n is given
|
|
285
307
|
def last(n = nil)
|
|
286
308
|
require_terminating_rules
|
|
287
|
-
occurrences =
|
|
309
|
+
occurrences = enumerate_occurrences(start_time).to_a
|
|
288
310
|
n.nil? ? occurrences.last : occurrences[-n..-1]
|
|
289
311
|
end
|
|
290
312
|
|
|
@@ -313,25 +335,27 @@ module IceCube
|
|
|
313
335
|
|
|
314
336
|
# Convert the schedule to yaml
|
|
315
337
|
def to_yaml(*args)
|
|
316
|
-
|
|
338
|
+
YAML::dump(to_hash, *args)
|
|
317
339
|
end
|
|
318
340
|
|
|
319
341
|
# Load the schedule from yaml
|
|
320
342
|
def self.from_yaml(yaml, options = {})
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
343
|
+
YamlParser.new(yaml).to_schedule do |schedule|
|
|
344
|
+
Deprecated.schedule_options(schedule, options)
|
|
345
|
+
yield schedule if block_given?
|
|
324
346
|
end
|
|
325
|
-
from_hash hash, options
|
|
326
347
|
end
|
|
327
348
|
|
|
328
349
|
# Convert the schedule to a hash
|
|
329
350
|
def to_hash
|
|
330
351
|
data = {}
|
|
331
|
-
data[:
|
|
352
|
+
data[:start_time] = TimeUtil.serialize_time(start_time)
|
|
353
|
+
data[:start_date] = data[:start_time] if IceCube.compatibility <= 11
|
|
332
354
|
data[:end_time] = TimeUtil.serialize_time(end_time) if end_time
|
|
333
355
|
data[:rrules] = recurrence_rules.map(&:to_hash)
|
|
334
|
-
|
|
356
|
+
if IceCube.compatibility <= 11 && exception_rules.any?
|
|
357
|
+
data[:exrules] = exception_rules.map(&:to_hash)
|
|
358
|
+
end
|
|
335
359
|
data[:rtimes] = recurrence_times.map do |rt|
|
|
336
360
|
TimeUtil.serialize_time(rt)
|
|
337
361
|
end
|
|
@@ -343,28 +367,10 @@ module IceCube
|
|
|
343
367
|
|
|
344
368
|
# Load the schedule from a hash
|
|
345
369
|
def self.from_hash(original_hash, options = {})
|
|
346
|
-
original_hash
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
schedule = IceCube::Schedule.new TimeUtil.deserialize_time(data[:start_date])
|
|
350
|
-
schedule.end_time = schedule.start_time + data[:duration] if data[:duration]
|
|
351
|
-
schedule.end_time = TimeUtil.deserialize_time(data[:end_time]) if data[:end_time]
|
|
352
|
-
data[:rrules] && data[:rrules].each { |h| schedule.rrule(IceCube::Rule.from_hash(h)) }
|
|
353
|
-
data[:exrules] && data[:exrules].each { |h| schedule.exrule(IceCube::Rule.from_hash(h)) }
|
|
354
|
-
data[:rtimes] && data[:rtimes].each do |t|
|
|
355
|
-
schedule.add_recurrence_time TimeUtil.deserialize_time(t)
|
|
356
|
-
end
|
|
357
|
-
data[:extimes] && data[:extimes].each do |t|
|
|
358
|
-
schedule.add_exception_time TimeUtil.deserialize_time(t)
|
|
370
|
+
HashParser.new(original_hash).to_schedule do |schedule|
|
|
371
|
+
Deprecated.schedule_options(schedule, options)
|
|
372
|
+
yield schedule if block_given?
|
|
359
373
|
end
|
|
360
|
-
# Also serialize old format for backward compat
|
|
361
|
-
data[:rdates] && data[:rdates].each do |t|
|
|
362
|
-
schedule.add_recurrence_time TimeUtil.deserialize_time(t)
|
|
363
|
-
end
|
|
364
|
-
data[:exdates] && data[:exdates].each do |t|
|
|
365
|
-
schedule.add_exception_time TimeUtil.deserialize_time(t)
|
|
366
|
-
end
|
|
367
|
-
schedule
|
|
368
374
|
end
|
|
369
375
|
|
|
370
376
|
# Determine if the schedule will end
|
|
@@ -391,30 +397,29 @@ module IceCube
|
|
|
391
397
|
|
|
392
398
|
# Find all of the occurrences for the schedule between opening_time
|
|
393
399
|
# and closing_time
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
400
|
+
# Iteration is unrolled in pairs to skip duplicate times in end of DST
|
|
401
|
+
def enumerate_occurrences(opening_time, closing_time = nil, &block)
|
|
402
|
+
opening_time = TimeUtil.match_zone(opening_time, start_time)
|
|
403
|
+
closing_time = TimeUtil.match_zone(closing_time, start_time)
|
|
397
404
|
opening_time += start_time.subsec - opening_time.subsec rescue 0
|
|
398
405
|
reset
|
|
399
|
-
answers = []
|
|
400
406
|
opening_time = start_time if opening_time < start_time
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
407
|
+
t1 = full_required? ? start_time : opening_time
|
|
408
|
+
e = Enumerator.new do |yielder|
|
|
409
|
+
loop do
|
|
410
|
+
break unless (t0 = next_time(t1, closing_time))
|
|
411
|
+
break if closing_time && t0 > closing_time
|
|
412
|
+
yielder << (block_given? ? block.call(t0) : t0) if t0 >= opening_time
|
|
413
|
+
break unless (t1 = next_time(t0 + 1, closing_time))
|
|
414
|
+
break if closing_time && t1 > closing_time
|
|
415
|
+
if TimeUtil.same_clock?(t0, t1) && recurrence_rules.any?(&:dst_adjust?)
|
|
416
|
+
wind_back_dst
|
|
417
|
+
next (t1 += 1)
|
|
418
|
+
end
|
|
419
|
+
yielder << (block_given? ? block.call(t1) : t1) if t1 >= opening_time
|
|
420
|
+
next (t1 += 1)
|
|
413
421
|
end
|
|
414
|
-
time = res + 1
|
|
415
422
|
end
|
|
416
|
-
# and return our answers
|
|
417
|
-
answers
|
|
418
423
|
end
|
|
419
424
|
|
|
420
425
|
# Get the next time after (or including) a specific time
|
|
@@ -424,17 +429,18 @@ module IceCube
|
|
|
424
429
|
begin
|
|
425
430
|
new_time = rule.next_time(time, self, min_time || closing_time)
|
|
426
431
|
[min_time, new_time].compact.min
|
|
427
|
-
rescue
|
|
432
|
+
rescue StopIteration
|
|
428
433
|
min_time
|
|
429
434
|
end
|
|
430
435
|
end
|
|
431
436
|
break nil unless min_time
|
|
432
|
-
next(time = min_time + 1) if exception_time?(min_time)
|
|
437
|
+
next (time = min_time + 1) if exception_time?(min_time)
|
|
433
438
|
break Occurrence.new(min_time, min_time + duration)
|
|
434
439
|
end
|
|
435
440
|
end
|
|
436
441
|
|
|
437
|
-
#
|
|
442
|
+
# Indicate if any rule needs to be run from the start of time
|
|
443
|
+
# If we have rules with counts, we need to walk from the beginning of time
|
|
438
444
|
def full_required?
|
|
439
445
|
@all_recurrence_rules.any?(&:full_required?) ||
|
|
440
446
|
@all_exception_rules.any?(&:full_required?)
|
|
@@ -478,6 +484,12 @@ module IceCube
|
|
|
478
484
|
end
|
|
479
485
|
end
|
|
480
486
|
|
|
487
|
+
def wind_back_dst
|
|
488
|
+
recurrence_rules.each do |rule|
|
|
489
|
+
rule.skipped_for_dst
|
|
490
|
+
end
|
|
491
|
+
end
|
|
492
|
+
|
|
481
493
|
end
|
|
482
494
|
|
|
483
495
|
end
|
data/lib/ice_cube/time_util.rb
CHANGED
|
@@ -3,7 +3,7 @@ require 'date'
|
|
|
3
3
|
module IceCube
|
|
4
4
|
module TimeUtil
|
|
5
5
|
|
|
6
|
-
extend
|
|
6
|
+
extend Deprecated
|
|
7
7
|
|
|
8
8
|
DAYS = {
|
|
9
9
|
:sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3,
|
|
@@ -16,6 +16,8 @@ module IceCube
|
|
|
16
16
|
:november => 11, :december => 12
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
CLOCK_VALUES = [:year, :month, :day, :hour, :min, :sec]
|
|
20
|
+
|
|
19
21
|
# Provides a Time.now without the usec, in the reference zone or utc offset
|
|
20
22
|
def self.now(reference=Time.now)
|
|
21
23
|
match_zone(Time.at(Time.now.to_i), reference)
|
|
@@ -72,7 +74,8 @@ module IceCube
|
|
|
72
74
|
if time_or_hash.is_a?(Time)
|
|
73
75
|
time_or_hash
|
|
74
76
|
elsif time_or_hash.is_a?(Hash)
|
|
75
|
-
|
|
77
|
+
hash = FlexibleHash.new(time_or_hash)
|
|
78
|
+
hash[:time].in_time_zone(hash[:zone])
|
|
76
79
|
end
|
|
77
80
|
end
|
|
78
81
|
|
|
@@ -82,7 +85,7 @@ module IceCube
|
|
|
82
85
|
# keep it. If it was serialized with a different offset than local TZ it
|
|
83
86
|
# will lose the zone and not support DST.
|
|
84
87
|
def self.restore_deserialized_offset(time, orig_offset_str)
|
|
85
|
-
return time if time.respond_to?(:time_zone)
|
|
88
|
+
return time if time.respond_to?(:time_zone) ||
|
|
86
89
|
time.getlocal(orig_offset_str).utc_offset == time.utc_offset
|
|
87
90
|
warn 'IceCube: parsed Time from nonlocal TZ. Use ActiveSupport to fix DST'
|
|
88
91
|
time.localtime(orig_offset_str)
|
|
@@ -112,22 +115,31 @@ module IceCube
|
|
|
112
115
|
|
|
113
116
|
# Convert a symbol to a numeric month
|
|
114
117
|
def self.sym_to_month(sym)
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
MONTHS.fetch(sym) do |k|
|
|
119
|
+
return wday = sym.to_i if MONTHS.values.any? { |i| i.to_s == sym.to_s }
|
|
120
|
+
raise ArgumentError, "Expecting Fixnum or Symbol value for month. " \
|
|
121
|
+
"No such month: #{k.inspect}"
|
|
122
|
+
end
|
|
117
123
|
end
|
|
118
124
|
deprecated_alias :symbol_to_month, :sym_to_month
|
|
119
125
|
|
|
120
126
|
# Convert a symbol to a wday number
|
|
121
127
|
def self.sym_to_wday(sym)
|
|
122
|
-
|
|
123
|
-
|
|
128
|
+
DAYS.fetch(sym) do |k|
|
|
129
|
+
return sym.to_i if DAYS.values.any? { |i| i.to_s == sym.to_s }
|
|
130
|
+
raise ArgumentError, "Expecting Fixnum or Symbol value for weekday. " \
|
|
131
|
+
"No such weekday: #{k.inspect}"
|
|
132
|
+
end
|
|
124
133
|
end
|
|
125
134
|
deprecated_alias :symbol_to_day, :sym_to_wday
|
|
126
135
|
|
|
127
136
|
# Convert wday number to day symbol
|
|
128
137
|
def self.wday_to_sym(wday)
|
|
129
138
|
return sym = wday if DAYS.keys.include? wday
|
|
130
|
-
DAYS.invert.fetch(wday)
|
|
139
|
+
DAYS.invert.fetch(wday) do |i|
|
|
140
|
+
raise ArgumentError, "Expecting Fixnum value for weekday. " \
|
|
141
|
+
"No such wday number: #{i.inspect}"
|
|
142
|
+
end
|
|
131
143
|
end
|
|
132
144
|
|
|
133
145
|
# Convert weekday from base sunday to the schedule's week start.
|
|
@@ -200,6 +212,10 @@ module IceCube
|
|
|
200
212
|
end
|
|
201
213
|
end
|
|
202
214
|
|
|
215
|
+
def self.same_clock?(t1, t2)
|
|
216
|
+
CLOCK_VALUES.all? { |i| t1.send(i) == t2.send(i) }
|
|
217
|
+
end
|
|
218
|
+
|
|
203
219
|
# A utility class for safely moving time around
|
|
204
220
|
class TimeWrapper
|
|
205
221
|
|
|
@@ -46,6 +46,14 @@ module IceCube
|
|
|
46
46
|
@time
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
+
def skipped_for_dst
|
|
50
|
+
@uses -= 1 if @uses > 0
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def dst_adjust?
|
|
54
|
+
@validations[:interval].any? &:dst_adjust?
|
|
55
|
+
end
|
|
56
|
+
|
|
49
57
|
def to_s
|
|
50
58
|
builder = StringBuilder.new
|
|
51
59
|
@validations.each do |name, validations|
|
|
@@ -132,12 +140,11 @@ module IceCube
|
|
|
132
140
|
end
|
|
133
141
|
|
|
134
142
|
def shift_time_by_validation(res, vals)
|
|
135
|
-
return unless res.min
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
wrapper
|
|
139
|
-
wrapper.
|
|
140
|
-
wrapper.clear_below(type)
|
|
143
|
+
return unless (interval = res.min)
|
|
144
|
+
validation = vals.first
|
|
145
|
+
wrapper = TimeUtil::TimeWrapper.new(@time, validation.dst_adjust?)
|
|
146
|
+
wrapper.add(validation.type, interval)
|
|
147
|
+
wrapper.clear_below(validation.type)
|
|
141
148
|
|
|
142
149
|
# Move over DST if blocked, no adjustments
|
|
143
150
|
if wrapper.to_time <= @time
|
|
@@ -2,7 +2,7 @@ module IceCube
|
|
|
2
2
|
|
|
3
3
|
module Validations::Count
|
|
4
4
|
|
|
5
|
-
#
|
|
5
|
+
# Value reader for limit
|
|
6
6
|
def occurrence_count
|
|
7
7
|
@count
|
|
8
8
|
end
|
|
@@ -12,7 +12,7 @@ module IceCube
|
|
|
12
12
|
raise ArgumentError, "Expecting Fixnum or nil value for count, got #{max.inspect}"
|
|
13
13
|
end
|
|
14
14
|
@count = max
|
|
15
|
-
replace_validations_for(:count, [Validation.new(max, self)])
|
|
15
|
+
replace_validations_for(:count, max && [Validation.new(max, self)])
|
|
16
16
|
self
|
|
17
17
|
end
|
|
18
18
|
|
|
@@ -26,13 +26,15 @@ module IceCube
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def type
|
|
29
|
-
:
|
|
29
|
+
:limit
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def dst_adjust?
|
|
33
|
+
false
|
|
30
34
|
end
|
|
31
35
|
|
|
32
36
|
def validate(time, schedule)
|
|
33
|
-
if rule.uses && rule.uses >= count
|
|
34
|
-
raise CountExceeded
|
|
35
|
-
end
|
|
37
|
+
raise CountExceeded if rule.uses && rule.uses >= count
|
|
36
38
|
end
|
|
37
39
|
|
|
38
40
|
def build_s(builder)
|
|
@@ -10,8 +10,6 @@ module IceCube
|
|
|
10
10
|
self
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
# A validation for checking to make sure that a time
|
|
14
|
-
# is inside of a certain DailyInterval
|
|
15
13
|
class Validation
|
|
16
14
|
|
|
17
15
|
attr_reader :interval
|
|
@@ -20,6 +18,22 @@ module IceCube
|
|
|
20
18
|
@interval = interval
|
|
21
19
|
end
|
|
22
20
|
|
|
21
|
+
def type
|
|
22
|
+
:day
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def dst_adjust?
|
|
26
|
+
true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def validate(step_time, schedule)
|
|
30
|
+
t0, t1 = schedule.start_time, step_time
|
|
31
|
+
days = Date.new(t1.year, t1.month, t1.day) -
|
|
32
|
+
Date.new(t0.year, t0.month, t0.day)
|
|
33
|
+
offset = (days % interval).nonzero?
|
|
34
|
+
interval - offset if offset
|
|
35
|
+
end
|
|
36
|
+
|
|
23
37
|
def build_s(builder)
|
|
24
38
|
builder.base = interval == 1 ? 'Daily' : "Every #{interval} days"
|
|
25
39
|
end
|
|
@@ -30,22 +44,7 @@ module IceCube
|
|
|
30
44
|
|
|
31
45
|
def build_ical(builder)
|
|
32
46
|
builder['FREQ'] << 'DAILY'
|
|
33
|
-
unless interval == 1
|
|
34
|
-
builder['INTERVAL'] << interval
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def type
|
|
39
|
-
:day
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def validate(time, schedule)
|
|
43
|
-
time_date = Date.new(time.year, time.month, time.day)
|
|
44
|
-
start_date = Date.new(schedule.start_time.year, schedule.start_time.month, schedule.start_time.day)
|
|
45
|
-
days = time_date - start_date
|
|
46
|
-
unless days % interval === 0
|
|
47
|
-
interval - (days % interval)
|
|
48
|
-
end
|
|
47
|
+
builder['INTERVAL'] << interval unless interval == 1
|
|
49
48
|
end
|
|
50
49
|
|
|
51
50
|
end
|
|
@@ -27,6 +27,14 @@ module IceCube
|
|
|
27
27
|
@day = day
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
def type
|
|
31
|
+
:wday
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def dst_adjust?
|
|
35
|
+
true
|
|
36
|
+
end
|
|
37
|
+
|
|
30
38
|
def build_s(builder)
|
|
31
39
|
builder.piece(:day) << day
|
|
32
40
|
end
|
|
@@ -43,10 +51,6 @@ module IceCube
|
|
|
43
51
|
end
|
|
44
52
|
end
|
|
45
53
|
|
|
46
|
-
def type
|
|
47
|
-
:wday
|
|
48
|
-
end
|
|
49
|
-
|
|
50
54
|
StringBuilder.register_formatter(:day) do |validation_days|
|
|
51
55
|
# sort the days
|
|
52
56
|
validation_days.sort!
|
|
@@ -2,8 +2,6 @@ module IceCube
|
|
|
2
2
|
|
|
3
3
|
module Validations::DayOfMonth
|
|
4
4
|
|
|
5
|
-
include Validations::Lock
|
|
6
|
-
|
|
7
5
|
def day_of_month(*days)
|
|
8
6
|
days.flatten.each do |day|
|
|
9
7
|
unless day.is_a?(Fixnum)
|
|
@@ -19,12 +17,6 @@ module IceCube
|
|
|
19
17
|
|
|
20
18
|
include Validations::Lock
|
|
21
19
|
|
|
22
|
-
StringBuilder.register_formatter(:day_of_month) do |entries|
|
|
23
|
-
str = "on the #{StringBuilder.sentence(entries)} "
|
|
24
|
-
str << (entries.size == 1 ? 'day of the month' : 'days of the month')
|
|
25
|
-
str
|
|
26
|
-
end
|
|
27
|
-
|
|
28
20
|
attr_reader :day
|
|
29
21
|
alias :value :day
|
|
30
22
|
|
|
@@ -32,6 +24,14 @@ module IceCube
|
|
|
32
24
|
@day = day
|
|
33
25
|
end
|
|
34
26
|
|
|
27
|
+
def type
|
|
28
|
+
:day
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def dst_adjust?
|
|
32
|
+
true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
35
|
def build_s(builder)
|
|
36
36
|
builder.piece(:day_of_month) << StringBuilder.nice_number(day)
|
|
37
37
|
end
|
|
@@ -44,8 +44,10 @@ module IceCube
|
|
|
44
44
|
builder['BYMONTHDAY'] << day
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
StringBuilder.register_formatter(:day_of_month) do |entries|
|
|
48
|
+
str = "on the #{StringBuilder.sentence(entries)} "
|
|
49
|
+
str << (entries.size == 1 ? 'day of the month' : 'days of the month')
|
|
50
|
+
str
|
|
49
51
|
end
|
|
50
52
|
|
|
51
53
|
end
|
|
@@ -17,23 +17,35 @@ module IceCube
|
|
|
17
17
|
|
|
18
18
|
attr_reader :day, :occ
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
def initialize(day, occ)
|
|
21
|
+
@day = day
|
|
22
|
+
@occ = occ
|
|
22
23
|
end
|
|
23
24
|
|
|
24
25
|
def type
|
|
25
26
|
:day
|
|
26
27
|
end
|
|
27
28
|
|
|
28
|
-
def
|
|
29
|
-
|
|
29
|
+
def dst_adjust?
|
|
30
|
+
true
|
|
30
31
|
end
|
|
31
32
|
|
|
32
|
-
def
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
def validate(step_time, schedule)
|
|
34
|
+
wday = step_time.wday
|
|
35
|
+
offset = (day < wday) ? (7 - wday + day) : (day - wday)
|
|
36
|
+
wrapper = TimeUtil::TimeWrapper.new(step_time)
|
|
37
|
+
wrapper.add :day, offset
|
|
38
|
+
loop do
|
|
39
|
+
which_occ, num_occ = TimeUtil.which_occurrence_in_month(wrapper.to_time, day)
|
|
40
|
+
this_occ = (occ < 0) ? (num_occ + occ + 1) : (occ)
|
|
41
|
+
break offset if which_occ == this_occ
|
|
42
|
+
wrapper.add :day, 7
|
|
43
|
+
offset += 7
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def build_s(builder)
|
|
48
|
+
builder.piece(:day_of_week) << "#{StringBuilder.nice_number(occ)} #{Date::DAYNAMES[day]}"
|
|
37
49
|
end
|
|
38
50
|
|
|
39
51
|
def build_hash(builder)
|
|
@@ -42,25 +54,15 @@ module IceCube
|
|
|
42
54
|
arr << occ
|
|
43
55
|
end
|
|
44
56
|
|
|
45
|
-
def
|
|
46
|
-
|
|
47
|
-
|
|
57
|
+
def build_ical(builder)
|
|
58
|
+
ical_day = IcalBuilder.fixnum_to_ical_day(day)
|
|
59
|
+
# Delete any with this day and no occ first
|
|
60
|
+
builder['BYDAY'].delete_if { |d| d == ical_day }
|
|
61
|
+
builder['BYDAY'] << "#{occ}#{ical_day}"
|
|
48
62
|
end
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
sum = day >= time.wday ? day - time.wday : 7 - time.wday + day
|
|
53
|
-
wrapper = TimeUtil::TimeWrapper.new(time)
|
|
54
|
-
wrapper.add :day, sum
|
|
55
|
-
# and then count the week until a viable occ
|
|
56
|
-
loop do
|
|
57
|
-
which_occ, num_occ = TimeUtil.which_occurrence_in_month(wrapper.to_time, day)
|
|
58
|
-
this_occ = occ < 0 ? num_occ + occ + 1 : occ
|
|
59
|
-
break if which_occ == this_occ
|
|
60
|
-
sum += 7
|
|
61
|
-
wrapper.add :day, 7 # one week
|
|
62
|
-
end
|
|
63
|
-
sum
|
|
64
|
+
StringBuilder.register_formatter(:day_of_week) do |segments|
|
|
65
|
+
'on the ' + segments.join(' and ')
|
|
64
66
|
end
|
|
65
67
|
|
|
66
68
|
end
|