ice_cube 0.6.10 → 0.7.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.
Files changed (43) hide show
  1. data/lib/ice_cube/builders/hash_builder.rb +27 -0
  2. data/lib/ice_cube/builders/ical_builder.rb +59 -0
  3. data/lib/ice_cube/builders/string_builder.rb +74 -0
  4. data/lib/ice_cube/errors/count_exceeded.rb +7 -0
  5. data/lib/ice_cube/errors/until_exceeded.rb +7 -0
  6. data/lib/ice_cube/rule.rb +86 -140
  7. data/lib/ice_cube/rules/daily_rule.rb +5 -27
  8. data/lib/ice_cube/rules/hourly_rule.rb +6 -26
  9. data/lib/ice_cube/rules/minutely_rule.rb +5 -25
  10. data/lib/ice_cube/rules/monthly_rule.rb +6 -30
  11. data/lib/ice_cube/rules/secondly_rule.rb +5 -26
  12. data/lib/ice_cube/rules/weekly_rule.rb +5 -36
  13. data/lib/ice_cube/rules/yearly_rule.rb +8 -34
  14. data/lib/ice_cube/schedule.rb +262 -189
  15. data/lib/ice_cube/single_occurrence_rule.rb +28 -0
  16. data/lib/ice_cube/time_util.rb +202 -76
  17. data/lib/ice_cube/validated_rule.rb +107 -0
  18. data/lib/ice_cube/validations/count.rb +56 -0
  19. data/lib/ice_cube/validations/daily_interval.rb +51 -0
  20. data/lib/ice_cube/validations/day.rb +45 -29
  21. data/lib/ice_cube/validations/day_of_month.rb +45 -43
  22. data/lib/ice_cube/validations/day_of_week.rb +59 -41
  23. data/lib/ice_cube/validations/day_of_year.rb +49 -43
  24. data/lib/ice_cube/validations/hour_of_day.rb +43 -29
  25. data/lib/ice_cube/validations/hourly_interval.rb +50 -0
  26. data/lib/ice_cube/validations/lock.rb +47 -0
  27. data/lib/ice_cube/validations/minute_of_hour.rb +43 -30
  28. data/lib/ice_cube/validations/minutely_interval.rb +50 -0
  29. data/lib/ice_cube/validations/month_of_year.rb +40 -29
  30. data/lib/ice_cube/validations/monthly_interval.rb +47 -0
  31. data/lib/ice_cube/validations/schedule_lock.rb +41 -0
  32. data/lib/ice_cube/validations/second_of_minute.rb +40 -29
  33. data/lib/ice_cube/validations/secondly_interval.rb +50 -0
  34. data/lib/ice_cube/validations/until.rb +49 -0
  35. data/lib/ice_cube/validations/weekly_interval.rb +50 -0
  36. data/lib/ice_cube/validations/yearly_interval.rb +45 -0
  37. data/lib/ice_cube/version.rb +2 -2
  38. data/lib/ice_cube.rb +63 -37
  39. data/spec/spec_helper.rb +13 -0
  40. metadata +47 -8
  41. data/lib/ice_cube/rule_occurrence.rb +0 -100
  42. data/lib/ice_cube/validation.rb +0 -44
  43. data/lib/ice_cube/validation_types.rb +0 -137
@@ -1,40 +1,14 @@
1
1
  module IceCube
2
2
 
