Class: IceCube::MinuteOfHourValidation

Inherits:
Validation
  • Object
show all
Defined in:
lib/ice_cube/validations/minute_of_hour.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) ⇒ MinuteOfHourValidation

Returns a new instance of MinuteOfHourValidation.



5
6
7
# File 'lib/ice_cube/validations/minute_of_hour.rb', line 5

def initialize(rule)
  @minutes_of_hour = rule.validations[:minute_of_hour]
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/minute_of_hour.rb', line 14

def closest(date)
  return nil if !@minutes_of_hour || @minutes_of_hour.empty?
  # turn minutes into minutes of hour
  # minute >= 60 should fall into the next hour
  minutes = @minutes_of_hour.map do |m|
    m > date.min ? m - date.min : 60 - date.min + m
  end
  minutes.compact!
  # go to the closest distance away, the beginning of that minute
  closest_minute = minutes.min
  goal = date + closest_minute * IceCube::ONE_MINUTE
  adjust(goal, date)
end

#to_icalObject



32
33
34
# File 'lib/ice_cube/validations/minute_of_hour.rb', line 32

def to_ical
  'BYMINUTE=' << @minutes_of_hour.join(',') unless @minutes_of_hour.empty?
end

#to_sObject



28
29
30
# File 'lib/ice_cube/validations/minute_of_hour.rb', line 28

def to_s
  'on the ' << nice_numbers(@minutes_of_hour) << (@minutes_of_hour.size == 1 ? ' minute' : ' minutes') << ' of the hour' unless @minutes_of_hour.empty?
end

#validate(date) ⇒ Object



9
10
11
12
# File 'lib/ice_cube/validations/minute_of_hour.rb', line 9

def validate(date)
  return true if !@minutes_of_hour || @minutes_of_hour.empty?
  @minutes_of_hour.include?(date.min)
end