Module: ValidationTypes

Included in:
IceCube::Rule
Defined in:
lib/ice_cube/validation_types.rb

Instance Method Summary collapse

Instance Method Details

#day(*days) ⇒ Object

Specify what days of the week this rule should occur on. ie: Schedule.weekly.day_of_week(:monday) would create a rule that occurs every monday.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ice_cube/validation_types.rb', line 18

def day(*days)
  @validations[:day] ||= []
  @validation_types[:day] ||= IceCube::DayValidation.new(self)
  days.each do |day|
    if day.is_a?(Integer)
      # integer type argument
      raise ArgumentError.new('Argument must be a valid day of week (0-6)') unless day >= 0 && day <= 6
      @validations[:day] << day
    else
      # symbol type argument
      raise ArgumentError.new('Argument must be a valid day of the week') unless IceCube::DAYS.has_key?(day)
      @validations[:day] << IceCube::DAYS[day]
    end
  end
  # enforce uniqueness
  @validations[:day].uniq!
  self
end

#day_of_month(*days) ⇒ Object

Specify the days of the month that this rule should occur on. ie: rule.day_of_month(1, -1) would mean that this rule should occur on the first and last day of every month.



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ice_cube/validation_types.rb', line 112

def day_of_month(*days)
  @validations[:day_of_month] ||= []
  @validation_types[:day_of_month] ||= IceCube::DayOfMonthValidation.new(self)
  days.each do |day|
    raise ArgumentError.new('Argument must be a valid date') if day.abs > 31 
    raise ArgumentError.new('Argument must be non-zero') if day == 0
    @validations[:day_of_month] << day
  end
  # enforce uniqueness
  @validations[:day_of_month].uniq!
  self
end

#day_of_week(days) ⇒ Object

Specify the day(s) of the week that this rule should occur on. ie: rule.day_of_week(:monday => [1, -1]) would mean that this rule should occur on the first and last mondays of each month.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ice_cube/validation_types.rb', line 80

def day_of_week(days)
  @validations[:day_of_week] ||= {}
  @validation_types[:day_of_week] ||= IceCube::DayOfWeekValidation.new(self)
  days.each do |day, occurrences|
    unless day.is_a?(Integer)
      raise ArgumentError.new('Argument must be a valid day of week') unless IceCube::DAYS.has_key?(day)
      day = IceCube::DAYS[day]
    end
    raise ArgumentError.new('Argument must be a valid day of week (0-6)') unless day >= 0 && day <= 6
    # add the day
    @validations[:day_of_week][day] ||= []
    @validations[:day_of_week][day].concat(occurrences)
    @validations[:day_of_week][day].uniq!
  end
  self
end

#day_of_year(*days) ⇒ Object

Specify what days of the year this rule applies to. ie: Schedule.yearly(2).days_of_year(17, -1) would create a rule which occurs every 17th and last day of every other year. Note: you cannot combine month_of_year and day_of_year in the same rule.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ice_cube/validation_types.rb', line 41

def day_of_year(*days)
  @validations[:day_of_year] ||= []
  @validation_types[:day_of_year] ||= IceCube::DayOfYearValidation.new(self)
  days.each do |day|
    raise ArgumentError.new('Argument must be a valid day') if day.abs > 366
    raise ArgumentError.new('Argument must be non-zero') if day == 0
    @validations[:day_of_year] << day
  end
  # enforce uniqueness
  @validations[:day_of_year].uniq!
  self
end

#hour_of_day(*hours) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ice_cube/validation_types.rb', line 97

def hour_of_day(*hours)
  @validations[:hour_of_day] ||= []
  @validation_types[:hour_of_day] ||= IceCube::HourOfDayValidation.new(self)
  hours.each do |hour| 
    raise ArgumentError.new('Argument must be a valid hour') unless hour < 24 && hour >= 0
    @validations[:hour_of_day] << hour
  end
  # enforce uniqueness
  @validations[:hour_of_day].uniq!
  self
end

#minute_of_hour(*minutes) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ice_cube/validation_types.rb', line 125

def minute_of_hour(*minutes)
  @validations[:minute_of_hour] ||= []
  @validation_types[:minute_of_hour] ||= IceCube::MinuteOfHourValidation.new(self)
  minutes.each do |minute|
    raise ArgumentError.new('Argument must be a valid minute') unless minute < 60 && minute >= 0
    @validations[:minute_of_hour] << minute
  end
  # enforce uniqueness
  @validations[:minute_of_hour].uniq!
  self
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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ice_cube/validation_types.rb', line 58

def month_of_year(*months)
  @validations[:month_of_year] ||= []
  @validation_types[:month_of_year] ||= IceCube::MonthOfYearValidation.new(self)
  months.each do |month|
    if month.is_a?(Integer)
      # integer type argument
      raise ArgumentError.new('Argument must be a valid month (1-12)') unless month >= 1 && month <= 12
      @validations[:month_of_year] << month
    else
      #symbol type argument
      raise ArgumentError.new('Argument must be a valid month') unless IceCube::MONTHS.has_key?(month)
      @validations[:month_of_year] << IceCube::MONTHS[month]
    end
  end
  # enforce uniqueness
  @validations[:month_of_year].uniq!
  self
end

#second_of_minute(*seconds) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/ice_cube/validation_types.rb', line 3

def second_of_minute(*seconds)
  @validations[:second_of_minute] ||= []
  @validation_types[:second_of_minute] ||= IceCube::SecondOfMinuteValidation.new(self)
  seconds.each do |second|
    raise ArgumentError.new('Argument must be a valid second') unless second < 60 && second >= 0
    @validations[:second_of_minute] << second
  end
  # enforce uniqueness
  @validations[:second_of_minute].uniq!
  self
end