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.
- data/lib/ice_cube/builders/hash_builder.rb +27 -0
- data/lib/ice_cube/builders/ical_builder.rb +59 -0
- data/lib/ice_cube/builders/string_builder.rb +74 -0
- data/lib/ice_cube/errors/count_exceeded.rb +7 -0
- data/lib/ice_cube/errors/until_exceeded.rb +7 -0
- data/lib/ice_cube/rule.rb +86 -140
- data/lib/ice_cube/rules/daily_rule.rb +5 -27
- data/lib/ice_cube/rules/hourly_rule.rb +6 -26
- data/lib/ice_cube/rules/minutely_rule.rb +5 -25
- data/lib/ice_cube/rules/monthly_rule.rb +6 -30
- data/lib/ice_cube/rules/secondly_rule.rb +5 -26
- data/lib/ice_cube/rules/weekly_rule.rb +5 -36
- data/lib/ice_cube/rules/yearly_rule.rb +8 -34
- data/lib/ice_cube/schedule.rb +262 -189
- data/lib/ice_cube/single_occurrence_rule.rb +28 -0
- data/lib/ice_cube/time_util.rb +202 -76
- data/lib/ice_cube/validated_rule.rb +107 -0
- data/lib/ice_cube/validations/count.rb +56 -0
- data/lib/ice_cube/validations/daily_interval.rb +51 -0
- data/lib/ice_cube/validations/day.rb +45 -29
- data/lib/ice_cube/validations/day_of_month.rb +45 -43
- data/lib/ice_cube/validations/day_of_week.rb +59 -41
- data/lib/ice_cube/validations/day_of_year.rb +49 -43
- data/lib/ice_cube/validations/hour_of_day.rb +43 -29
- data/lib/ice_cube/validations/hourly_interval.rb +50 -0
- data/lib/ice_cube/validations/lock.rb +47 -0
- data/lib/ice_cube/validations/minute_of_hour.rb +43 -30
- data/lib/ice_cube/validations/minutely_interval.rb +50 -0
- data/lib/ice_cube/validations/month_of_year.rb +40 -29
- data/lib/ice_cube/validations/monthly_interval.rb +47 -0
- data/lib/ice_cube/validations/schedule_lock.rb +41 -0
- data/lib/ice_cube/validations/second_of_minute.rb +40 -29
- data/lib/ice_cube/validations/secondly_interval.rb +50 -0
- data/lib/ice_cube/validations/until.rb +49 -0
- data/lib/ice_cube/validations/weekly_interval.rb +50 -0
- data/lib/ice_cube/validations/yearly_interval.rb +45 -0
- data/lib/ice_cube/version.rb +2 -2
- data/lib/ice_cube.rb +63 -37
- data/spec/spec_helper.rb +13 -0
- metadata +47 -8
- data/lib/ice_cube/rule_occurrence.rb +0 -100
- data/lib/ice_cube/validation.rb +0 -44
- data/lib/ice_cube/validation_types.rb +0 -137
|
@@ -1,40 +1,14 @@
|
|
|
1
1
|
module IceCube
|
|
2
2
|
|
|
3
|
-
class YearlyRule <
|
|
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
|
-
|
|
35
|
-
|
|
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
|
data/lib/ice_cube/schedule.rb
CHANGED
|
@@ -1,262 +1,335 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
1
3
|
module IceCube
|
|
2
4
|
|
|
3
5
|
class Schedule
|
|
4
6
|
|
|
5
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
def initialize(
|
|
11
|
-
@
|
|
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
|
-
#
|
|
23
|
-
def
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
#
|
|
60
|
-
def
|
|
61
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
#
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
#
|
|
102
|
-
def
|
|
103
|
-
|
|
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
|
-
#
|
|
109
|
-
def
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
#
|
|
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
|
-
|
|
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
|
-
#
|
|
135
|
-
def
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
# Iterate forever
|
|
135
|
+
def each_occurrence(&block)
|
|
136
|
+
find_occurrences(start_time, &block)
|
|
137
|
+
self
|
|
138
138
|
end
|
|
139
139
|
|
|
140
|
-
#
|
|
141
|
-
def
|
|
142
|
-
|
|
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
|
-
#
|
|
145
|
+
# The next occurrence after now (overridable)
|
|
147
146
|
def next_occurrence(from = Time.now)
|
|
148
|
-
|
|
147
|
+
find_occurrences(from + 1, nil, 1).first
|
|
149
148
|
end
|
|
150
149
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
#
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
#
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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
|
-
|
|
172
|
-
|
|
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
|
-
#
|
|
176
|
-
def
|
|
177
|
-
|
|
178
|
-
|
|
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
|
-
|
|
182
|
-
|
|
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
|
-
#
|
|
186
|
-
def
|
|
187
|
-
|
|
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
|
-
#
|
|
191
|
-
def
|
|
192
|
-
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
204
|
-
|
|
240
|
+
data[:exdates] = exception_times.map do |et|
|
|
241
|
+
TimeUtil.serialize_time(et)
|
|
205
242
|
end
|
|
206
|
-
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
-
#
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
#
|
|
234
|
-
def
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
#
|
|
246
|
-
#
|
|
247
|
-
def
|
|
248
|
-
|
|
249
|
-
|
|
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
|