Class: IceCube::DayOfMonthValidation
Constant Summary
Constants inherited
from Validation
Validation::NUMBER_SUFFIX, Validation::SPECIAL_SUFFIX
Instance Method Summary
collapse
Methods inherited from Validation
adjust, nice_numbers, sentence
Constructor Details
Returns a new instance of DayOfMonthValidation.
5
6
7
|
# File 'lib/ice_cube/validations/day_of_month.rb', line 5
def initialize(rule)
@days_of_month = rule.validations[:day_of_month]
end
|
Instance Method Details
#closest(date) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/ice_cube/validations/day_of_month.rb', line 14
def closest(date)
return nil if !@days_of_month || @days_of_month.empty?
days_in_month = TimeUtil.days_in_month(date)
days_left_in_this_month = days_in_month - date.mday
next_month, next_year = date.month == 12 ? [1, date.year + 1] : [date.month + 1, date.year] days_in_next_month = TimeUtil.days_in_month(Time.utc(next_year, next_month, 1))
distances = []
@days_of_month.each do |d|
if d > 0
distances << d - date.mday distances << days_left_in_this_month + d elsif d < 0
distances << (days_in_month + d + 1) - date.mday distances << (days_in_next_month + d + 1) + days_left_in_this_month end
end
distances = distances.select { |d| d > 0 }
return nil if distances.empty?
goal = date + distances.min * IceCube::ONE_DAY
self.class.adjust(goal, date)
end
|
#to_ical ⇒ Object
44
45
46
|
# File 'lib/ice_cube/validations/day_of_month.rb', line 44
def to_ical
'BYMONTHDAY=' << @days_of_month.join(',') unless @days_of_month.empty?
end
|
#to_s ⇒ Object
40
41
42
|
# File 'lib/ice_cube/validations/day_of_month.rb', line 40
def to_s
'on the ' << self.class.nice_numbers(@days_of_month) << (@days_of_month.size == 1 ? ' day' : ' days') << ' of the month' unless @days_of_month.empty?
end
|
#validate(date) ⇒ Object
9
10
11
12
|
# File 'lib/ice_cube/validations/day_of_month.rb', line 9
def validate(date)
return true if !@days_of_month || @days_of_month.empty?
@days_of_month.include?(date.mday) || @days_of_month.include?(date.mday - TimeUtil.days_in_month(date) - 1)
end
|