3
- class YearlyRule < Rule
4
-
5
- # Determine whether or not the rule, given a start_date,
6
- # occurs on a given date.
7
- # Yearly occurs if we're in a proper interval
8
- # and either (1) we're on a day of the year, or (2) we're on a month of the year as specified
9
- # Note: rollover dates don't work, so you can't ask for the 400th day of a year
10
- # and expect to roll into the next year (this might be a possible direction in the future)
11
- def in_interval?(date, start_date)
12
- #make sure we're in the proper interval
13
- (date.year - start_date.year) % @interval == 0
14
- end
15
-
16
- def to_ical
17
- 'FREQ=YEARLY' << to_ical_base
18
- end
19
-
20
- def to_s
21
- to_s_base 'Yearly', "Every #{@interval} years"
22
- end
23
-
24
- protected
25
-
26
- # one year from now, the same month and day of the year
27
- def default_jump(date, attempt_count = 1)
28
- # jump by months since there's no reliable way to jump by year
29
- TimeUtil.date_in_n_months(date, attempt_count * @interval * 12)
30
- end
31
-
32
- private
3
+ class YearlyRule < ValidatedRule
33
4
 
34
- def initialize(interval)
35
- super(interval)
5
+ include Validations::YearlyInterval
6
+
7
+ def initialize(interval = 1)
8
+ interval(interval)
9
+ schedule_lock(:month, :day, :hour, :min, :sec)
36
10
  end
37
-
11
+
38
12
  end
39
-
13
+
40
14
  end
@@ -1,262 +1,335 @@
1
+ require 'yaml'
2
+
1
3
  module IceCube
2
4
 
3
5
  class Schedule
4
6
 
5
- attr_reader :rdates, :exdates, :start_date, :duration, :end_time
7
+ # Get the start time
8
+ attr_accessor :start_time
9
+ alias :start_date :start_time
10
+ alias :start_date= :start_time=
11
+
12
+ # Get the duration
13
+ attr_accessor :duration
6
14
 
15
+ # Get the end time
16
+ attr_accessor :end_time
7
17
  alias :end_date :end_time
8
- alias :start_time :start_date
9
-
10
- def initialize(start_date, options = {})
11
- @rrule_occurrence_heads = []
12
- @exrule_occurrence_heads = []
13
- @rdates = []
14
- @exdates = []
15
- @start_date = start_date || Time.now
16
- raise ArgumentError.new('Duration cannot be negative') if options[:duration] && options[:duration] < 0
17
- @duration = options[:duration]
18
- raise ArgumentError.new('Start time must be before end time') if options[:end_time] && options[:end_time] < @start_date
18
+
19
+ # Create a new schedule
20
+ def initialize(start_time = nil, options = {})
21
+ @start_time = start_time || Time.now
19
22
  @end_time = options[:end_time]
23
+ @duration = options[:duration]
24
+ @all_recurrence_rules = []
25
+ @all_exception_rules = []
20
26
  end
21
27
 
22
- # Convert the schedule to a hash, reverse of Schedule.from_hash
23
- def to_hash
24
- hash = Hash.new
25
- hash[:start_date] = @start_date
26
- hash[:rrules] = @rrule_occurrence_heads.map { |rr| rr.rule.to_hash }
27
- hash[:exrules] = @exrule_occurrence_heads.map { |ex| ex.rule.to_hash }
28
- hash[:rdates] = @rdates
29
- hash[:exdates] = @exdates
30
- hash[:duration] = @duration
31
- hash[:end_time] = @end_time
32
- hash
33
- end
34
-
35
- # Convert the schedule to yaml, reverse of Schedule.from_yaml
36
- def to_yaml(options = {})
37
- hash = to_hash
38
- hash[:start_date] = TimeUtil.serialize_time(hash[:start_date])
39
- hash[:rdates] = hash[:rdates].map { |t| TimeUtil.serialize_time(t) }
40
- hash[:exdates] = hash[:exdates].map { |t| TimeUtil.serialize_time(t) }
41
- hash[:end_time] = TimeUtil.serialize_time(hash[:end_time])
42
- hash.to_yaml(options)
43
- end
44
-
45
- # Create a schedule from a hash created by instance.to_hash
46
- def self.from_hash(hash, hash_options = {})
47
- options = {}
48
- options[:duration] = hash[:duration] if hash.has_key?(:duration)
49
- options[:end_time] = TimeUtil.deserialize_time(hash[:end_time]) if hash.has_key?(:end_time)
50
- start_date = hash_options[:start_date_override] || TimeUtil.deserialize_time(hash[:start_date])
51
- schedule = Schedule.new(start_date, options)
52
- hash[:rrules].each { |rr| schedule.add_recurrence_rule Rule.from_hash(rr) }
53
- hash[:exrules].each { |ex| schedule.add_exception_rule Rule.from_hash(ex) }
54
- hash[:rdates].each { |rd| schedule.add_recurrence_date TimeUtil.deserialize_time(rd) }
55
- hash[:exdates].each { |ed| schedule.add_exception_date TimeUtil.deserialize_time(ed) }
56
- schedule
28
+ # Add a recurrence time to the schedule
29
+ def add_recurrence_time(time)
30
+ return nil if time.nil?
31
+ rule = SingleOccurrenceRule.new(time)
32
+ add_recurrence_rule rule
33
+ time
57
34
  end
