Class: IceCube::DayOfMonthValidation

Inherits:
Validation show all
Defined in:
lib/ice_cube/validations/day_of_month.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) ⇒ DayOfMonthValidation

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?
  #get some variables we need
  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] #clean way to wrap over years
  days_in_next_month = TimeUtil.days_in_month(Time.utc(next_year, next_month, 1))
  # create a list of distances
  distances = []
  @days_of_month.each do |d|
    if d > 0
      distances << d - date.mday #today is 1, we want 20 (19)
      distances << days_left_in_this_month + d #(364 + 20)
    elsif d < 0
      distances << (days_in_month + d + 1) - date.mday #today is 30, we want -1
      distances << (days_in_next_month + d + 1) + days_left_in_this_month #today is 300, we want -70
    end
  end
  #return the lowest distance
  distances = distances.select { |d| d > 0 }
  return nil if distances.empty?
  # return the start of the proper day
  goal = date + distances.min * IceCube::ONE_DAY
  adjust(goal, date)
end

#to_icalObject



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_sObject



40
41
42
# File 'lib/ice_cube/validations/day_of_month.rb', line 40

def to_s
  'on the ' << 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