Class: IceCube::HourOfDayValidation

Inherits:
Validation show all
Defined in:
lib/ice_cube/validations/hour_of_day.rb

Constant Summary

Constants inherited from Validation

Validation::NUMBER_SUFFIX

Instance Method Summary collapse

Methods inherited from Validation

#adjust, #nice_numbers, #sentence

Constructor Details

#initialize(rule) ⇒ HourOfDayValidation

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?
  # turn hours into hour of day
  # hour >= 24 should fall into the next day
  hours = @hours_of_day.map do |h|
    h > date.hour ? h - date.hour : 24 - date.hour + h
  end
  hours.compact!
  # go to the closest distance away, the start of that hour
  closest_hour = hours.min
  goal = date + IceCube::ONE_HOUR * closest_hour
  adjust(goal, date)
end

#to_icalObject



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_sObject



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