35
+ alias :rdate :add_recurrence_time
36
+ alias :add_recurrence_date :add_recurrence_time
37
+
38
+ # Add an exception time to the schedule
39
+ def add_exception_time(time)
40
+ return nil if time.nil?
41
+ rule = SingleOccurrenceRule.new(time)
42
+ add_exception_rule rule
43
+ time
44
+ end
45
+ alias :exdate :add_exception_time
46
+ alias :add_exception_date :add_exception_time
58
47
 
59
- # Create a schedule from a yaml string created by instance.to_yaml
60
- def self.from_yaml(str, hash_options = {})
61
- from_hash(YAML::load(str), hash_options)
48
+ # Add a recurrence rule to the schedule
49
+ def add_recurrence_rule(rule)
50
+ @all_recurrence_rules << rule unless @all_recurrence_rules.include?(rule)
62
51
  end
52
+ alias :rrule :add_recurrence_rule
63
53
 
64
- TIME_FORMAT = '%B %e, %Y'
65
- SEPARATOR = ' / '
66
- NEWLINE = "\n"
54
+ # Remove a recurrence rule
55
+ def remove_recurrence_rule(rule)
56
+ res = @all_recurrence_rules.delete(rule)
57
+ res.nil? ? [] : [res]
58
+ end
67
59
 
68
- # use with caution
69
- # incomplete and not entirely tested - no time representation in dates
70
- # there's a lot that can happen here
71
- def to_s
72
- representation_pieces = []
73
- inc_dates = (@rdates - @exdates).uniq
74
- representation_pieces.concat inc_dates.sort.map { |d| d.strftime(TIME_FORMAT) } unless inc_dates.empty?
75
- representation_pieces.concat @rrule_occurrence_heads.map{ |r| r.rule.to_s } if @rrule_occurrence_heads
76
- representation_pieces.concat @exrule_occurrence_heads.map { |r| 'not ' << r.rule.to_s } if @exrule_occurrence_heads
77
- representation_pieces.concat @exdates.uniq.sort.map { |d| 'not on ' << d.strftime(TIME_FORMAT) } if @exdates
78
- representation_pieces << "until #{end_time.strftime(TIME_FORMAT)}" if @end_time
79
- representation_pieces.join(SEPARATOR)
60
+ # Add an exception rule to the schedule
61
+ def add_exception_rule(rule)
62
+ @all_exception_rules << rule unless @all_exception_rules.include?(rule)
80
63
  end
64
+ alias :exrule :add_exception_rule
81
65
 
