Class: IceCube::HourOfDayValidation
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 HourOfDayValidation.
5
6
7
|
# File 'lib/ice_cube/validations/hour_of_day.rb', line 5
def initialize(rule)
@hours_of_day = rule.validations[:hour_of_day]
end
|
Instance Method Details
#closest(date) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/ice_cube/validations/hour_of_day.rb', line 14
def closest(date)
return nil if !@hours_of_day || @hours_of_day.empty?
hours = @hours_of_day.map do |h|
h > date.hour ? h - date.hour : 24 - date.hour + h
end
hours.compact!
closest_hour = hours.min
goal = date + IceCube::ONE_HOUR * closest_hour
adjust(goal, date)
end
|
#to_ical ⇒ Object
32
33
34
|
# File 'lib/ice_cube/validations/hour_of_day.rb', line 32
def to_ical
'BYHOUR=' << @hours_of_day.join(',') unless @hours_of_day.empty?
end
|
#to_s ⇒ Object
28
29
30
|
# File 'lib/ice_cube/validations/hour_of_day.rb', line 28
def to_s
'on the ' << nice_numbers(@hours_of_day) << (@hours_of_day.size == 1 ? ' hour' : ' hours') << ' of the day' unless @hours_of_day.empty?
end
|
#validate(date) ⇒ Object
9
10
11
12
|
# File 'lib/ice_cube/validations/hour_of_day.rb', line 9
def validate(date)
return true if !@hours_of_day || @hours_of_day.empty?
@hours_of_day.include?(date.hour)
end
|