ice_cube 0.10.0 → 0.11.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.
@@ -0,0 +1,65 @@
1
+ module IceCube
2
+ class Enumerator < ::Enumerator
3
+
4
+ def initialize(schedule, from_time, to_time)
5
+ @schedule = schedule
6
+ @from_time = TimeUtil.ensure_time(from_time)
7
+ @to_time = TimeUtil.ensure_time(to_time)
8
+ align_start_time
9
+ @cursor = @from_time
10
+ end
11
+
12
+ def each
13
+ while res = self.find_next && @to_time.nil? || res <= @to_time
14
+ yield Occurrence.new(res, res + schedule.duration)
15
+ end
16
+ raise StopIteration
17
+ end
18
+
19
+ def find_next
20
+ loop do
21
+ min_time = recurrence_rules.reduce(nil) do |min_time, rule|
22
+ begin
23
+ new_time = rule.next_time(time, schedule, min_time || @to_time)
24
+ [min_time, new_time].compact.min
25
+ rescue CountExceeded, UntilExceeded
26
+ min_time
27
+ end
28
+ end
29
+ break nil unless min_time
30
+ @cursor = min_time + 1
31
+ next if exception_time?(min_time)
32
+ break min_time
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def align_start_time
39
+ if @from_time <= schedule.start_time || full_required?
40
+ @from_time = schedule.start_time
41
+ else
42
+ @from_time += @schedule.start_time.subsec - @from_time.subsec rescue 0
43
+ end
44
+ end
45
+
46
+ # Return a boolean indicating if any rule needs to be run from the start of time
47
+ def full_required?
48
+ recurrence_rules.any?(&:full_required?) ||
49
+ exception_rules.any?(&:full_required?)
50
+ end
51
+
52
+ def exception_rules
53
+ schedule.instance_variable_get(:@all_exception_rules)
54
+ end
55
+
56
+ def recurrence_rules
57
+ @recurrence_rules ||= if recurrence_rules.empty?
58
+ [SingleOccurrenceRule.new(schedule.start_time)].concat schedule.instance_variable_get(:@all_recurrence_rules)
59
+ else
60
+ schedule.instance_variable_get(:@all_recurrence_rules)
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -21,9 +21,14 @@ module IceCube
21
21
  #
22
22
  class Occurrence < SimpleDelegator
23
23
 
24
+ # Report class name as 'Time' to thwart type checking.
25
+ def self.name
26
+ 'Time'
27
+ end
28
+
24
29
  # Optimize for common methods to avoid method_missing
25
30
  extend Forwardable
26
- def_delegators :start_time, :to_s, :to_i, :<=>, :==
31
+ def_delegators :start_time, :to_i, :<=>, :==
27
32
  def_delegators :to_range, :cover?, :include?, :each, :first, :last
28
33
 
29
34
  attr_reader :start_time, :end_time
@@ -74,12 +79,17 @@ module IceCube
74
79
  start_time
75
80
  end
76
81
 
77
- def to_s
78
- if duration > 0
79
- "#{start_time} - #{end_time}"
82
+ # Shows both the start and end time if there is a duration.
83
+ # Optional format argument (e.g. :long, :short) supports Rails
84
+ # time formats and is only used when ActiveSupport is available.
85
+ #
86
+ def to_s(format=nil)
87
+ if format && to_time.public_method(:to_s).arity > 0
88
+ t0, t1 = start_time.to_s(format), end_time.to_s(format)
80
89
  else
81
- "#{start_time}"
90
+ t0, t1 = start_time.to_s, end_time.to_s
82
91
  end
92
+ duration > 0 ? "#{t0} - #{t1}" : t0
83
93
  end
84
94
 
85
95
  def overnight?
@@ -5,6 +5,7 @@ module IceCube
5
5
  include Validations::DailyInterval
6
6
 
7
7
  def initialize(interval = 1)
8
+ super
8
9
  interval(interval)
9
10
  schedule_lock(:hour, :min, :sec)
10
11
  reset
@@ -5,6 +5,7 @@ module IceCube
5
5
  include Validations::HourlyInterval
6
6
 
7
7
  def initialize(interval = 1)
8
+ super
8
9
  interval(interval)
9
10
  schedule_lock(:min, :sec)
10
11
  reset
@@ -5,6 +5,7 @@ module IceCube
5
5
  include Validations::MinutelyInterval
6
6
 
