ice_cube 0.9.1 → 0.10.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0cc5c669bd801317d8256d7fc8b77841e80dae6e
4
+ data.tar.gz: 6bda80589b1d0a91d54ea2d7b4194d320e26b2ac
5
+ SHA512:
6
+ metadata.gz: d3a10207a9dc7368b3cc6f045160c2b3f6651e1a95570a8d6c58af689eb616deb47291485b514b4ec58b1275595dce7f7c21f1606341d624449edfc5c842648d
7
+ data.tar.gz: 0abec738ddd565630a8a92468a8db708c2c18e2601abe3824ba10b766ce952341d7ff90da50a7ac6f4349d079661d66e8b131f177cbf4ee2853b4b046d4389a6
@@ -1,7 +1,7 @@
1
1
  module IceCube
2
2
 
3
3
  class HashBuilder
4
-
4
+
5
5
  def initialize(rule = nil)
6
6
  @hash = { :validations => {}, :rule_type => rule.class.name }
7
7
  end
@@ -41,8 +41,8 @@ module IceCube
41
41
  class << self
42
42
 
43
43
  NUMBER_SUFFIX = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
44
- SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }
45
-
44
+ SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }
45
+
46
46
  # influenced by ActiveSupport's to_sentence
47
47
  def sentence(array)
48
48
  case array.length
@@ -63,12 +63,12 @@ module IceCube
63
63
  else
64
64
  suffix = SPECIAL_SUFFIX.include?(number) ?
65
65
  SPECIAL_SUFFIX[number] : NUMBER_SUFFIX[number.abs % 10]
66
- number.to_s << suffix
66
+ number.to_s << suffix
67
67
  end
68
68
  end
69
69
 
70
70
  end
71
-
71
+
72
72
  end
73
73
 
74
74
  end
@@ -0,0 +1,90 @@
1
+ require 'forwardable'
2
+ require 'delegate'
3
+
4
+ module IceCube
5
+
6
+ # Wraps start_time and end_time in a single concept concerning the duration.
7
+ # This delegates to the enclosed start_time so it behaves like a normal Time
8
+ # in almost all situations, however:
9
+ #
10
+ # Without ActiveSupport, it's necessary to cast the occurrence using
11
+ # +#to_time+ before doing arithmetic, else Time will try to subtract it
12
+ # using +#to_i+ and return a new time instead.
13
+ #
14
+ # Time.now - Occurrence.new(start_time) # => 1970-01-01 01:00:00
15
+ # Time.now - Occurrence.new(start_time).to_time # => 3600
16
+ #
17
+ # When ActiveSupport::Time core extensions are loaded, it's possible to
18
+ # subtract an Occurrence object directly from a Time to get the difference:
19
+ #
20
+ # Time.now - Occurrence.new(start_time) # => 3600
21
+ #
22
+ class Occurrence < SimpleDelegator
23
+
24
+ # Optimize for common methods to avoid method_missing
25
+ extend Forwardable
26
+ def_delegators :start_time, :to_s, :to_i, :<=>, :==
27
+ def_delegators :to_range, :cover?, :include?, :each, :first, :last
28
+
29
+ attr_reader :start_time, :end_time
30
+
31
+ def initialize(start_time, end_time=nil)
32
+ @start_time = start_time
33
+ @end_time = end_time || start_time
34
+ __setobj__ @start_time
35
+ end
36
+
37
+ def is_a?(klass)
38
+ klass == ::Time || super
39
+ end
40
+ alias_method :kind_of?, :is_a?
41
+
42
+ def intersects? other
43
+ if other.is_a?(Occurrence) || other.is_a?(Range)
44
+ lower_bound_1 = first + 1
45
+ upper_bound_1 = last # exclude end
46
+ lower_bound_2 = other.first + 1
47
+ upper_bound_2 = other.last + 1
48
+ if (lower_bound_2 <=> upper_bound_2) > 0
49
+ false
50
+ elsif (lower_bound_1 <=> upper_bound_1) > 0
51
+ false
52
+ else
53
+ (upper_bound_1 <=> lower_bound_2) >= 0 and
54
+ (upper_bound_2 <=> lower_bound_1) >= 0
55
+ end
56
+ else
57
+ cover? other
58
+ end
59
+ end
60
+
61
+ def comparable_time
62
+ start_time
63
+ end
64
+
65
+ def duration
66
+ end_time - start_time
67
+ end
68
+
69
+ def to_range
70
+ start_time..end_time
71
+ end
72
+
73
+ def to_time
74
+ start_time
75
+ end
76
+
77
+ def to_s
78
+ if duration > 0
79
+ "#{start_time} - #{end_time}"
80
+ else
81
+ "#{start_time}"
82
+ end
83
+ end
84
+
85
+ def overnight?
86
+ midnight = Time.new(start_time.year, start_time.month, start_time.day + 1)
87
+ midnight < end_time
88
+ end
89
+ end
90
+ end
data/lib/ice_cube/rule.rb CHANGED
@@ -48,6 +48,7 @@ module IceCube
48
48
  hash = IceCube::FlexibleHash.new original_hash
