Class: IceCube::DayOfYearValidation
Constant Summary
Constants inherited
from Validation
Validation::NUMBER_SUFFIX
Instance Method Summary
collapse
Methods inherited from Validation
#adjust, #nice_numbers, #sentence
Constructor Details
Returns a new instance of DayOfYearValidation.
5
6
7
|
# File 'lib/ice_cube/validations/day_of_year.rb', line 5
def initialize(rule)
@days_of_year = rule.validations[:day_of_year]
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
|
# File 'lib/ice_cube/validations/day_of_year.rb', line 14
def closest(date)
return nil if !@days_of_year || @days_of_year.empty?
days_in_year = TimeUtil.days_in_year(date)
days_left_in_this_year = days_in_year - date.yday
days_in_next_year = TimeUtil.days_in_year(Time.utc(date.year + 1, 1, 1))
distances = []
@days_of_year.each do |d|
if d > 0
distances << d - date.yday distances << days_left_in_this_year + d elsif d < 0
distances << (days_in_year + d + 1) - date.yday distances << (days_in_next_year + d + 1) + days_left_in_this_year end
end
distances = distances.select { |d| d > 0 }
return nil if distances.empty?
goal = date + distances.min * IceCube::ONE_DAY
adjust(goal, date)
end
|
#to_ical ⇒ Object
43
44
45
|
# File 'lib/ice_cube/validations/day_of_year.rb', line 43
def to_ical
'BYYEARDAY=' << @days_of_year.join(',') unless @days_of_year.empty?
end
|
#to_s ⇒ Object
39
40
41
|
# File 'lib/ice_cube/validations/day_of_year.rb', line 39
def to_s
'on the ' << nice_numbers(@days_of_year) << (@days_of_year.size == 1 ? ' day' : ' days') << ' of the year' unless @days_of_year.empty?
end
|
#validate(date) ⇒ Object
9
10
11
12
|
# File 'lib/ice_cube/validations/day_of_year.rb', line 9
def validate(date)
return true if !@days_of_year || @days_of_year.empty?
@days_of_year.include?(date.yday) || @days_of_year.include?(date.yday - TimeUtil.days_in_year(date) - 1)
end
|