7
7
  def initialize(interval = 1)
8
+ super
8
9
  interval(interval)
9
10
  schedule_lock(:sec)
10
11
  reset
@@ -5,6 +5,7 @@ module IceCube
5
5
  include Validations::MonthlyInterval
6
6
 
7
7
  def initialize(interval = 1)
8
+ super
8
9
  interval(interval)
9
10
  schedule_lock(:day, :hour, :min, :sec)
10
11
  reset
@@ -5,6 +5,7 @@ module IceCube
5
5
  include Validations::SecondlyInterval
6
6
 
7
7
  def initialize(interval = 1)
8
+ super
8
9
  interval(interval)
9
10
  reset
10
11
  end
@@ -5,6 +5,7 @@ module IceCube
5
5
  include Validations::WeeklyInterval
6
6
 
7
7
  def initialize(interval = 1, week_start = :sunday)
8
+ super
8
9
  interval(interval, week_start)
9
10
  schedule_lock(:wday, :hour, :min, :sec)
10
11
  reset
@@ -5,6 +5,7 @@ module IceCube
5
5
  include Validations::YearlyInterval
6
6
 
7
7
  def initialize(interval = 1)
8
+ super
8
9
  interval(interval)
9
10
  schedule_lock(:month, :day, :hour, :min, :sec)
10
11
  reset
@@ -150,7 +150,7 @@ module IceCube
150
150
 
151
151
  # All of the occurrences
152
152
  def all_occurrences
153
- raise ArgumentError.new('Rule must specify either an until date or a count to use #all_occurrences') unless terminating?
153
+ require_terminating_rules
154
154
  find_occurrences(start_time)
155
155
  end
156
156
 
@@ -172,8 +172,21 @@ module IceCube
172
172
  find_occurrences(from + 1, nil, 1).first
173
173
  end
174
174
 
175
+ # The previous occurrence from a given time
176
+ def previous_occurrence(from)
177
+ return nil if from <= start_time
178
+ find_occurrences(start_time, from - 1, nil, 1).last
179
+ end
180
+
181
+ # The previous n occurrences before a given time
182
+ def previous_occurrences(num, from)
183
+ return [] if from <= start_time
184
+ find_occurrences(start_time, from - 1, nil, num)
185
+ end
186
+
175
187
  # The remaining occurrences (same requirements as all_occurrences)
176
188
  def remaining_occurrences(from = nil)
189
+ require_terminating_rules
177
190
  from ||= TimeUtil.now(@start_time)
178
191
  find_occurrences(from)
179
192
  end
@@ -267,6 +280,14 @@ module IceCube
267
280
  n.nil? ? occurrences.first : occurrences
268
281
  end
269
282
 
283
+ # Get the final n occurrences of a terminating schedule
284
+ # or the final one if no n is given
285
+ def last(n = nil)
286
+ require_terminating_rules
287
+ occurrences = find_occurrences(start_time, nil, nil, n || 1)
288
+ n.nil? ? occurrences.last : occurrences[-n..-1]
289
+ end
290
+
270
291
  # String serialization
271
292
  def to_s
272
293
  pieces = []
@@ -370,7 +391,7 @@ module IceCube
370
391
 
371
392
  # Find all of the occurrences for the schedule between opening_time
372
393
  # and closing_time
373
- def find_occurrences(opening_time, closing_time = nil, limit = nil, &block)
394
+ def find_occurrences(opening_time, closing_time = nil, limit = nil, tail_limit = nil, &block)
374
395
  opening_time = TimeUtil.ensure_time opening_time
375
396
  closing_time = TimeUtil.ensure_time closing_time
376
397
  opening_time += start_time.subsec - opening_time.subsec rescue 0
@@ -387,6 +408,7 @@ module IceCube
387
408
  break if closing_time && res > closing_time
388
409
  if res >= opening_time
389
410
  block_given? ? block.call(res) : (answers << res)
411
+ answers.shift if tail_limit && answers.length > tail_limit
390
412
  break if limit && answers.length == limit
391
413
  end
392
414
  time = res + 1
@@ -426,7 +448,13 @@ module IceCube
426
448
  end
427
449
  end
428
450
 
429
- def implicit_start_occurrence
451
+ def require_terminating_rules
452
+ return true if terminating?
453
+ method_name = caller[0].split(' ').last
454
+ raise ArgumentError, "All recurrence rules must specify .until or .count to use #{method_name}"
455
+ end
456
+
457
+ def implicit_start_occurrence_rule
430
458
  SingleOccurrenceRule.new(start_time)
