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,52 +1,70 @@
|
|
|
1
1
|
module IceCube
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module Validations::DayOfWeek
|
|
4
4
|
|
|
5
|
-
def
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
# does this fall on one of the occurrences?
|
|
15
|
-
first_occurrence = ((7 - Time.utc(date.year, date.month, 1).wday) + date.wday) % 7 + 1 #day of first occurrence of a wday in a month
|
|
16
|
-
this_weekday_in_month_count = ((TimeUtil.days_in_month(date) - first_occurrence + 1) / 7.0).ceil #how many of these in the month
|
|
17
|
-
nth_occurrence_of_weekday = (date.mday - first_occurrence) / 7 + 1 #what occurrence of the weekday is +date+
|
|
18
|
-
@days_of_week[date.wday].include?(nth_occurrence_of_weekday) || @days_of_week[date.wday].include?(nth_occurrence_of_weekday - this_weekday_in_month_count - 1)
|
|
5
|
+
def day_of_week(dows)
|
|
6
|
+
dows.each do |day, occs|
|
|
7
|
+
occs.each do |occ|
|
|
8
|
+
day = TimeUtil.symbol_to_day(day) if day.is_a?(Symbol)
|
|
9
|
+
validations_for(:day_of_week) << Validation.new(day, occ)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
clobber_base_validations :day, :wday
|
|
13
|
+
self
|
|
19
14
|
end
|
|
20
|
-
|
|
21
15
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
class Validation
|
|
17
|
+
|
|
18
|
+
attr_reader :day, :occ
|
|
19
|
+
|
|
20
|
+
StringBuilder.register_formatter(:day_of_week) do |segments|
|
|
21
|
+
'on the ' + segments.join(' when it is the ')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def type
|
|
25
|
+
:day
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def build_s(builder)
|
|
29
|
+
builder.piece(:day_of_week) << "#{StringBuilder.nice_number(occ)} #{Date::DAYNAMES[day]}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def build_ical(builder)
|
|
33
|
+
ical_day = IcalBuilder.fixnum_to_ical_day(day)
|
|
34
|
+
# Delete any with this day and no occ first
|
|
35
|
+
builder['BYDAY'].delete_if { |d| d == ical_day }
|
|
36
|
+
builder['BYDAY'] << "#{occ}#{ical_day}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def build_hash(builder)
|
|
40
|
+
builder.validations[:day_of_week] ||= {}
|
|
41
|
+
arr = (builder.validations[:day_of_week][day] ||= [])
|
|
42
|
+
arr << occ
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def initialize(day, occ)
|
|
46
|
+
@day = day
|
|
47
|
+
@occ = occ
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def validate(time, schedule)
|
|
51
|
+
# count the days to the weekday
|
|
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
|
|
30
64
|
end
|
|
31
|
-
end
|
|
32
65
|
|
|
33
|
-
def to_s
|
|
34
|
-
representation = ''
|
|
35
|
-
representation << 'on the '
|
|
36
|
-
representation << @days_of_week.map do |day, occ|
|
|
37
|
-
self.class.nice_numbers(occ) << ' ' << Date::DAYNAMES[day] << (occ.size != 1 ? 's' : '') unless @days_of_week.empty?
|
|
38
|
-
end.join(' when it is the ')
|
|
39
|
-
representation
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def to_ical
|
|
43
|
-
representation = 'BYDAY='
|
|
44
|
-
representation << @days_of_week.map do |day, occ|
|
|
45
|
-
occ.map { |o| o.to_s + IceCube::ICAL_DAYS[day] }.join(',')
|
|
46
|
-
end.join(',')
|
|
47
|
-
representation
|
|
48
66
|
end
|
|
49
67
|
|
|
50
68
|
end
|
|
51
|
-
|
|
69
|
+
|
|
52
70
|
end
|
|
@@ -1,49 +1,55 @@
|
|
|
1
1
|
module IceCube
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@days_of_year.include?(date.yday) || @days_of_year.include?(date.yday - TimeUtil.days_in_year(date) - 1)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def closest(date)
|
|
15
|
-
return nil if !@days_of_year || @days_of_year.empty?
|
|
16
|
-
#get some variables we need
|
|
17
|
-
days_in_year = TimeUtil.days_in_year(date)
|
|
18
|
-
days_left_in_this_year = days_in_year - date.yday
|
|
19
|
-
days_in_next_year = TimeUtil.days_in_year(Time.utc(date.year + 1, 1, 1))
|
|
20
|
-
# create a list of distances
|
|
21
|
-
distances = []
|
|
22
|
-
@days_of_year.each do |d|
|
|
23
|
-
if d > 0
|
|
24
|
-
distances << d - date.yday #today is 1, we want 20 (19)
|
|
25
|
-
distances << days_left_in_this_year + d #(364 + 20)
|
|
26
|
-
elsif d < 0
|
|
27
|
-
distances << (days_in_year + d + 1) - date.yday #today is 300, we want -1
|
|
28
|
-
distances << (days_in_next_year + d + 1) + days_left_in_this_year #today is 300, we want -70
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
#return the lowest distance
|
|
32
|
-
distances = distances.select { |d| d > 0 }
|
|
33
|
-
return nil if distances.empty?
|
|
34
|
-
# return the start of the proper day
|
|
35
|
-
goal = date + distances.min * IceCube::ONE_DAY
|
|
36
|
-
self.class.adjust(goal, date)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def to_s
|
|
40
|
-
'on the ' << self.class.nice_numbers(@days_of_year) << (@days_of_year.size == 1 ? ' day' : ' days') << ' of the year' unless @days_of_year.empty?
|
|
2
|
+
|
|
3
|
+
module Validations::DayOfYear
|
|
4
|
+
|
|
5
|
+
def day_of_year(*days)
|
|
6
|
+
days.each do |day|
|
|
7
|
+
validations_for(:day_of_year) << Validation.new(day)
|
|
8
|
+
end
|
|
9
|
+
clobber_base_validations(:month, :day, :wday)
|
|
10
|
+
self
|
|
41
11
|
end
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
12
|
+
|
|
13
|
+
class Validation
|
|
14
|
+
|
|
15
|
+
attr_reader :day
|
|
16
|
+
|
|
17
|
+
StringBuilder.register_formatter(:day_of_year) do |entries|
|
|
18
|
+
str = "on the #{StringBuilder.sentence(entries)} "
|
|
19
|
+
str << (entries.size == 1 ? 'day of the year' : 'days of the year')
|
|
20
|
+
str
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def initialize(day)
|
|
24
|
+
@day = day
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def type
|
|
28
|
+
:day
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def build_s(builder)
|
|
32
|
+
builder.piece(:day_of_year) << StringBuilder.nice_number(day)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def build_hash(builder)
|
|
36
|
+
builder.validations_array(:day_of_year) << day
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def build_ical(builder)
|
|
40
|
+
builder['BYYEARDAY'] << day
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def validate(time, schedule)
|
|
44
|
+
days_in_year = TimeUtil.days_in_year(time)
|
|
45
|
+
the_day = day < 0 ? day + days_in_year : day
|
|
46
|
+
# compute the diff
|
|
47
|
+
diff = the_day - time.yday
|
|
48
|
+
diff >= 0 ? diff : diff + days_in_year
|
|
49
|
+
end
|
|
50
|
+
|
|
45
51
|
end
|
|
46
|
-
|
|
52
|
+
|
|
47
53
|
end
|
|
48
54
|
|
|
49
55
|
end
|
|
@@ -1,38 +1,52 @@
|
|
|
1
1
|
module IceCube
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@hours_of_day.include?(date.hour)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def closest(date)
|
|
15
|
-
return nil if !@hours_of_day || @hours_of_day.empty?
|
|
16
|
-
# turn hours into hour of day
|
|
17
|
-
# hour >= 24 should fall into the next day
|
|
18
|
-
hours = @hours_of_day.map do |h|
|
|
19
|
-
h > date.hour ? h - date.hour : 24 - date.hour + h
|
|
3
|
+
module Validations::HourOfDay
|
|
4
|
+
|
|
5
|
+
include Validations::Lock
|
|
6
|
+
|
|
7
|
+
# Add hour of day validations
|
|
8
|
+
def hour_of_day(*hours)
|
|
9
|
+
hours.each do |hour|
|
|
10
|
+
validations_for(:hour_of_day) << Validation.new(hour)
|
|
20
11
|
end
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
closest_hour = hours.min
|
|
24
|
-
goal = date + IceCube::ONE_HOUR * closest_hour
|
|
25
|
-
self.class.adjust(goal, date)
|
|
12
|
+
clobber_base_validations(:hour)
|
|
13
|
+
self
|
|
26
14
|
end
|
|
27
15
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
class Validation
|
|
17
|
+
|
|
18
|
+
include Validations::Lock
|
|
19
|
+
|
|
20
|
+
StringBuilder.register_formatter(:hour_of_day) do |segments|
|
|
21
|
+
str = "on the #{StringBuilder.sentence(segments)} "
|
|
22
|
+
str << (segments.size == 1 ? 'hour of the day' : 'hours of the day')
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
attr_reader :hour
|
|
26
|
+
alias :value :hour
|
|
27
|
+
|
|
28
|
+
def initialize(hour)
|
|
29
|
+
@hour = hour
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def build_s(builder)
|
|
33
|
+
builder.piece(:hour_of_day) << StringBuilder.nice_number(hour)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def type
|
|
37
|
+
:hour
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def build_hash(builder)
|
|
41
|
+
builder.validations_array(:hour_of_day) << hour
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def build_ical(builder)
|
|
45
|
+
builder['BYHOUR'] << hour
|
|
46
|
+
end
|
|
31
47
|
|
|
32
|
-
def to_ical
|
|
33
|
-
'BYHOUR=' << @hours_of_day.join(',') unless @hours_of_day.empty?
|
|
34
48
|
end
|
|
35
|
-
|
|
49
|
+
|
|
36
50
|
end
|
|
37
|
-
|
|
51
|
+
|
|
38
52
|
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module IceCube
|
|
2
|
+
|
|
3
|
+
module Validations::HourlyInterval
|
|
4
|
+
|
|
5
|
+
def interval(interval)
|
|
6
|
+
validations_for(:interval) << Validation.new(interval)
|
|
7
|
+
clobber_base_validations(:hour)
|
|
8
|
+
self
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Validation
|
|
12
|
+
|
|
13
|
+
attr_reader :interval
|
|
14
|
+
|
|
15
|
+
def type
|
|
16
|
+
:hour
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def build_s(builder)
|
|
20
|
+
builder.base = interval == 1 ? 'Hourly' : "Every #{interval} hours"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def build_hash(builder)
|
|
24
|
+
builder.validations[:interval] = interval
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def build_ical(builder)
|
|
28
|
+
builder['FREQ'] << 'HOURLY'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def initialize(interval)
|
|
32
|
+
@interval = interval
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def dst_adjust?
|
|
36
|
+
false
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def validate(time, schedule)
|
|
40
|
+
hours = (time.to_i - schedule.start_time.to_i) / IceCube::ONE_HOUR
|
|
41
|
+
unless hours % interval == 0
|
|
42
|
+
interval - (hours % interval)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module IceCube
|
|
2
|
+
|
|
3
|
+
# A validation mixin that will lock the +type field to
|
|
4
|
+
# +value or +schedule.start_time.send(type) if value is nil
|
|
5
|
+
|
|
6
|
+
module Validations::Lock
|
|
7
|
+
|
|
8
|
+
INTERVALS = { :hour => 24, :min => 60, :sec => 60, :month => 12, :wday => 7 }
|
|
9
|
+
def validate(time, schedule)
|
|
10
|
+
return send(:"validate_#{type}_lock", time, schedule) unless INTERVALS[type]
|
|
11
|
+
start = value || schedule.start_time.send(type)
|
|
12
|
+
start = INTERVALS[type] + start if start < 0 # handle negative values
|
|
13
|
+
start >= time.send(type) ? start - time.send(type) : INTERVALS[type] - time.send(type) + start
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
# Needs to be custom since we don't know the days in the month
|
|
19
|
+
# (meaning, its not a fixed interval)
|
|
20
|
+
def validate_day_lock(time, schedule)
|
|
21
|
+
start = value || schedule.start_time.day
|
|
22
|
+
days_in_this_month = TimeUtil.days_in_month(time)
|
|
23
|
+
# If this number is positive, then follow our normal procedure
|
|
24
|
+
if start > 0
|
|
25
|
+
return start >= time.day ? start - time.day : days_in_this_month - time.day + start
|
|
26
|
+
end
|
|
27
|
+
# If the number is negative, and it resolved against the current month
|
|
28
|
+
# puts it in the future, just return the difference
|
|
29
|
+
days_in_this_month = TimeUtil.days_in_month(time)
|
|
30
|
+
start_one = days_in_this_month + start + 1
|
|
31
|
+
if start_one >= time.day
|
|
32
|
+
return start_one - time.day
|
|
33
|
+
end
|
|
34
|
+
# Otherwise, we need to figure out the meaning of the value
|
|
35
|
+
# in the next month, and then figure out how to get there
|
|
36
|
+
days_in_next_month = TimeUtil.days_in_next_month(time)
|
|
37
|
+
start_two = days_in_next_month + start + 1
|
|
38
|
+
if start_two >= time.day
|
|
39
|
+
days_in_this_month + start_two - time.day
|
|
40
|
+
else
|
|
41
|
+
days_in_next_month + start_two - time.day
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
@@ -1,38 +1,51 @@
|
|
|
1
1
|
module IceCube
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return true if !@minutes_of_hour || @minutes_of_hour.empty?
|
|
11
|
-
@minutes_of_hour.include?(date.min)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def closest(date)
|
|
15
|
-
return nil if !@minutes_of_hour || @minutes_of_hour.empty?
|
|
16
|
-
# turn minutes into minutes of hour
|
|
17
|
-
# minute >= 60 should fall into the next hour
|
|
18
|
-
minutes = @minutes_of_hour.map do |m|
|
|
19
|
-
m > date.min ? m - date.min : 60 - date.min + m
|
|
3
|
+
module Validations::MinuteOfHour
|
|
4
|
+
|
|
5
|
+
include Validations::Lock
|
|
6
|
+
|
|
7
|
+
def minute_of_hour(*minutes)
|
|
8
|
+
minutes.each do |minute|
|
|
9
|
+
validations_for(:minute_of_hour) << Validation.new(minute)
|
|
20
10
|
end
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
closest_minute = minutes.min
|
|
24
|
-
goal = date + closest_minute * IceCube::ONE_MINUTE
|
|
25
|
-
self.class.adjust(goal, date)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def to_s
|
|
29
|
-
'on the ' << self.class.nice_numbers(@minutes_of_hour) << (@minutes_of_hour.size == 1 ? ' minute' : ' minutes') << ' of the hour' unless @minutes_of_hour.empty?
|
|
11
|
+
clobber_base_validations(:min)
|
|
12
|
+
self
|
|
30
13
|
end
|
|
31
14
|
|
|
32
|
-
|
|
33
|
-
|
|
15
|
+
class Validation
|
|
16
|
+
|
|
17
|
+
include Validations::Lock
|
|
18
|
+
|
|
19
|
+
StringBuilder.register_formatter(:minute_of_hour) do |segments|
|
|
20
|
+
str = "on the #{StringBuilder.sentence(segments)} "
|
|
21
|
+
str << (segments.size == 1 ? 'minute of the hour' : 'minutes of the hour')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
attr_reader :minute
|
|
25
|
+
alias :value :minute
|
|
26
|
+
|
|
27
|
+
def initialize(minute)
|
|
28
|
+
@minute = minute
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def build_s(builder)
|
|
32
|
+
builder.piece(:minute_of_hour) << StringBuilder.nice_number(minute)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def type
|
|
36
|
+
:min
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def build_hash(builder)
|
|
40
|
+
builder.validations_array(:minute_of_hour) << minute
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def build_ical(builder)
|
|
44
|
+
builder['BYMINUTE'] << minute
|
|
45
|
+
end
|
|
46
|
+
|
|
34
47
|
end
|
|
35
|
-
|
|
48
|
+
|
|
36
49
|
end
|
|
37
|
-
|
|
50
|
+
|
|
38
51
|
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module IceCube
|
|
2
|
+
|
|
3
|
+
module Validations::MinutelyInterval
|
|
4
|
+
|
|
5
|
+
def interval(interval)
|
|
6
|
+
validations_for(:interval) << Validation.new(interval)
|
|
7
|
+
clobber_base_validations(:min)
|
|
8
|
+
self
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Validation
|
|
12
|
+
|
|
13
|
+
attr_reader :interval
|
|
14
|
+
|
|
15
|
+
def type
|
|
16
|
+
:min
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def dst_adjust?
|
|
20
|
+
false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def build_s(builder)
|
|
24
|
+
builder.base = interval == 1 ? 'Minutely' : "Every #{interval} minutes"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def build_ical(builder)
|
|
28
|
+
builder['FREQ'] << 'MINUTELY'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def build_hash(builder)
|
|
32
|
+
builder.validations[:interval] = interval
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def initialize(interval)
|
|
36
|
+
@interval = interval
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def validate(time, schedule)
|
|
40
|
+
minutes = (time.to_i - schedule.start_time.to_i) / IceCube::ONE_MINUTE
|
|
41
|
+
unless minutes % interval == 0
|
|
42
|
+
interval - (minutes % interval)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
@@ -1,38 +1,49 @@
|
|
|
1
1
|
module IceCube
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def validate(date)
|
|
10
|
-
return true if !@months_of_year || @months_of_year.empty?
|
|
11
|
-
@months_of_year.include?(date.month)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def closest(date)
|
|
15
|
-
return nil if !@months_of_year || @months_of_year.empty?
|
|
16
|
-
# turn months into month of year
|
|
17
|
-
# month > 12 should fall into the next year
|
|
18
|
-
months = @months_of_year.map do |m|
|
|
19
|
-
m > date.month ? m - date.month : 12 - date.month + m
|
|
3
|
+
module Validations::MonthOfYear
|
|
4
|
+
|
|
5
|
+
def month_of_year(*months)
|
|
6
|
+
months.each do |month|
|
|
7
|
+
month = TimeUtil.symbol_to_month(month) if month.is_a?(Symbol)
|
|
8
|
+
validations_for(:month_of_year) << Validation.new(month)
|
|
20
9
|
end
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
goal = date
|
|
24
|
-
months.min.times { goal += TimeUtil.days_in_month(goal) * IceCube::ONE_DAY }
|
|
25
|
-
self.class.adjust(goal, date)
|
|
10
|
+
clobber_base_validations :month
|
|
11
|
+
self
|
|
26
12
|
end
|
|
27
13
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
class Validation
|
|
15
|
+
|
|
16
|
+
include Validations::Lock
|
|
17
|
+
|
|
18
|
+
attr_reader :month
|
|
19
|
+
alias :value :month
|
|
20
|
+
|
|
21
|
+
def initialize(month)
|
|
22
|
+
@month = month
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def build_s(builder)
|
|
26
|
+
builder.piece(:month_of_year) << Date::MONTHNAMES[month]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def build_hash(builder)
|
|
30
|
+
builder.validations_array(:month_of_year) << month
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def build_ical(builder)
|
|
34
|
+
builder['BYMONTH'] << month
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def type
|
|
38
|
+
:month
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
StringBuilder.register_formatter(:month_of_year) do |segments|
|
|
42
|
+
"in #{StringBuilder.sentence(segments)}"
|
|
43
|
+
end
|
|
31
44
|
|
|
32
|
-
def to_ical
|
|
33
|
-
'BYMONTH=' << @months_of_year.join(',') unless @months_of_year.empty?
|
|
34
45
|
end
|
|
35
|
-
|
|
46
|
+
|
|
36
47
|
end
|
|
37
|
-
|
|
48
|
+
|
|
38
49
|
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module IceCube
|
|
2
|
+
|
|
3
|
+
module Validations::MonthlyInterval
|
|
4
|
+
|
|
5
|
+
def interval(interval = 1)
|
|
6
|
+
validations_for(:interval) << Validation.new(interval)
|
|
7
|
+
clobber_base_validations(:month)
|
|
8
|
+
self
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Validation
|
|
12
|
+
|
|
13
|
+
attr_reader :interval
|
|
14
|
+
|
|
15
|
+
def type
|
|
16
|
+
:month
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def build_s(builder)
|
|
20
|
+
builder.base = interval == 1 ? 'Monthly' : "Every #{interval} months"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def build_ical(builder)
|
|
24
|
+
builder['FREQ'] << 'MONTHLY'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def build_hash(builder)
|
|
28
|
+
builder.validations[:interval] = interval
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def initialize(interval)
|
|
32
|
+
@interval = interval
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def validate(time, schedule)
|
|
36
|
+
start_time = schedule.start_time
|
|
37
|
+
months_to_start = (time.month - start_time.month) + (time.year - start_time.year) * 12
|
|
38
|
+
unless months_to_start % interval == 0
|
|
39
|
+
interval - (months_to_start % interval)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module IceCube
|
|
2
|
+
|
|
3
|
+
module Validations::ScheduleLock
|
|
4
|
+
|
|
5
|
+
# Lock the given times down the schedule's start_time for that position
|
|
6
|
+
# These locks are all clobberable by other rules of the same #type
|
|
7
|
+
# using clobber_base_validation
|
|
8
|
+
def schedule_lock(*types)
|
|
9
|
+
types.each do |type|
|
|
10
|
+
validations_for(:"base_#{type}") << Validation.new(type)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# A validation used for locking time into a certain value
|
|
15
|
+
class Validation
|
|
16
|
+
|
|
17
|
+
include Validations::Lock
|
|
18
|
+
|
|
19
|
+
attr_reader :type, :value
|
|
20
|
+
|
|
21
|
+
def initialize(type)
|
|
22
|
+
@type = type
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# no -op
|
|
26
|
+
def build_s(builder)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# no -op
|
|
30
|
+
def build_ical(builder)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# no -op
|
|
34
|
+
def build_hash(builder)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|