Skip to content

Commit 98a10e1

Browse files
committed
Cleanup method order: validate before builders
1 parent 2d440cb commit 98a10e1

10 files changed

Lines changed: 92 additions & 92 deletions

lib/ice_cube/validations/daily_interval.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ def type
2424
:day
2525
end
2626

27+
def validate(time, schedule)
28+
time_date = Date.new(time.year, time.month, time.day)
29+
start_date = Date.new(schedule.start_time.year, schedule.start_time.month, schedule.start_time.day)
30+
days = time_date - start_date
31+
unless days % interval === 0
32+
interval - (days % interval)
33+
end
34+
end
35+
2736
def build_s(builder)
2837
builder.base = interval == 1 ? 'Daily' : "Every #{interval} days"
2938
end
@@ -37,15 +46,6 @@ def build_ical(builder)
3746
builder['INTERVAL'] << interval unless interval == 1
3847
end
3948

40-
def validate(time, schedule)
41-
time_date = Date.new(time.year, time.month, time.day)
42-
start_date = Date.new(schedule.start_time.year, schedule.start_time.month, schedule.start_time.day)
43-
days = time_date - start_date
44-
unless days % interval === 0
45-
interval - (days % interval)
46-
end
47-
end
48-
4949
end
5050

5151
end

lib/ice_cube/validations/day_of_week.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ def type
2626
:day
2727
end
2828

29+
def validate(time, schedule)
30+
# count the days to the weekday
31+
sum = day >= time.wday ? day - time.wday : 7 - time.wday + day
32+
wrapper = TimeUtil::TimeWrapper.new(time)
33+
wrapper.add :day, sum
34+
# and then count the week until a viable occ
35+
loop do
36+
which_occ, num_occ = TimeUtil.which_occurrence_in_month(wrapper.to_time, day)
37+
this_occ = occ < 0 ? num_occ + occ + 1 : occ
38+
break if which_occ == this_occ
39+
sum += 7
40+
wrapper.add :day, 7 # one week
41+
end
42+
sum
43+
end
44+
2945
StringBuilder.register_formatter(:day_of_week) do |segments|
3046
'on the ' + segments.join(' and ')
3147
end
@@ -47,22 +63,6 @@ def build_hash(builder)
4763
arr << occ
4864
end
4965

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
64-
end
65-
6666
end
6767

6868
end

lib/ice_cube/validations/day_of_year.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def type
2525
:day
2626
end
2727

28+
def validate(time, schedule)
29+
days_in_year = TimeUtil.days_in_year(time)
30+
the_day = day < 0 ? day + days_in_year : day
31+
# compute the diff
32+
diff = the_day - time.yday
33+
diff >= 0 ? diff : diff + days_in_year
34+
end
35+
2836
StringBuilder.register_formatter(:day_of_year) do |entries|
2937
str = "on the #{StringBuilder.sentence(entries)} "
3038
str << (entries.size == 1 ? 'day of the year' : 'days of the year')
@@ -43,14 +51,6 @@ def build_ical(builder)
4351
builder['BYYEARDAY'] << day
4452
end
4553

46-
def validate(time, schedule)
47-
days_in_year = TimeUtil.days_in_year(time)
48-
the_day = day < 0 ? day + days_in_year : day
49-
# compute the diff
50-
diff = the_day - time.yday
51-
diff >= 0 ? diff : diff + days_in_year
52-
end
53-
5454
end
5555

5656
end

lib/ice_cube/validations/hourly_interval.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def dst_adjust?
2525
false
2626
end
2727

28+
def validate(time, schedule)
29+
start_time = schedule.start_time
30+
sec = (time.to_i - time.to_i % ONE_HOUR) -
31+
(start_time.to_i - start_time.to_i % ONE_HOUR)
32+
hours = sec / ONE_HOUR
33+
unless hours % interval == 0
34+
interval - (hours % interval)
35+
end
36+
end
37+
2838
def build_s(builder)
2939
builder.base = interval == 1 ? 'Hourly' : "Every #{interval} hours"
3040
end
@@ -38,16 +48,6 @@ def build_ical(builder)
3848
builder['INTERVAL'] << interval unless interval == 1
3949
end
4050

41-
def validate(time, schedule)
42-
start_time = schedule.start_time
43-
sec = (time.to_i - time.to_i % ONE_HOUR) -
44-
(start_time.to_i - start_time.to_i % ONE_HOUR)
45-
hours = sec / ONE_HOUR
46-
unless hours % interval == 0
47-
interval - (hours % interval)
48-
end
49-
end
50-
5151
end
5252

5353
end

lib/ice_cube/validations/minutely_interval.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def dst_adjust?
2525
false
2626
end
2727

28+
def validate(time, schedule)
29+
start_time = schedule.start_time
30+
sec = (time.to_i - time.to_i % ONE_MINUTE) -
31+
(start_time.to_i - start_time.to_i % ONE_MINUTE)
32+
minutes = sec / ONE_MINUTE
33+
unless minutes % interval == 0
34+
interval - (minutes % interval)
35+
end
36+
end
37+
2838
def build_s(builder)
2939
builder.base = interval == 1 ? 'Minutely' : "Every #{interval} minutes"
3040
end
@@ -38,16 +48,6 @@ def build_hash(builder)
3848
builder[:interval] = interval
3949
end
4050