82
- def to_ical(force_utc = false)
83
- representation_pieces = ["DTSTART#{TimeUtil.ical_format(@start_date, force_utc)}"]
84
- representation_pieces << "DURATION:#{TimeUtil.ical_duration(@duration)}" if @duration
85
- inc_dates = (@rdates - @exdates).uniq
86
- representation_pieces.concat inc_dates.sort.map { |d| "RDATE#{TimeUtil.ical_format(d, force_utc)}" } if inc_dates.any?
87
- representation_pieces.concat @exdates.uniq.sort.map { |d| "EXDATE#{TimeUtil.ical_format(d, force_utc)}" } if @exdates
88
- representation_pieces.concat @rrule_occurrence_heads.map { |r| "RRULE:#{r.rule.to_ical}" } if @rrule_occurrence_heads
89
- representation_pieces.concat @exrule_occurrence_heads.map { |r| "EXRULE:#{r.rule.to_ical}" } if @exrule_occurrence_heads
90
- representation_pieces << "DTEND#{TimeUtil.ical_format(@end_time, force_utc)}" if @end_time
91
- representation_pieces.join(NEWLINE)
66
+ # Remove an exception rule
67
+ def remove_exception_rule(rule)
68
+ res = @all_exception_rules.delete(rule)
69
+ res.nil? ? [] : [res]
92
70
  end
93
71
 
94
- def occurring_at?(time)
95
- return false if @exdates.include?(time)
96
- return true if @rdates.include?(time)
97
- return false if any_occurring_at?(@exrule_occurrence_heads, time)
98
- any_occurring_at?(@rrule_occurrence_heads, time)
72
+ # Get the recurrence rules
73
+ def recurrence_rules
74
+ @all_recurrence_rules.reject { |r| r.is_a?(SingleOccurrenceRule) }
99
75
  end
76
+ alias :rrules :recurrence_rules
100
77
 
101
- # Determine whether a given time adheres to the ruleset of this schedule.
102
- def occurs_at?(date)
103
- return false if @end_time && date > @end_time
104
- dates = occurrences(date)
105
- dates.last == date
78
+ # Get the exception rules
79
+ def exception_rules
80
+ @all_exception_rules.reject { |r| r.is_a?(SingleOccurrenceRule) }
106
81
  end
82
+ alias :exrules :exception_rules
107
83
 
108
- # Determine whether a given date appears in the times returned by the schedule
109
- def occurs_on?(date)
110
- if defined?(ActiveSupport::TimeWithZone) && @start_date.is_a?(ActiveSupport::TimeWithZone)
111
- return active_support_occurs_on?(date)
84
+ # Get the recurrence times that are on the schedule
85
+ def recurrence_times
86
+ @all_recurrence_rules.select { |r| r.is_a?(SingleOccurrenceRule) }.map(&:time)
87
+ end
88
+ alias :rdates :recurrence_times
89
+ alias :recurrence_dates :recurrence_times
90
+
91
+ # Remove a recurrence time
92
+ def remove_recurrence_time(time)
93
+ found = false
94
+ @all_recurrence_rules.delete_if do |rule|
95
+ found = true if rule.is_a?(SingleOccurrenceRule) && rule.time == time
112
96
  end
113
- # fall back to our own way of doing things
114
- time_format = @start_date.utc? ? :utc : :local
115
- self.occurrences_between(Time.send(time_format, date.year, date.month, date.day, 0, 0, 0), Time.send(time_format, date.year, date.month, date.day, 23, 59, 59)).any?
97
+ time if found
116
98
  end
99
+ alias :remove_recurrence_date :remove_recurrence_time
100
+ alias :remove_rdate :remove_recurrence_time
117
101
 
118
- def occurs_between?(begin_time, end_time)
119
- if defined?(ActiveSupport::TimeWithZone) && @start_date.is_a?(ActiveSupport::TimeWithZone)
120
- return active_support_occurs_between?(begin_time, end_time)
102
+ # Get the exception times that are on the schedule
103
+ def exception_times
104
+ @all_exception_rules.select { |r| r.is_a?(SingleOccurrenceRule) }.map(&:time)
105
+ end
106
+ alias :exdates :exception_times
107
+ alias :exception_dates :exception_times
108
+
109
+ # Remove an exception time
110
+ def remove_exception_time(time)
111
+ found = false
112
+ @all_exception_rules.delete_if do |rule|
113
+ found = true if rule.is_a?(SingleOccurrenceRule) && rule.time == time
121
114
  end