49
49
  return nil unless match = hash[:rule_type].match(/\:\:(.+?)Rule/)
50
50
  rule = IceCube::Rule.send(match[1].downcase.to_sym, hash[:interval] || 1)
51
+ rule.interval(hash[:interval] || 1, TimeUtil.wday_to_sym(hash[:week_start] || 0)) if match[1] == "Weekly"
51
52
  rule.until(TimeUtil.deserialize_time(hash[:until])) if hash[:until]
52
53
  rule.count(hash[:count]) if hash[:count]
53
54
  hash[:validations] && hash[:validations].each do |key, value|
@@ -66,7 +67,7 @@ module IceCube
66
67
  end
67
68
 
68
69
  def on?(time, schedule)
69
- next_time(time, schedule, time) == time
70
+ next_time(time, schedule, time).to_i == time.to_i
70
71
  end
71
72
 
72
73
  # Whether this rule requires a full run
@@ -113,7 +114,7 @@ module IceCube
113
114
  end
114
115
 
115
116
  end
116
-
117
+
117
118
  end
118
119
 
119
120
  end
@@ -7,6 +7,7 @@ module IceCube
7
7
  def initialize(interval = 1)
8
8
  interval(interval)
9
9
  schedule_lock(:hour, :min, :sec)
10
+ reset
10
11
  end
11
12
 
12
13
  end
@@ -7,6 +7,7 @@ module IceCube
7
7
  def initialize(interval = 1)
8
8
  interval(interval)
9
9
  schedule_lock(:min, :sec)
10
+ reset
10
11
  end
11
12
 
12
13
  end
@@ -7,6 +7,7 @@ module IceCube
7
7
  def initialize(interval = 1)
8
8
  interval(interval)
9
9
  schedule_lock(:sec)
10
+ reset
10
11
  end
11
12
 
12
13
  end
@@ -7,6 +7,7 @@ module IceCube
7
7
  def initialize(interval = 1)
8
8
  interval(interval)
9
9
  schedule_lock(:day, :hour, :min, :sec)
10
+ reset
10
11
  end
11
12
 
12
13
  end
@@ -1,11 +1,12 @@
1
1
  module IceCube
2
2
 
3
3
  class SecondlyRule < ValidatedRule
4
-
4
+
5
5
  include Validations::SecondlyInterval
6
6
 
7
7
  def initialize(interval = 1)
8
8
  interval(interval)
9
+ reset
9
10
  end
10
11
 
11
12
  end
@@ -7,6 +7,7 @@ module IceCube
7
7
  def initialize(interval = 1, week_start = :sunday)
8
8
  interval(interval, week_start)
9
9
  schedule_lock(:wday, :hour, :min, :sec)
10
+ reset
10
11
  end
11
12
 
12
13
  end
