convert most shortcuts to seconds

This commit is contained in:
Javan Makhmali 2011-05-23 21:08:43 -04:00
parent a3b5791bbb
commit 1654f67774
4 changed files with 35 additions and 15 deletions

View File

@ -1,3 +1,8 @@
### 0.6.8 / May 23rd, 2011
* Convert most shortcuts to seconds. every :day -> every 1.day. #129 [Javan Makhmali]
### 0.6.7 / March 23rd, 2011 ### 0.6.7 / March 23rd, 2011
* Fix issue with comment block being corrupted during subsequent insertion of duplicate entries to the crontab. #123 [Jeremy (@lingmann)] * Fix issue with comment block being corrupted during subsequent insertion of duplicate entries to the crontab. #123 [Jeremy (@lingmann)]

View File

@ -45,16 +45,25 @@ module Whenever
def parse_symbol def parse_symbol
shortcut = case @time shortcut = case @time
when :reboot then '@reboot' when :reboot then '@reboot'
when :year, :yearly then '@annually' when :year then 12.months
when :day, :daily then '@daily' when :yearly,
when :midnight then '@midnight' :annually then '@annually'
when :month, :monthly then '@monthly' when :day then 1.day
when :week, :weekly then '@weekly' when :daily then '@daily'
when :hour, :hourly then '@hourly' when :midnight then '@midnight'
when :month then 1.month
when :monthly then '@monthly'
when :week then 1.week
when :weekly then '@weekly'
when :hour then 1.hour
when :hourly then '@hourly'
end end
if shortcut if shortcut.is_a?(Numeric)
@time = shortcut
parse_time
elsif shortcut
if @at.is_a?(Time) || (@at.is_a?(Numeric) && @at > 0) if @at.is_a?(Time) || (@at.is_a?(Numeric) && @at > 0)
raise ArgumentError, "You cannot specify an ':at' when using the shortcuts for times." raise ArgumentError, "You cannot specify an ':at' when using the shortcuts for times."
else else

View File

@ -166,7 +166,7 @@ class OutputAtTest < Test::Unit::TestCase
<<-file <<-file
set :job_template, nil set :job_template, nil
set :path, '/your/path' set :path, '/your/path'
every :day do every :daily do
rake "blah:blah" rake "blah:blah"
runner "runner_1" runner "runner_1"
command "command_1" command "command_1"

View File

@ -174,20 +174,26 @@ class CronTest < Test::Unit::TestCase
context "When parsing time using the cron shortcuts" do context "When parsing time using the cron shortcuts" do
should "parse a :symbol into the correct shortcut" do should "parse a :symbol into the correct shortcut" do
assert_equal '@reboot', parse_time(:reboot) assert_equal '@reboot', parse_time(:reboot)
assert_equal '@annually', parse_time(:year) assert_equal '@annually', parse_time(:annually)
assert_equal '@annually', parse_time(:yearly) assert_equal '@annually', parse_time(:yearly)
assert_equal '@daily', parse_time(:day)
assert_equal '@daily', parse_time(:daily) assert_equal '@daily', parse_time(:daily)
assert_equal '@midnight', parse_time(:midnight) assert_equal '@midnight', parse_time(:midnight)
assert_equal '@monthly', parse_time(:month)
assert_equal '@monthly', parse_time(:monthly) assert_equal '@monthly', parse_time(:monthly)
assert_equal '@hourly', parse_time(:hour) assert_equal '@weekly', parse_time(:weekly)
assert_equal '@hourly', parse_time(:hourly) assert_equal '@hourly', parse_time(:hourly)
end end
should "convert time-based shortcuts to times" do
assert_equal '0 0 1 * *', parse_time(:month)
assert_equal '0 0 * * *', parse_time(:day)
assert_equal '0 * * * *', parse_time(:hour)
assert_equal '0 0 1 12 *', parse_time(:year)
assert_equal '0 0 1,8,15,22 * *', parse_time(:week)
end
should "raise an exception if a valid shortcut is given but also an :at" do should "raise an exception if a valid shortcut is given but also an :at" do
assert_raises ArgumentError do assert_raises ArgumentError do
parse_time(:hour, nil, "1:00 am") parse_time(:hourly, nil, "1:00 am")
end end
assert_raises ArgumentError do assert_raises ArgumentError do
@ -195,7 +201,7 @@ class CronTest < Test::Unit::TestCase
end end
assert_raises ArgumentError do assert_raises ArgumentError do
parse_time(:day, nil, '4:20pm') parse_time(:daily, nil, '4:20pm')
end end
end end
end end