431
459
  end
432
460
 
@@ -435,8 +463,8 @@ module IceCube
435
463
  end
436
464
 
437
465
  def recurrence_times_with_start_time
438
- if (recurrence_rules).empty?
439
- [start_time] + recurrence_times_without_start_time
466
+ if recurrence_rules.empty?
467
+ [start_time].concat recurrence_times_without_start_time
440
468
  else
441
469
  recurrence_times
442
470
  end
@@ -444,7 +472,7 @@ module IceCube
444
472
 
445
473
  def recurrence_rules_with_implicit_start_occurrence
446
474
  if recurrence_rules.empty?
447
- [implicit_start_occurrence] + @all_recurrence_rules
475
+ [implicit_start_occurrence_rule].concat @all_recurrence_rules
448
476
  else
449
477
  @all_recurrence_rules
450
478
  end
@@ -22,6 +22,7 @@ module IceCube
22
22
  end
23
23
 
24
24
  def self.match_zone(time, reference)
25
+ return unless time = ensure_time(time)
25
26
  if reference.respond_to? :time_zone
26
27
  time.in_time_zone(reference.time_zone)
27
28
  else
@@ -160,7 +161,7 @@ module IceCube
160
161
  # overflowing shorter months
161
162
  def self.days_to_next_month(time)
162
163
  date = Date.new(time.year, time.month, time.day)
163
- (date >> 1) - date
164
+ ((date >> 1) - date).to_i
164
165
  end
165
166
 
166
167
  # Get a day of the month in the month of a given time without overflowing
@@ -30,6 +30,10 @@ module IceCube
30
30
  :interval
31
31
  ]
32
32
 
33
+ def initialize(interval = 1, *)
34
+ @validations = Hash.new
35
+ end
36
+
33
37
  # Compute the next time after (or including) the specified time in respect
34
38
  # to the given schedule
35
39
  def next_time(time, schedule, closing_time)
@@ -74,7 +78,6 @@ module IceCube
74
78
 
75
79
  # Get the collection that contains validations of a certain type
76
80
  def validations_for(key)
77
- @validations ||= {}
78
81
  @validations[key] ||= []
79
82
  end
80
83
 
@@ -8,8 +8,11 @@ module IceCube
8
8
  end
9
9
 
10
10
  def count(max)
11
+ unless max.nil? || max.is_a?(Fixnum)
12
+ raise ArgumentError, "Expecting Fixnum or nil value for count, got #{max.inspect}"
13
+ end
11
14
  @count = max
12
- replace_validations_for(:count, [Validation.new(max, self)]) # replace
15
+ replace_validations_for(:count, [Validation.new(max, self)])
13
16
  self
14
17
  end
15
18
 
@@ -5,7 +5,7 @@ module IceCube
5
5
  # Add a new interval validation
6
6
  def interval(interval)
7
7
  @interval = interval
8
- validations_for(:interval) << Validation.new(interval)
8
+ replace_validations_for(:interval, [Validation.new(interval)])
9
9
  clobber_base_validations(:wday, :day)
10
10
  self
11
11
  end
@@ -5,7 +5,10 @@ module IceCube
5
5
  module Validations::Day
6
6
 
7
7
  def day(*days)
8
- days.each do |day|
8
+ days.flatten.each do |day|
9
+ unless day.is_a?(Fixnum) || day.is_a?(Symbol)
10
+ raise ArgumentError, "expecting Fixnum or Symbol value for day, got #{day.inspect}"
11
+ end
9
12
  day = TimeUtil.sym_to_wday(day)
10
13
  validations_for(:day) << Validation.new(day)
11
14
  end
@@ -5,7 +5,10 @@ module IceCube
5
5
  include Validations::Lock
6
6
 
7
7
  def day_of_month(*days)
8
- days.each do |day|
8
+ days.flatten.each do |day|
9
+ unless day.is_a?(Fixnum)
10
+ raise ArgumentError, "expecting Fixnum value for day, got #{day.inspect}"
11
+ end
9
12
  validations_for(:day_of_month) << Validation.new(day)
10
13
  end
11
14
  clobber_base_validations(:day, :wday)
@@ -3,7 +3,10 @@ module IceCube
3
3
  module Validations::DayOfYear
4
4
 