@@ -7,6 +7,7 @@ module IceCube
7
7
  def initialize(interval = 1)
8
8
  interval(interval)
9
9
  schedule_lock(:month, :day, :hour, :min, :sec)
10
+ reset
10
11
  end
11
12
 
12
13
  end
@@ -7,25 +7,41 @@ module IceCube
7
7
  extend ::Deprecated
8
8
 
9
9
  # Get the start time
10
- attr_accessor :start_time
10
+ attr_reader :start_time
11
11
  deprecated_alias :start_date, :start_time
12
- deprecated_alias :start_date=, :start_time=
13
-
14
- # Get the duration
15
- attr_accessor :duration
16
12
 
17
13
  # Get the end time
18
- attr_accessor :end_time
14
+ attr_reader :end_time
19
15
  deprecated_alias :end_date, :end_time
20
- deprecated_alias :end_date=, :end_time=
21
16
 
22
17
  # Create a new schedule
23
18
  def initialize(start_time = nil, options = {})
24
- @start_time = start_time || TimeUtil.now
25
- @end_time = options[:end_time]
26
- @duration = options[:duration]
19
+ self.start_time = start_time || TimeUtil.now
20
+ self.end_time = self.start_time + options[:duration] if options[:duration]
21
+ self.end_time = options[:end_time] if options[:end_time]
27
22
  @all_recurrence_rules = []
28
23
  @all_exception_rules = []
24
+ yield self if block_given?
25
+ end
26
+
27
+ # Set start_time
28
+ def start_time=(start_time)
29
+ @start_time = TimeUtil.ensure_time start_time
30
+ end
31
+ deprecated_alias :start_date=, :start_time=
32
+
33
+ # Set end_time
34
+ def end_time=(end_time)
35
+ @end_time = TimeUtil.ensure_time end_time
36
+ end
37
+ deprecated_alias :end_date=, :end_time=
38
+
39
+ def duration
40
+ end_time ? end_time - start_time : 0
41
+ end
42
+
43
+ def duration=(seconds)
44
+ @end_time = start_time + seconds
29
45
  end
30
46
 
31
47
  # Add a recurrence time to the schedule
@@ -145,17 +161,20 @@ module IceCube
145
161
  end
146
162
 
147
163
  # The next n occurrences after now
148
- def next_occurrences(num, from = TimeUtil.now)
164
+ def next_occurrences(num, from = nil)
165
+ from ||= TimeUtil.now(@start_time)
149
166
  find_occurrences(from + 1, nil, num)
150
167
  end
151
168
 
152
169
  # The next occurrence after now (overridable)
153
- def next_occurrence(from = TimeUtil.now)
170
+ def next_occurrence(from = nil)
171
+ from ||= TimeUtil.now(@start_time)
154
172
  find_occurrences(from + 1, nil, 1).first
155
173
  end
156
174
 
157
175
  # The remaining occurrences (same requirements as all_occurrences)
158
- def remaining_occurrences(from = TimeUtil.now)
176
+ def remaining_occurrences(from = nil)
177
+ from ||= TimeUtil.now(@start_time)
159
178
  find_occurrences(from)
160
179
  end
161
180
 
@@ -164,29 +183,33 @@ module IceCube
164
183
  find_occurrences(begin_time, closing_time)
165
184
  end
166
185
 
167
- # Return a boolean indicating if an occurrence falls between
168
- # two times
186
+ # Return a boolean indicating if an occurrence falls between two times
169
187
  def occurs_between?(begin_time, closing_time)
170
188
  !find_occurrences(begin_time, closing_time, 1).empty?
171
189
  end
172
190
 
