Class: IceCube::MonthOfYearValidation

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

Returns a new instance of MonthOfYearValidation.



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

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

def closest(date)
  return nil if !@months_of_year || @months_of_year.empty?
  # turn months into month of year
  # month > 12 should fall into the next year
  months = @months_of_year.map do |m|
    m > date.month ? m - date.month : 12 - date.month + m
  end
  months.compact!
  # go to the closest distance away
  goal = date
  months.min.times { goal += TimeUtil.days_in_month(goal) * IceCube::ONE_DAY }
  adjust(goal, date)
end

#to_icalObject



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

def to_ical
  'BYMONTH=' << @months_of_year.join(',') unless @months_of_year.empty?
end

#to_sObject



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

def to_s
  'in ' << @months_of_year.map { |m| Date::MONTHNAMES[m] }.join(', ') unless @months_of_year.empty?
end

#validate(date) ⇒ Object



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

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