cronos 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGELOG +4 -0
  2. data/README.rdoc +17 -9
  3. data/lib/cronos.rb +58 -11
  4. data/spec/cronos_spec.rb +28 -0
  5. metadata +2 -2
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.4.0 2009-03-06
2
+ - every method now handles days and months eg. every(:mon, :tue), every('mon'..'wed'), every(:jan, :feb) etc.
3
+ - added schedule method which takes a command and outputs it in the cron string
4
+
1
5
  == 0.3.1 2009-02-03
2
6
  - added ranges to #days
3
7
 
@@ -10,33 +10,41 @@ you are after instead of trying to remember the cron syntax and conventions.
10
10
 
11
11
  cron = Cronos::Interval
12
12
 
13
- cron.new.hourly.to_s
13
+ puts cron.new.hourly
14
14
  # => '0 * * * *'
15
15
 
16
- cron.new.daily.to_s
16
+ puts cron.new.daily
17
17
  # => '0 0 * * *'
18
18
 
19
- cron.new.weekly.to_s
19
+ puts cron.new.weekly
20
20
  # => '0 0 * * 0'
21
21
 
22
- cron.new.weekdays.at(12.30).to_s
22
+ puts cron.new.weekdays.at(12.30)
23
23
  # => '30 12 * * 1-5'
24
24
 
25
- cron.new.weekends.at(12.30).to_s
25
+ puts cron.new.weekends.at(12.30)
26
26
  # => '30 12 * * 0,6'
27
27
 
28
- cron.new.at('1.30pm').daily.to_s
28
+ puts cron.new.at('1.30pm').daily
29
29
  # => '30 13 * * *'
30
30
 
31
- cron.new.every(6).hours.on('15th').of(:january).to_s
31
+ puts cron.new.every(6).hours.on('15th').of(:january)
32
32
  # => '0 0,6,12,18 15 1 *'
33
33
 
34
- cron.new.every(20).minutes.on(15..18).of(:jan, :feb, :mar).to_s
34
+ puts cron.new.every(20).minutes.on('15th'..'18th').of(:jan, :feb, :mar)
35
35
  # => '0,20,40 * 15-18 1,2,3 *'
36
36
 
37
- cron.new.at(14.45).on_days(:monday, :tuesday).to_s
37
+ puts cron.new.at(14.45).every(:monday, :tuesday)
38
38
  # => '45 14 * * 1,2'
39
39
 
40
+
41
+ You can also output the whole cron task string using the schedule method:
42
+
43
+ puts Cronos.schedule('some_task').every(:Sunday).at('3am')
44
+ # => '0 3 * * 0 some_task'
45
+
46
+ This does not actually schedule the task in cron. Cronos is merely a DSL to output the cron intervals.
47
+
40
48
  == Caveats
41
49
 
42
50
  Certain combinations produce unintuitive results. They should be obvious but just in case I will
@@ -1,11 +1,15 @@
1
1
  module Cronos
2
2
 
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4.0'
4
+
5
+ def self.schedule(task)
6
+ TaskInterval.new(task)
7
+ end
4
8
 
5
9
  class Interval
6
10
  attr_accessor :min, :hour, :day, :month, :dow
7
11
 
8
- MONTHS = [:jan, :feb, :mar, :apr, :may, :jun, :jul, :aug, :sep, :oct, :nov, :dec]
12
+ MONTHS = [nil, :jan, :feb, :mar, :apr, :may, :jun, :jul, :aug, :sep, :oct, :nov, :dec]
9
13
 
10
14
  DAYS = [:sun, :mon, :tue, :wed, :thu, :fri, :sat]
11
15
 
@@ -37,17 +41,18 @@ module Cronos
37
41
  #
38
42
  # or use as an alias for #on or #days
39
43
  # every(:monday)
44
+ # every(:mon, :tues)
45
+ # every('Monday'.. 'Wednesday')
40
46
  # every('February', :march)
47
+ # every('Feb'..'June')
41
48
  #
42
49
  def every(*multiple)
43
50
  return RepeatInterval.new(multiple.first, self) if multiple.first.is_a?(Fixnum)
44
51
 
45
- abbrs = multiple.map {|i| i.to_s.downcase[0..2].to_sym }
46
-
47
- if abbrs.all? {|abbr| MONTHS.include?(abbr) }
48
- of(*abbrs)
49
- elsif abbrs.all? {|abbr| DAYS.include?(abbr) }
50
- days(*abbrs)
52
+ if multiple.all? {|abbr| is_month?(abbr) }
53
+ of(*multiple)
54
+ elsif multiple.all? {|abbr| is_day?(abbr) }
55
+ days(*multiple)
51
56
  else
52
57
  raise "Unknown interval type passed to #every"
53
58
  end
@@ -57,6 +62,7 @@ module Cronos
57
62
  # on(13)
58
63
  # on('13th')
59
64
  # on(13..17)
65
+ # on('13th'..'17th')
60
66
  # on(13...18)
61
67
  # on_the('13th')
62
68
  #
@@ -76,6 +82,7 @@ module Cronos
76
82
  # days('Monday')
77
83
  # days(:mon)
78
84
  # days(1..3)
85
+ # days('mon'..'wed')
79
86
  # days(1...4)
80
87
  # on_day(:monday)
81
88
  # days(:mon, :tues)
@@ -85,7 +92,7 @@ module Cronos
85
92
  if args.first.is_a?(Range)
86
93
  @dow = format_range(args.first)