5
5
  def day_of_year(*days)
6
- days.each do |day|
6
+ days.flatten.each do |day|
7
+ unless day.is_a?(Fixnum)
8
+ raise ArgumentError, "expecting Fixnum value for day, got #{day.inspect}"
9
+ end
7
10
  validations_for(:day_of_year) << Validation.new(day)
8
11
  end
9
12
  clobber_base_validations(:month, :day, :wday)
@@ -6,7 +6,10 @@ module IceCube
6
6
 
7
7
  # Add hour of day validations
8
8
  def hour_of_day(*hours)
9
- hours.each do |hour|
9
+ hours.flatten.each do |hour|
10
+ unless hour.is_a?(Fixnum)
11
+ raise ArgumentError, "expecting Fixnum value for hour, got #{hour.inspect}"
12
+ end
10
13
  validations_for(:hour_of_day) << Validation.new(hour)
11
14
  end
12
15
  clobber_base_validations(:hour)
@@ -4,7 +4,7 @@ module IceCube
4
4
 
5
5
  def interval(interval)
6
6
  @interval = interval
7
- validations_for(:interval) << Validation.new(interval)
7
+ replace_validations_for(:interval, [Validation.new(interval)])
8
8
  clobber_base_validations(:hour)
9
9
  self
10
10
  end
@@ -62,8 +62,8 @@ module IceCube
62
62
  if value && value > 0
63
63
  until_next_month = days_in_month + sleeps
64
64
  else
65
- until_next_month = TimeUtil.days_to_next_month(date) + sleeps
66
- until_next_month -= month_overflow
65
+ until_next_month = start < 28 ? days_in_month : TimeUtil.days_to_next_month(date)
66
+ until_next_month += sleeps - month_overflow
67
67
  end
68
68
 
69
69
  sleeps >= 0 ? sleeps : until_next_month
@@ -5,7 +5,10 @@ module IceCube
5
5
  include Validations::Lock
6
6
 
7
7
  def minute_of_hour(*minutes)
8
- minutes.each do |minute|
8
+ minutes.flatten.each do |minute|
9
+ unless minute.is_a?(Fixnum)
10
+ raise ArgumentError, "expecting Fixnum value for minute, got #{minute.inspect}"
11
+ end
9
12
  validations_for(:minute_of_hour) << Validation.new(minute)
10
13
  end
11
14
  clobber_base_validations(:min)
@@ -4,7 +4,7 @@ module IceCube
4
4
 
5
5
  def interval(interval)
6
6
  @interval = interval
7
- validations_for(:interval) << Validation.new(interval)
7
+ replace_validations_for(:interval, [Validation.new(interval)])
8
8
  clobber_base_validations(:min)
9
9
  self
10
10
  end
@@ -3,7 +3,10 @@ module IceCube
3
3
  module Validations::MonthOfYear
4
4
 
5
5
  def month_of_year(*months)
6
- months.each do |month|
6
+ months.flatten.each do |month|
7
+ unless month.is_a?(Fixnum) || month.is_a?(Symbol)
8
+ raise ArgumentError, "expecting Fixnum or Symbol value for month, got #{month.inspect}"
9
+ end
7
10
  month = TimeUtil.sym_to_month(month)
8
11
  validations_for(:month_of_year) << Validation.new(month)
9
12
  end
@@ -2,9 +2,9 @@ module IceCube
2
2
 
3
3
  module Validations::MonthlyInterval
4
4
 
5
- def interval(interval = 1)
5
+ def interval(interval)
6
6
  @interval = interval
7
- validations_for(:interval) << Validation.new(interval)
7
+ replace_validations_for(:interval, [Validation.new(interval)])
8
8
  clobber_base_validations(:month)
9
9
  self
10
10
  end
@@ -3,7 +3,10 @@ module IceCube
3
3
  module Validations::SecondOfMinute
4
4
 
5
5
  def second_of_minute(*seconds)
6
- seconds.each do |second|
6
+ seconds.flatten.each do |second|
7
+ unless second.is_a?(Fixnum)
8
+ raise ArgumentError, "Expecting Fixnum value for second, got #{second.inspect}"
9
+ end
7
10
  validations_for(:second_of_minute) << Validation.new(second)
8
11
  end
9
12
  clobber_base_validations :sec
@@ -4,7 +4,7 @@ module IceCube
4
4
 
5
5
  def interval(interval)
