Class: Cronos::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/cronos.rb

Direct Known Subclasses

TaskInterval

Defined Under Namespace

Classes: RepeatInterval

Constant Summary collapse

MONTHS =
[nil, :jan, :feb, :mar, :apr, :may, :jun, :jul, :aug, :sep, :oct, :nov, :dec]
DAYS =
[:sun, :mon, :tue, :wed, :thu, :fri, :sat]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dayObject

Returns the value of attribute day.



10
11
12
# File 'lib/cronos.rb', line 10

def day
  @day
end

#dowObject

Returns the value of attribute dow.



10
11
12
# File 'lib/cronos.rb', line 10

def dow
  @dow
end

#hourObject

Returns the value of attribute hour.



10
11
12
# File 'lib/cronos.rb', line 10

def hour
  @hour
end

#minObject

Returns the value of attribute min.



10
11
12
# File 'lib/cronos.rb', line 10

def min
  @min
end

#monthObject

Returns the value of attribute month.



10
11
12
# File 'lib/cronos.rb', line 10

def month
  @month
end

Instance Method Details

#at(*times) ⇒ Object

Time:

  at(12)
  at(1.30)
  at('01.30')
  at(14.30)
  at('2pm')
  at('2:20')
  at('5:15pm')

  at(12.30, '3.15 pm', 22, '15:30')
Note that the last one will return an array of cron strings


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cronos.rb', line 29

def at(*times)
  @hours = []
  @mins = []
  
  times.each do |time|
    @hour, @min, meridian = parse_time(time)

    raise "invalid hour value for 'at'" if @hour > 12 && meridian
    raise "invalid minute value for 'at'" if @min > 59

    case meridian
    when 'am'
      @hour = 0 if @hour == 12 
    when 'pm'
      @hour += 12 if @hour < 12
    end

    raise "invalid hour value for 'at'" if @hour > 23

    @hours << @hour
    @mins << @min
  end
  
  self
end

#dailyObject Also known as: once_a_day



154
155
156
157
158
159
# File 'lib/cronos.rb', line 154

def daily
  @min   ||= 0
  @hour  ||= 0
  @day   = nil
  self
end

#days(*args) ⇒ Object Also known as: on_days, on_day

Days of the week:

days(:monday)
days('Monday')
days(:mon)
days(1..3)
days('mon'..'wed')
days(1...4)
on_day(:monday)
days(:mon, :tues)
on_days(:mon, :tues)


112
113
114
115
116
117
118
119
120
# File 'lib/cronos.rb', line 112

def days(*args)
  if args.first.is_a?(Range)
    @dow = format_range(args.first)
  else
    list = args.map {|day| day_value(day) unless day.is_a?(Fixnum) }
    @dow = list.join(',')
  end
  self
end

#every(*multiple) ⇒ Object

Repeats an interval:

  every(10).minutes
  every(6).hours
  every(2).months

or use as an alias for #on or #days
  every(:monday)
  every(:mon, :tues)
  every('Monday'.. 'Wednesday')
  every('February', :march)
  every('Feb'..'June')


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cronos.rb', line 68

def every(*multiple)
  return RepeatInterval.new(multiple.first, self) if multiple.first.is_a?(Fixnum)

  if multiple.all? {|abbr| is_month?(abbr) }
    of(*multiple)
  elsif multiple.all? {|abbr| is_day?(abbr) }
    days(*multiple)
  else
    raise "Unknown interval type passed to #every"
  end
end

#hourlyObject



148
149
150
151
152
# File 'lib/cronos.rb', line 148

def hourly
  @min  = 0
  @hour = nil
  self
end

#middayObject Also known as: at_midday



169
170
171
172
173
# File 'lib/cronos.rb', line 169

def midday
  @min  = 0
  @hour = 12
  self
end

#midnightObject Also known as: at_midnight



162
163
164
165
166
# File 'lib/cronos.rb', line 162

def midnight
  @min  = 0
  @hour = 0
  self
end

#monthlyObject Also known as: once_a_month



186
187
188
189
190
191
192
193
# File 'lib/cronos.rb', line 186

def monthly
  @min   ||= 0
  @hour  ||= 0
  @day   ||= 1
  @month = nil
  @dow   = nil
  self
end

#of(*args) ⇒ Object Also known as: of_months, in

Months:

of(:january)
of('January')
of(:jan)
of(:jan, :feb, :mar)
of(1..3)
of('jan'..'mar')
of(1...4)
of_months(1, 2, 3)
in(:january)


136
137
138
139
140
141
142
143
144
# File 'lib/cronos.rb', line 136

def of(*args)
  if args.first.is_a?(Range)
    @month = format_range(args.first)
  else
    list = args.map {|month| month_value(month) unless month.is_a?(Fixnum) }
    @month = list.join(',')
  end
  self
end

#on(*args) ⇒ Object Also known as: on_the

Days of month:

on(13)
on('13th')
on(13..17)
on('13th'..'17th')
on(13...18)
on_the('13th')


89
90
91
92
93
94
95
96
97
# File 'lib/cronos.rb', line 89

def on(*args)
  if args.first.is_a?(Range)
    @day = format_range(args.first)
  else
    list = args.collect {|day| day.to_s.to_i }
    @day = list.join(',')
  end
  self
end

#to_hashObject

Returns the rules as a human-readable hash



226
227
228
229
230
231
232
233
234
# File 'lib/cronos.rb', line 226

def to_hash
  if @hours and @mins and @hours.length == @mins.length and @mins.length > 1
    [@hours, @mins].transpose.collect do |hour, min|
      cron_hash hour, min
    end
  else
    cron_hash
  end
end

#to_sObject

Returns the rules as a cron string (like the ones used in /etc/crontab)



213
214
215
216
217
218
219
220
221
# File 'lib/cronos.rb', line 213

def to_s
  if @hours and @mins and @hours.length == @mins.length and @mins.length > 1
    try_to_combine([@hours, @mins].transpose.collect do |hour, min|
      cron_string hour, min
    end)
  else
    cron_string
  end
end

#weekdaysObject



196
197
198
199
200
201
# File 'lib/cronos.rb', line 196

def weekdays
  @min  ||= 0
  @hour ||= 0
  @dow  = '1-5'
  self
end

#weekendsObject



203
204
205
206
207
208
# File 'lib/cronos.rb', line 203

def weekends
  @min  ||= 0
  @hour ||= 0
  @dow  = '0,6'
  self
end

#weeklyObject Also known as: once_a_week



176
177
178
179
180
181
182
183
# File 'lib/cronos.rb', line 176

def weekly
  @min   ||= 0
  @hour  ||= 0
  @dow   ||= 0
  @day   = nil
  @month = nil
  self
end