173
- # Return a boolean indicating if an occurrence is occurring between
174
- # two times, inclusive
175
- def occurring_between?(begin_time, closing_time)
176
- dur = duration || 0
177
- occurs_between?(begin_time - dur + 1, closing_time + dur - 1)
191
+ # Return a boolean indicating if an occurrence is occurring between two
192
+ # times, inclusive of its duration. This counts zero-length occurrences
193
+ # that intersect the start of the range and within the range, but not
194
+ # occurrences at the end of the range since none of their duration
195
+ # intersects the range.
196
+ def occurring_between?(opening_time, closing_time)
197
+ opening_time = opening_time - duration
198
+ closing_time = closing_time - 1 if duration > 0
199
+ occurs_between?(opening_time, closing_time)
178
200
  end
179
201
 
180
202
  # Return a boolean indicating if an occurrence falls on a certain date
181
203
  def occurs_on?(date)
182
- begin_time = TimeUtil.beginning_of_date(date)
183
- closing_time = TimeUtil.end_of_date(date)
204
+ date = TimeUtil.ensure_date date
205
+ begin_time = TimeUtil.beginning_of_date(date, start_time)
206
+ closing_time = TimeUtil.end_of_date(date, start_time)
184
207
  occurs_between?(begin_time, closing_time)
185
208
  end
186
209
 
187
210
  # Determine if the schedule is occurring at a given time
188
211
  def occurring_at?(time)
189
- if duration
212
+ if duration > 0
190
213
  return false if exception_time?(time)
191
214
  occurs_between?(time - duration + 1, time)
192
215
  else
@@ -199,6 +222,7 @@ module IceCube
199
222
  # @param [Time] closing_time - the last time to consider
200
223
  # @return [Boolean] whether or not the schedules conflict at all
201
224
  def conflicts_with?(other_schedule, closing_time = nil)
225
+ closing_time = TimeUtil.ensure_time closing_time
202
226
  unless terminating? || other_schedule.terminating? || closing_time
203
227
  raise ArgumentError.new 'At least one schedule must be terminating to use #conflicts_with?'
204
228
  end
@@ -222,7 +246,7 @@ module IceCube
222
246
  # Due to durations, we need to walk up to the end time, and verify in the
223
247
  # other direction
224
248
  if last_time
225
- last_time = terminating_schedule.duration ? last_time + terminating_schedule.duration : last_time
249
+ last_time += terminating_schedule.duration
226
250
  other_schedule.each_occurrence do |time|
227
251
  break if time > last_time
228
252
  return true if terminating_schedule.occurring_at?(time)
@@ -246,11 +270,11 @@ module IceCube
246
270
  # String serialization
247
271
  def to_s
248
272
  pieces = []
249
- ed = extimes; rd = rtimes - ed
273
+ rd = recurrence_times_with_start_time - extimes
250
274
  pieces.concat rd.sort.map { |t| t.strftime(IceCube.to_s_time_format) }
251
- pieces.concat rrules.map { |t| t.to_s }
275
+ pieces.concat rrules.map { |t| t.to_s }
252
276
  pieces.concat exrules.map { |t| "not #{t.to_s}" }
253
- pieces.concat ed.sort.map { |t| "not on #{t.strftime(IceCube.to_s_time_format)}" }
277
+ pieces.concat extimes.sort.map { |t| "not on #{t.strftime(IceCube.to_s_time_format)}" }
254
278
  pieces.join(' / ')
255
279
  end
256
280
 
@@ -258,11 +282,10 @@ module IceCube
258
282
  def to_ical(force_utc = false)
259
283
  pieces = []
260
284
  pieces << "DTSTART#{IcalBuilder.ical_format(start_time, force_utc)}"
261
- pieces << "DURATION:#{IcalBuilder.ical_duration(duration)}" if duration
262
285
  pieces.concat recurrence_rules.map { |r| "RRULE:#{r.to_ical}" }