122
- # fall back to our own way of doing things
123
- time_format = @start_date.utc? ? :utc : :local
124
- self.occurrences_between(Time.send(time_format, begin_time.year, begin_time.month, begin_time.day, 0, 0, 0), Time.send(time_format, end_time.year, end_time.month, end_time.day, 23, 59, 59)).any?
115
+ time if found
116
+ end
117
+ alias :remove_exception_date :remove_exception_time
118
+ alias :remove_exdate :remove_exception_time
119
+
120
+ # Get all of the occurrences from the start_time up until a
121
+ # given Time
122
+ def occurrences(closing_time)
123
+ find_occurrences(start_time, closing_time)
125
124
  end
126
125
 
127
- # Return all possible occurrences
128
- # In order to make this call, all rules in the schedule must have
129
- # either an until date or an occurrence count
126
+ # All of the occurrences
130
127
  def all_occurrences
131
- find_occurrences { |head| head.all_occurrences }
128
+ unless end_time || recurrence_rules.all?(&:terminating?)
129
+ raise ArgumentError.new('Rule must specify either an until date or a count to use #all_occurrences')
130
+ end
131
+ find_occurrences(start_time)
132
132
  end
133
133
 
134
- # Find all occurrences until a certain date
135
- def occurrences(end_date)
136
- end_date = @end_time if @end_time && @end_time < end_date
137
- find_occurrences { |head| head.upto(end_date) }
134
+ # Iterate forever
135
+ def each_occurrence(&block)
136
+ find_occurrences(start_time, &block)
137
+ self
138
138
  end
139
139
 
140
- # Find remaining occurrences
141
- def remaining_occurrences(from = Time.now)
142
- raise ArgumentError.new('Schedule must have an end_time to use remaining_occurrences') unless @end_time
143
- occurrences_between(from, @end_time)
140
+ # The next n occurrences after now
141
+ def next_occurrences(num, from = Time.now)
142
+ find_occurrences(from + 1, nil, num)
144
143
  end
145
144
 
146
- # Find next scheduled occurrence
145
+ # The next occurrence after now (overridable)
147
146
  def next_occurrence(from = Time.now)
148
- next_occurrences(1, from).first
147
+ find_occurrences(from + 1, nil, 1).first
149
148
  end
150
149
 
151
- def next_occurrences(n, from = Time.now)
152
- nexts = find_occurrences { |head| head.next_occurrences(n, from) }
153
- #Grabs the first n occurrences after the from date, remembering that there is still a
154
- #possibility that recurrence dates before the from time could be in the array
155
- nexts.select{|occurrence| occurrence > from}.first(n)
150
+ # The remaining occurrences (same requirements as all_occurrences)
151
+ def remaining_occurrences(from = Time.now)
152
+ find_occurrences(from)
156
153
  end
157
154
 
158
- # Retrieve the first (n) occurrences of the schedule. May return less than
159
- # n results, if the rules end before n results are reached.
160
- def first(n = nil)
161
- dates = find_occurrences { |head| head.first(n || 1) }
162
- n.nil? ? dates.first : dates.slice(0, n)
155
+ # Occurrences between two times
156
+ def occurrences_between(begin_time, closing_time)
157
+ find_occurrences(begin_time, closing_time)
163
158
  end
164
159
 
165
- # Add a rule of any type as an recurrence in this schedule
166
- def add_recurrence_rule(rule)
167
- raise ArgumentError.new('Argument must be a valid rule') unless rule.class < Rule
168
- @rrule_occurrence_heads << RuleOccurrence.new(rule, @start_date, @end_time)
160
+ # Return a boolean indicating if an occurrence falls between
161
+ # two times
162
+ def occurs_between?(begin_time, closing_time)
163
+ !find_occurrences(begin_time, closing_time, 1).empty?
169
164
  end
