Module: MonthOfYearValidation

Included in:
IceCube::Rule
Defined in:
lib/ice_cube/validations/month_of_year.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/ice_cube/validations/month_of_year.rb', line 3

def self.included(base)
  base::SuggestionTypes << :month_of_year
end

Instance Method Details

#closest_month_of_year(date) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ice_cube/validations/month_of_year.rb', line 25

def closest_month_of_year(date)
  return nil if !@validations[:month_of_year] || @validations[:month_of_year].empty?
  # turn months into month of year
  # month > 12 should fall into the next year
  months = @validations[:month_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

#month_of_year(*months) ⇒ Object

Specify what months of the year this rule applies to.

ie: Schedule.yearly(2).month_of_year(:january, :march) would create a rule which occurs every january and march, every other year Note: you cannot combine day_of_year and month_of_year in the same rule.



11
12
13
14
15
16
17
18
# File 'lib/ice_cube/validations/month_of_year.rb', line 11

def month_of_year(*months)
  @validations[:month_of_year] ||= []
  months.each do |month|
    raise ArgumentError.new('Argument must be a valid month') unless IceCube::MONTHS.has_key?(month)
    @validations[:month_of_year] << IceCube::MONTHS[month]
  end
  self
end

#validate_month_of_year(date) ⇒ Object



20
21
22
23
# File 'lib/ice_cube/validations/month_of_year.rb', line 20

def validate_month_of_year(date)
  return true if !@validations[:month_of_year] || @validations[:month_of_year].empty?
  @validations[:month_of_year].include?(date.month)
end