263
- pieces.concat exception_rules.map { |r| "EXRULE:#{r.to_ical}" }
264
- pieces.concat recurrence_times.map { |t| "RDATE#{IcalBuilder.ical_format(t, force_utc)}" }
265
- pieces.concat exception_times.map { |t| "EXDATE#{IcalBuilder.ical_format(t, force_utc)}" }
286
+ pieces.concat exception_rules.map { |r| "EXRULE:#{r.to_ical}" }
287
+ pieces.concat recurrence_times_without_start_time.map { |t| "RDATE#{IcalBuilder.ical_format(t, force_utc)}" }
288
+ pieces.concat exception_times.map { |t| "EXDATE#{IcalBuilder.ical_format(t, force_utc)}" }
266
289
  pieces << "DTEND#{IcalBuilder.ical_format(end_time, force_utc)}" if end_time
267
290
  pieces.join("\n")
268
291
  end
@@ -274,7 +297,11 @@ module IceCube
274
297
 
275
298
  # Load the schedule from yaml
276
299
  def self.from_yaml(yaml, options = {})
277
- from_hash IceCube::use_psych? ? Psych::load(yaml) : YAML::load(yaml), options
300
+ hash = IceCube::use_psych? ? Psych::load(yaml) : YAML::load(yaml)
301
+ if match = yaml.match(/start_date: .+((?:-|\+)\d{2}:\d{2})$/)
302
+ TimeUtil.restore_deserialized_offset(hash[:start_date], match[1])
303
+ end
304
+ from_hash hash, options
278
305
  end
279
306
 
280
307
  # Convert the schedule to a hash
@@ -282,7 +309,6 @@ module IceCube
282
309
  data = {}
283
310
  data[:start_date] = TimeUtil.serialize_time(start_time)
284
311
  data[:end_time] = TimeUtil.serialize_time(end_time) if end_time
285
- data[:duration] = duration if duration
286
312
  data[:rrules] = recurrence_rules.map(&:to_hash)
287
313
  data[:exrules] = exception_rules.map(&:to_hash)
288
314
  data[:rtimes] = recurrence_times.map do |rt|
@@ -300,9 +326,9 @@ module IceCube
300
326
  # And then deserialize
301
327
  data = IceCube::FlexibleHash.new(original_hash)
302
328
  schedule = IceCube::Schedule.new TimeUtil.deserialize_time(data[:start_date])
303
- schedule.duration = data[:duration] if data[:duration]
329
+ schedule.end_time = schedule.start_time + data[:duration] if data[:duration]
304
330
  schedule.end_time = TimeUtil.deserialize_time(data[:end_time]) if data[:end_time]
305
- data[:rrules] && data[:rrules].each { |h| schedule.rrule(IceCube::Rule.from_hash(h)) }
331
+ data[:rrules] && data[:rrules].each { |h| schedule.rrule(IceCube::Rule.from_hash(h)) }
306
332
  data[:exrules] && data[:exrules].each { |h| schedule.exrule(IceCube::Rule.from_hash(h)) }
307
333
  data[:rtimes] && data[:rtimes].each do |t|
308
334
  schedule.add_recurrence_time TimeUtil.deserialize_time(t)
@@ -345,6 +371,9 @@ module IceCube
345
371
  # Find all of the occurrences for the schedule between opening_time
346
372
  # and closing_time
347
373
  def find_occurrences(opening_time, closing_time = nil, limit = nil, &block)
374
+ opening_time = TimeUtil.ensure_time opening_time
375
+ closing_time = TimeUtil.ensure_time closing_time
376
+ opening_time += start_time.subsec - opening_time.subsec rescue 0
348
377
  reset
349
378
  answers = []
350
379
  opening_time = start_time if opening_time < start_time
@@ -368,33 +397,19 @@ module IceCube
368
397
 
369
398
  # Get the next time after (or including) a specific time
370
399
  def next_time(time, closing_time)
371
- min_time = nil
372
400
  loop do
373
- @all_recurrence_rules.each do |rule|
401
+ min_time = recurrence_rules_with_implicit_start_occurrence.reduce(nil) do |min_time, rule|
374
402
  begin