170
165
 
171
- def rrules
172
- @rrule_occurrence_heads.map { |h| h.rule }
166
+ # Return a boolean indicating if an occurrence falls on a certain date
167
+ def occurs_on?(date)
168
+ begin_time = TimeUtil.beginning_of_date(date)
169
+ closing_time = TimeUtil.end_of_date(date)
170
+ occurs_between?(begin_time, closing_time)
173
171
  end
174
172
 
175
- # Add a rule of any type as an exception to this schedule
176
- def add_exception_rule(rule)
177
- raise ArgumentError.new('Argument must be a valid rule') unless rule.class < Rule
178
- @exrule_occurrence_heads << RuleOccurrence.new(rule, @start_date, @end_time)
173
+ # Determine if the schedule is occurring at a given time
174
+ def occurring_at?(time)
175
+ if duration
176
+ return false if exception_time?(time)
177
+ occurs_between?(time - duration + 1, time)
178
+ else
179
+ occurs_at?(time)
180
+ end
181
+ end
182
+
183
+ # Determine if the schedule occurs at a specific time
184
+ def occurs_at?(time)
185
+ occurs_between?(time, time)
186
+ end
187
+
188
+ # Get the first n occurrences, or the first occurrence if n is skipped
189
+ def first(n = nil)
190
+ occurrences = find_occurrences start_time, nil, n || 1
191
+ n.nil? ? occurrences.first : occurrences
192
+ end
193
+
194
+ # String serialization
195
+ def to_s
196
+ pieces = []
197
+ ed = exdates; rd = rdates - ed
198
+ pieces.concat rd.sort.map { |t| t.strftime(TO_S_TIME_FORMAT) }
199
+ pieces.concat rrules.map { |t| t.to_s }
200
+ pieces.concat exrules.map { |t| "not #{t.to_s}" }
201
+ pieces.concat ed.sort.map { |t| "not on #{t.strftime(TO_S_TIME_FORMAT)}" }
202
+ pieces << "until #{end_time.strftime(TIME_FORMAT)}" if end_time
203
+ pieces.join(' / ')
179
204
  end
180
205
 
181
- def exrules
182
- @exrule_occurrence_heads.map { |h| h.rule }
206
+ # Serialize this schedule to_ical
207
+ def to_ical(force_utc = false)
208
+ pieces = []
209
+ pieces << "DTSTART#{IcalBuilder.ical_format(start_time, force_utc)}"
210
+ pieces << "DURATION:#{IcalBuilder.ical_duration(duration)}" if duration
211
+ pieces.concat recurrence_rules.map { |r| "RRULE:#{r.to_ical}" }
212
+ pieces.concat exception_rules.map { |r| "EXRULE:#{r.to_ical}" }
213
+ pieces.concat recurrence_times.map { |t| "RDATE#{IcalBuilder.ical_format(t, force_utc)}" }
214
+ pieces.concat exception_times.map { |t| "EXDATE#{IcalBuilder.ical_format(t, force_utc)}" }
215
+ pieces << "DTEND#{IcalBuilder.ical_format(end_time, force_utc)}" if end_time
216
+ pieces.join("\n")
183
217
  end
184
218
 
185
- # Add an individual date to this schedule
186
- def add_recurrence_date(date)
187
- @rdates << date unless date.nil?
219
+ # Convert the schedule to yaml
220
+ def to_yaml(*args)
221
+ defined?(Psych) ? Psych::dump(to_hash) : YAML::dump(to_hash, *args)
188
222
  end
189
223
 
190
- # Add an individual date exception to this schedule
191
- def add_exception_date(date)
192
- @exdates << date unless date.nil?
224
+ # Load the schedule from yaml
225
+ def self.from_yaml(yaml, options = {})
226
+ from_hash defined?(Psych) ? Psych::load(yaml) : YAML::load(yaml), options
193
227
  end