41-
def validate(time, schedule)
42-
start_time = schedule.start_time
43-
sec = (time.to_i - time.to_i % ONE_MINUTE) -
44-
(start_time.to_i - start_time.to_i % ONE_MINUTE)
45-
minutes = sec / ONE_MINUTE
46-
unless minutes % interval == 0
47-
interval - (minutes % interval)
48-
end
49-
end
50-
5151
end
5252

5353
end

lib/ice_cube/validations/monthly_interval.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def type
2121
:month
2222
end
2323

24+
def validate(time, schedule)
25+
start_time = schedule.start_time
26+
months_to_start = (time.month - start_time.month) + (time.year - start_time.year) * 12
27+
unless months_to_start % interval == 0
28+
interval - (months_to_start % interval)
29+
end
30+
end
31+
2432
def build_s(builder)
2533
builder.base = interval == 1 ? 'Monthly' : "Every #{interval} months"
2634
end
@@ -34,14 +42,6 @@ def build_hash(builder)
3442
builder[:interval] = interval
3543
end
3644

37-
def validate(time, schedule)
38-
start_time = schedule.start_time
39-
months_to_start = (time.month - start_time.month) + (time.year - start_time.year) * 12
40-
unless months_to_start % interval == 0
41-
interval - (months_to_start % interval)
42-
end
43-
end
44-
4545
end
4646

4747
end

lib/ice_cube/validations/secondly_interval.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def dst_adjust?
2525
false
2626
end
2727

28+
def validate(time, schedule)
29+
seconds = time.to_i - schedule.start_time.to_i
30+
unless seconds % interval == 0
31+
interval - (seconds % interval)
32+
end
33+
end
34+
2835
def build_s(builder)
2936
builder.base = interval == 1 ? 'Secondly' : "Every #{interval} seconds"
3037
end
@@ -38,13 +45,6 @@ def build_hash(builder)
3845
builder[:interval] = interval
3946
end
4047

41-
def validate(time, schedule)
42-
seconds = time.to_i - schedule.start_time.to_i
43-
unless seconds % interval == 0
44-
interval - (seconds % interval)
45-
end
46-
end
47-
4848
end
4949

5050
end

lib/ice_cube/validations/until.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def type
2929
:dealbreaker
3030
end
3131

32+
def validate(t, schedule)
33+
raise UntilExceeded if t > time
34+
end
35+
3236
def build_ical(builder)
3337
builder['UNTIL'] << IcalBuilder.ical_utc_format(time)
3438
end
@@ -41,10 +45,6 @@ def build_s(builder)
4145
builder.piece(:until) << "until #{time.strftime(IceCube.to_s_time_format)}"
4246
end
4347

44-
def validate(t, schedule)
45-
raise UntilExceeded if t > time
46-
end
47-
4848
end
4949

5050
end

lib/ice_cube/validations/weekly_interval.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ def type
2929
:day
3030
end
3131

32+
def validate(time, schedule)
33+
date = Date.new(time.year, time.month, time.day)
34+
st = schedule.start_time
35+
start_date = Date.new(st.year, st.month, st.day)
36+
weeks = (
37+
(date - TimeUtil.normalize_wday(date.wday, week_start)) -
38+
(start_date - TimeUtil.normalize_wday(start_date.wday, week_start))
39+
) / 7
40+
unless weeks % interval == 0
41+
(interval - (weeks % interval)) * 7
42+
end
43+
end
44+
3245
def build_s(builder)
3346
builder.base = interval == 1 ? 'Weekly' : "Every #{interval} weeks"
3447
end
@@ -46,19 +59,6 @@ def build_hash(builder)
4659
builder[:week_start] = TimeUtil.sym_to_wday(week_start)
4760
end
4861

49-
def validate(time, schedule)
50-
date = Date.new(time.year, time.month, time.day)
51-
st = schedule.start_time
52-
start_date = Date.new(st.year, st.month, st.day)
53-
weeks = (
54-
(date - TimeUtil.normalize_wday(date.wday, week_start)) -
55-
(start_date - TimeUtil.normalize_wday(start_date.wday, week_start))
56-
) / 7
57-
unless weeks % interval == 0
58-
(interval - (weeks % interval)) * 7
59-
end
60-
end
61-
6262
end
6363

6464
end

lib/ice_cube/validations/yearly_interval.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def type
2020
:year
2121
end
2222

23+
def validate(time, schedule)
24+
years_to_start = time.year - schedule.start_time.year
25+
unless years_to_start % interval == 0
26+
interval - (years_to_start % interval)
27+
end
28+
end
29+
2330
def build_s(builder)
2431
builder.base = interval == 1 ? 'Yearly' : "Every #{interval} years"
2532
end
@@ -35,13 +42,6 @@ def build_ical(builder)
3542
end
3643
end
3744

38-
def validate(time, schedule)
39-
years_to_start = time.year - schedule.start_time.year
40-
unless years_to_start % interval == 0
41-
interval - (years_to_start % interval)
42-
end
43-
end
44-
4545
end
4646

4747
end

0 commit comments

Comments
 (0)