375
- if res = rule.next_time(time, self, closing_time)
376
- if min_time.nil? || res < min_time
377
- min_time = res
378
- end
379
- end
380
- # Certain exceptions mean this rule no longer wants to play
403
+ new_time = rule.next_time(time, self, min_time || closing_time)
404
+ [min_time, new_time].compact.min
381
405
  rescue CountExceeded, UntilExceeded
382
- next
406
+ min_time
383
407
  end
384
408
  end
385
- # If there is no match, return nil
386
- return nil unless min_time
387
- # Now make sure that its not an exception_time, and if it is
388
- # then keep looking
389
- if exception_time?(min_time)
390
- time = min_time + 1
391
- min_time = nil
392
- next
393
- end
394
- # Break, we're done
395
- break
409
+ break nil unless min_time
410
+ next(time = min_time + 1) if exception_time?(min_time)
411
+ break Occurrence.new(min_time, min_time + duration)
396
412
  end
397
- min_time
398
413
  end
399
414
 
400
415
  # Return a boolean indicating if any rule needs to be run from the start of time
@@ -411,6 +426,30 @@ module IceCube
411
426
  end
412
427
  end
413
428
 
429
+ def implicit_start_occurrence
430
+ SingleOccurrenceRule.new(start_time)
431
+ end
432
+
433
+ def recurrence_times_without_start_time
434
+ recurrence_times.reject { |t| t == start_time }
435
+ end
436
+
437
+ def recurrence_times_with_start_time
438
+ if (recurrence_rules).empty?
439
+ [start_time] + recurrence_times_without_start_time
440
+ else
441
+ recurrence_times
442
+ end
443
+ end
444
+
445
+ def recurrence_rules_with_implicit_start_occurrence
446
+ if recurrence_rules.empty?
447
+ [implicit_start_occurrence] + @all_recurrence_rules
448
+ else
449
+ @all_recurrence_rules
450
+ end
451
+ end
452
+
414
453
  end
415
454
 
416
455
  end
@@ -5,7 +5,7 @@ module IceCube
5
5
  attr_reader :time
6
6
 
7
7
  def initialize(time)
8
- @time = time
8
+ @time = TimeUtil.ensure_time time
9
9
  end
10
10
 
11
11
  # Always terminating
@@ -15,7 +15,7 @@ module IceCube
15
15
 
16
16
  def next_time(t, schedule, closing_time)
17
17
  unless closing_time && closing_time < t
18
- time if time >= t
18
+ time if time.to_i >= t.to_i
19
19
  end
20
20
  end
21
21
 
@@ -0,0 +1,30 @@
1
+ module IceCube
2
+ class TimeStep
3
+ SECS = 1
4
+ MINS = 60
5
+ HOURS = MINS * 60
6
+ DAYS = HOURS * 24
7
+ WEEKS = DAYS * 7
8
+ MONTHS = {
9
+ 1 => [ 28, 29, 30, 31].map { |m| m * DAYS },
10
+ 2 => [ 59, 60, 61, 62].map { |m| m * DAYS },
11
+ 3 => [ 89, 90, 91, 92].map { |m| m * DAYS },
12
+ 4 => [120, 121, 122, 123].map { |m| m * DAYS },
13
+ 5 => [150, 151, 152, 153].map { |m| m * DAYS },
14
+ 6 => [181, 182, 183, 184].map { |m| m * DAYS },
15
+ 7 => [212, 213, 214, 215].map { |m| m * DAYS },
16
+ 8 => [242, 243, 244, 245].map { |m| m * DAYS },
17
+ 9 => [273, 274, 275, 276].map { |m| m * DAYS },
18
+ 10 => [303, 304, 305, 306].map { |m| m * DAYS },
19
+ 11 => [334, 335, 336, 337].map { |m| m * DAYS },
20
+ 12 => [365, 366] .map { |m| m * DAYS }
21
+ }
22
+ YEARS = [365, 366]
23
+
24
+ def next(base, validations)
25
+ end
26
+
27
+ def prev(base, validations)
28
+ end
29
+ end
30
+ end