194
228
 
195
- def occurrences_between(begin_time, end_time)
196
- # adjust to the propert end date
197
- end_time = @end_time if @end_time && @end_time < end_time
198
- # collect the occurrences
199
- include_dates, exclude_dates = SortedSet.new(@rdates), Set.new(@exdates)
200
- @rrule_occurrence_heads.each do |rrule_occurrence_head|
201
- include_dates.merge(rrule_occurrence_head.between(begin_time, end_time))
229
+ # Convert the schedule to a hash
230
+ def to_hash
231
+ data = {}
232
+ data[:start_date] = TimeUtil.serialize_time(start_time)
233
+ data[:end_time] = end_time if end_time
234
+ data[:duration] = duration if duration
235
+ data[:rrules] = recurrence_rules.map(&:to_hash)
236
+ data[:exrules] = exception_rules.map(&:to_hash)
237
+ data[:rdates] = recurrence_times.map do |rt|
238
+ TimeUtil.serialize_time(rt)
202
239
  end
203
- @exrule_occurrence_heads.each do |exrule_occurrence_head|
204
- exclude_dates.merge(exrule_occurrence_head.between(begin_time, end_time))
240
+ data[:exdates] = exception_times.map do |et|
241
+ TimeUtil.serialize_time(et)
205
242
  end
206
- # reject all of the ones outside of the range
207
- include_dates.reject! { |date| exclude_dates.include?(date) || date < begin_time || date > end_time }
208
- include_dates.to_a
243
+ data
209
244
  end
210
245
 
211
- alias :rdate :add_recurrence_date
212
- alias rrule add_recurrence_rule
213
- alias exdate add_exception_date
214
- alias exrule add_exception_rule
215
-
246
+ # Load the schedule from a hash
247
+ def self.from_hash(data, options = {})
248
+ data[:start_date] = options[:start_date_override] if options[:start_date_override]
249
+ # And then deserialize
250
+ schedule = IceCube::Schedule.new TimeUtil.deserialize_time(data[:start_date])
251
+ schedule.duration = data[:duration] if data[:duration]
252
+ schedule.end_time = TimeUtil.deserialize_time(data[:end_time]) if data[:end_time]
253
+ data[:rrules] && data[:rrules].each { |h| schedule.rrule(IceCube::Rule.from_hash(h)) }
254
+ data[:exrules] && data[:exrules].each { |h| schedule.exrule(IceCube::Rule.from_hash(h)) }
255
+ data[:rdates] && data[:rdates].each do |t|
256
+ schedule.add_recurrence_time TimeUtil.deserialize_time(t)
257
+ end
258
+ data[:exdates] && data[:exdates].each do |t|
259
+ schedule.add_exception_time TimeUtil.deserialize_time(t)
260
+ end
261
+ schedule
262
+ end
216
263
 
217
264
  private
218
265
 
219
- # We know that start_date is a time with zone - so check referencing
220
- # The date in that time zone
221
- def active_support_occurs_on?(date)
222
- time = Time.zone.parse(date.to_s) # date.to_time.in_time_zone(@start_date.time_zone)
223
- occurrences_between(time.beginning_of_day, time.end_of_day).any?
266
+ # Reset all rules for another run
267
+ def reset
268
+ @all_recurrence_rules.each(&:reset)
269
+ @all_exception_rules.each(&:reset)
224
270
  end
225
271
 