6
6
  @interval = interval
7
- validations_for(:interval) << Validation.new(interval)
7
+ replace_validations_for(:interval, [Validation.new(interval)])
8
8
  clobber_base_validations(:sec)
9
9
  self
10
10
  end
@@ -11,7 +11,7 @@ module IceCube
11
11
  deprecated_alias :until_date, :until_time
12
12
 
13
13
  def until(time)
14
- time = TimeUtil.ensure_time time, true
14
+ time = TimeUtil.ensure_time(time, true)
15
15
  @until = time
16
16
  replace_validations_for(:until, time.nil? ? nil : [Validation.new(time)])
17
17
  self
@@ -7,7 +7,7 @@ module IceCube
7
7
  def interval(interval, week_start = :sunday)
8
8
  @interval = interval
9
9
  @week_start = TimeUtil.wday_to_sym(week_start)
10
- validations_for(:interval) << Validation.new(interval, week_start)
10
+ replace_validations_for(:interval, [Validation.new(interval, week_start)])
11
11
  clobber_base_validations(:day)
12
12
  self
13
13
  end
@@ -2,9 +2,9 @@ module IceCube
2
2
 
3
3
  module Validations::YearlyInterval
4
4
 
5
- def interval(interval = 1)
5
+ def interval(interval)
6
6
  @interval = interval
7
- validations_for(:interval) << Validation.new(interval)
7
+ replace_validations_for(:interval, [Validation.new(interval)])
8
8
  clobber_base_validations(:year)
9
9
  end
10
10
 
@@ -1,5 +1,5 @@
1
1
  module IceCube
2
2
 
3
- VERSION = '0.10.0'
3
+ VERSION = '0.11.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,69 +1,78 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ice_cube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - John Crepezzi
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-02-25 00:00:00.000000000 Z
12
+ date: 2013-06-13 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rspec
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: active_support
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: 3.0.0
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: 3.0.0
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: tzinfo
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - '>='
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - '>='
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  description: ice_cube is a recurring date library for Ruby. It allows for quick,
@@ -77,6 +86,7 @@ files:
77
86
  - lib/ice_cube/builders/ical_builder.rb
78
87
  - lib/ice_cube/builders/string_builder.rb
79
88
  - lib/ice_cube/deprecated.rb
89
+ - lib/ice_cube/enumerator.rb
80
90
  - lib/ice_cube/errors/count_exceeded.rb
81
91
  - lib/ice_cube/errors/until_exceeded.rb
82
92
  - lib/ice_cube/flexible_hash.rb
@@ -119,26 +129,33 @@ files:
119
129
  homepage: http://seejohnrun.github.com/ice_cube/
120
130
  licenses:
121
131
  - MIT
122
- metadata: {}
123
132
  post_install_message:
124
133
  rdoc_options: []
125
134
  require_paths:
126
135
  - lib
127
136
  required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
128
138
  requirements:
129
- - - '>='
139
+ - - ! '>='
130
140
  - !ruby/object:Gem::Version
131
141
  version: '0'
142
+ segments:
143
+ - 0
144
+ hash: 3343770401458335569
132
145
  required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
133
147
  requirements:
134
- - - '>='
148
+ - - ! '>='
135
149
  - !ruby/object:Gem::Version
136
150
  version: '0'
151
+ segments:
152
+ - 0
153
+ hash: 3343770401458335569
137
154
  requirements: []
138
155
  rubyforge_project: ice-cube
139
- rubygems_version: 2.0.0
156
+ rubygems_version: 1.8.25
140
157
  signing_key:
141
- specification_version: 4
158
+ specification_version: 3
142
159
  summary: Ruby Date Recurrence Library
143
160
  test_files:
144
161
  - spec/spec_helper.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 0cc5c669bd801317d8256d7fc8b77841e80dae6e
4
- data.tar.gz: 6bda80589b1d0a91d54ea2d7b4194d320e26b2ac
5
- SHA512:
6
- metadata.gz: d3a10207a9dc7368b3cc6f045160c2b3f6651e1a95570a8d6c58af689eb616deb47291485b514b4ec58b1275595dce7f7c21f1606341d624449edfc5c842648d
7
- data.tar.gz: 0abec738ddd565630a8a92468a8db708c2c18e2601abe3824ba10b766ce952341d7ff90da50a7ac6f4349d079661d66e8b131f177cbf4ee2853b4b046d4389a6