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

Constructor Details

#initialize(interval = nil) ⇒ Interval

Returns a new instance of Interval.



16
17
18
# File 'lib/cronos.rb', line 16

def initialize(interval=nil)
  @min, @hour, @day, @month, @dow = *parse_cron(interval) if interval
end

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(time) ⇒ Object

Time:

at(12)
at(1.30)
at('01.30')
at(14.30)
at('2pm')

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cronos.rb', line 27

def at(time)
  @hour, @min, meridian = parse_time(time)

  raise ArgumentError, "invalid hour value for 'at'" if @hour > 12 && meridian
  raise ArgumentError, "invalid minute value for 'at'" if @min > 59
    
  case meridian
    when 'am' then @hour = 0 if @hour == 12
    when 'pm' then @hour += 12 if @hour < 12
  end
  raise ArgumentError, "invalid hour value for 'at'" if @hour > 23
  self
end

#dailyObject Also known as: once_a_day



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

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)


95
96
97
98
99
100
101
102
103
# File 'lib/cronos.rb', line 95

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?(Numeric) }
    @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')


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cronos.rb', line 53

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

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

#hourlyObject



130
131
132
133
134
# File 'lib/cronos.rb', line 130

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

#middayObject Also known as: at_midday



151
152
153
154
155
# File 'lib/cronos.rb', line 151

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

#midnightObject Also known as: at_midnight



144
145
146
147
148
# File 'lib/cronos.rb', line 144

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

#monthlyObject Also known as: once_a_month



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

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)


118
119
120
121
122
123
124
125
126
# File 'lib/cronos.rb', line 118

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?(Numeric) }
    @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')


73
74
75
76
77
78
79
80
81
# File 'lib/cronos.rb', line 73

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



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

def to_hash
  {
    :minute  => "#{min   || '*'}",
    :hour    => "#{hour  || '*'}",
    :day     => "#{day   || '*'}",
    :month   => "#{month || '*'}",
    :weekday => "#{dow   || '*'}"
  }
end

#to_sObject



192
193
194
# File 'lib/cronos.rb', line 192

def to_s
  "#{min || '*'} #{hour || '*'} #{day || '*'} #{month || '*'} #{dow || '*'}"
end

#weekdaysObject



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

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

#weekendsObject



185
186
187
188
189
190
# File 'lib/cronos.rb', line 185

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

#weeklyObject Also known as: once_a_week



158
159
160
161
162
163
164
165
# File 'lib/cronos.rb', line 158

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