226
- def active_support_occurs_between(start_time, end_time)
227
- time_start = Time.zone.parse(start_time.to_s) # date.to_time.in_time_zone(@start_date.time_zone)
228
- time_end = Time.zone.parse(end_time.to_s) # date.to_time.in_time_zone(@end_date.time_zone)
229
-
230
- occurrences_between(time_start.beginning_of_day, time_end.end_of_day).any?
272
+ # Find all of the occurrences for the schedule between opening_time
273
+ # and closing_time
274
+ def find_occurrences(opening_time, closing_time = nil, limit = nil, &block)
275
+ reset
276
+ answers = []
277
+ # ensure the bounds are proper
278
+ if end_time
279
+ closing_time = end_time unless closing_time && closing_time < @end_time
280
+ end
281
+ opening_time = start_time if opening_time < start_time
282
+ # And off we go
283
+ time = opening_time
284
+ loop do
285
+ res = next_time(time, closing_time)
286
+ break unless res
287
+ break if closing_time && res > closing_time
288
+ block_given? ? block.call(res) : (answers << res)
289
+ break if limit && answers.length == limit
290
+ time = res + 1
291
+ end
292
+ # and return our answers
293
+ answers
231
294
  end
232
295
 
233
- # tell if, from a list of rule_occurrence heads, a certain time is occurring
234
- def any_occurring_at?(what, time)
235
- return false if @start_time && time < @start_time
236
- what.any? do |occurrence_head|
237
- # time is less than now and duration is less than that distance
238
- possibilities = occurrence_head.between(@start_date, time)
239
- possibilities.any? do |possibility|
240
- possibility + (@duration || 0) >= time
296
+ # Get the next time after (or including) a specific time
297
+ def next_time(time, closing_time)
298
+ min_time = nil
299
+ loop do
300
+ @all_recurrence_rules.each do |rule|
301
+ begin
302
+ if res = rule.next_time(time, self, closing_time)
303
+ if min_time.nil? || res < min_time
304
+ min_time = res
305
+ end
306
+ end
307
+ # Certain exceptions mean this rule no longer wants to play
308
+ rescue CountExceeded, UntilExceeded
309
+ next
310
+ end
241
311
  end
312
+ # If there is no match, return nil
313
+ return nil unless min_time
314
+ # Now make sure that its not an exception_time, and if it is
315
+ # then keep looking
316
+ if exception_time?(min_time)
317
+ time = min_time + 1
318
+ min_time = nil
319
+ next
320
+ end
321
+ # Break, we're done
322
+ break
242
323
  end
324
+ min_time
243
325
  end
244
326
 
245
- # Find all occurrences (following rules and exceptions) from the schedule's start date to end_date.
246
- # Use custom methods to say when to end
247
- def find_occurrences
248
- exclude_dates, include_dates = Set.new(@exdates), SortedSet.new(@rdates)
249
- # walk through each rule, adding it to dates
250
- @rrule_occurrence_heads.each do |rrule_occurrence_head|
251
- include_dates.merge(yield(rrule_occurrence_head))
252
- end
253
- # walk through each exrule, removing it from dates
254
- @exrule_occurrence_heads.each do |exrule_occurrence_head|
255
- exclude_dates.merge(yield(exrule_occurrence_head))
327
+ # Return a boolean indicating whether or not a specific time
328
+ # is excluded from the schedule
329
+ def exception_time?(time)
330
+ @all_exception_rules.any? do |rule|
331
+ rule.on?(time, self)
256
332
  end
257
- #return a unique list of dates
258
- include_dates.reject! { |date| exclude_dates.include?(date) }
259
- include_dates.to_a
260
333
  end
261
334
 
262
335
  end
@@ -0,0 +1,28 @@
1
+ module IceCube
2
+
3
+ class SingleOccurrenceRule < Rule
4
+
5
+ attr_reader :time
6
+
7
+ def initialize(time)
8
+ @time = time
9
+ end
10
+
11
+ # Always terminating
12
+ def terminating?
13
+ true
14
+ end
15
+
16
+ def next_time(t, schedule, closing_time)
17
+ unless closing_time && closing_time < t
18
+ time if time >= t
19
+ end
20
+ end
21
+
22
+ def to_hash
23
+ { :time => time }
24
+ end
25
+
26
+ end
27
+
28
+ end