87
94
  else
88
- list = args.map {|day| DAYS.index(day.to_s.downcase[0..2].to_sym) }
95
+ list = args.map {|day| day_value(day) unless day.is_a?(Fixnum) }
89
96
  @dow = list.join(',')
90
97
  end
91
98
  self
@@ -99,6 +106,7 @@ module Cronos
99
106
  # of(:jan)
100
107
  # of(:jan, :feb, :mar)
101
108
  # of(1..3)
109
+ # of('jan'..'mar')
102
110
  # of(1...4)
103
111
  # of_months(1, 2, 3)
104
112
  # in(:january)
@@ -107,7 +115,7 @@ module Cronos
107
115
  if args.first.is_a?(Range)
108
116
  @month = format_range(args.first)
109
117
  else
110
- list = args.map {|month| MONTHS.index(month.to_s.downcase[0..2].to_sym) + 1 unless month.is_a?(Fixnum) }
118
+ list = args.map {|month| month_value(month) unless month.is_a?(Fixnum) }
111
119
  @month = list.join(',')
112
120
  end
113
121
  self
@@ -205,10 +213,37 @@ module Cronos
205
213
  end
206
214
 
207
215
  def format_range(range)
208
- list = Array(range).sort
216
+ values = [range.first, range.last]
217
+
218
+ if values.all? {|v| v.to_i > 0 }
219
+ first, last = values.first.to_i, values.last.to_i
220
+ elsif values.all? {|abbr| is_month?(abbr) }
221
+ first, last = month_value(values.first), month_value(values.last)
222
+ elsif values.all? {|abbr| is_day?(abbr) }
223
+ first, last = day_value(values.first), day_value(values.last)
224
+ end
225
+
226
+ int_range = range.exclude_end? ? first...last : first..last
227
+ list = Array(int_range).sort
209
228
  "#{list.first}-#{list.last}"
210
229
  end
211
230
 
231
+ def is_month?(value)
232
+ MONTHS.include?(value.to_s.downcase[0..2].to_sym)
233
+ end
234
+
235
+ def month_value(value)
236
+ MONTHS.index(value.to_s.downcase[0..2].to_sym)
237
+ end
238
+
239
+ def is_day?(value)
240
+ DAYS.include?(value.to_s.downcase[0..2].to_sym)
241
+ end
242
+
243
+ def day_value(value)
244
+ DAYS.index(value.to_s.downcase[0..2].to_sym)
245
+ end
246
+
212
247
  class RepeatInterval
213
248
 
214
249
  def initialize(multiple, interval)
@@ -258,4 +293,16 @@ module Cronos
258
293
 
259
294
  end
260
295
 
296
+ class TaskInterval < Interval
297
+ attr_accessor :task
298
+
299
+ def initialize(task)
300
+ @task = task
301
+ end
302
+
303
+ def to_s
304
+ "#{super} #{@task}"
305
+ end
306
+ end
307
+
261
308
  end
@@ -1,5 +1,13 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
+ describe Cronos do
4
+
5
+ it "should return task interval instance from schedule method" do
6
+ Cronos.schedule('ls').should be_kind_of(Cronos::TaskInterval)
7
+ end
8
+
9
+ end
10
+
3
11
  describe Cronos::Interval do
4
12
 
5
13
  it "should return default interval for every minute" do
@@ -57,6 +65,10 @@ describe Cronos::Interval do
57
65
  interval.on(15..17).to_s.should == '* * 15-17 * *'
58
66
  end
59
67
 
68
+ it "should output interval from string inclusive range as dashed day of month range " do
69
+ interval.on('15th'..'17th').to_s.should == '* * 15-17 * *'
70
+ end
71
+
60
72
  it "should output interval from exclusive range as dashed day of month range " do
61
73
  interval.on(15...18).to_s.should == '* * 15-17 * *'
62
74
  end
@@ -99,6 +111,10 @@ describe Cronos::Interval do
99
111
  interval.of(1..3).to_s.should == '* * * 1-3 *'
100
112
  end
101
113
 
114
+ it "should output interval from string inclusive range as dashed month range " do
115
+ interval.of('jan'..'mar').to_s.should == '* * * 1-3 *'
116
+ end
117
+
102
118
  it "should output interval from integer exclusive range as dashed month range " do
103
119
  interval.of(1...4).to_s.should == '* * * 1-3 *'
104
120
  end
@@ -133,6 +149,10 @@ describe Cronos::Interval do
133
149
  interval.days(1..3).to_s.should == '* * * * 1-3'
134
150
  end
135
151
 
152
+ it "should output interval from string inclusive range as dashed dow range " do
153
+ interval.days('mon'..'wed').to_s.should == '* * * * 1-3'
154
+ end
155
+
136
156
  it "should output interval from integer exclusive range as dashed dow range " do
137
157
  interval.days(1...4).to_s.should == '* * * * 1-3'
138
158
  end
@@ -327,3 +347,11 @@ describe Cronos::Interval do
327
347
  @interval ||= Cronos::Interval.new
328
348
  end
329
349
  end
350
+
351
+ describe Cronos::TaskInterval do
352
+
353
+ it "should output task at end of interval string" do
354
+ Cronos::TaskInterval.new('ls').at('12pm').to_s.should == '0 12 * * * ls'
355
+ end
356
+
357
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cronos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
@@ -9,7 +9,7 @@ autorequire: cronos
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-03 00:00:00 +11:00
12
+ date: 2009-03-12 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15