Skip to content

Commit

Permalink
Merge pull request #202 from fbuenemann/patch-1
Browse files Browse the repository at this point in the history
accept '@reboot' and friends in string cron syntax
  • Loading branch information
javan committed Dec 22, 2011
2 parents bd57f39 + 81dde20 commit 4cabc62
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
14 changes: 4 additions & 10 deletions lib/whenever/cron.rb
Expand Up @@ -3,7 +3,8 @@
module Whenever
module Output
class Cron
REGEX = /^.+ .+ .+ .+ .+.?$/
KEYWORDS = [:reboot, :yearly, :annually, :monthly, :weekly, :daily, :midnight, :hourly]
REGEX = /^(@(#{KEYWORDS.join '|'})|.+ .+ .+ .+ .+.?)$/

attr_accessor :time, :task

Expand Down Expand Up @@ -52,25 +53,18 @@ def time_in_cron_syntax

protected
def day_given?
months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
months = %w(jan feb mar apr may jun jul aug sep oct nov dec)
@at_given.is_a?(String) && months.any? { |m| @at_given.downcase.index(m) }
end

def parse_symbol
shortcut = case @time
when :reboot then '@reboot'
when *KEYWORDS then "@#{@time}" # :reboot => '@reboot'
when :year then 12.months
when :yearly,
:annually then '@annually'
when :day then 1.day
when :daily then '@daily'
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

if shortcut.is_a?(Numeric)
Expand Down
6 changes: 4 additions & 2 deletions test/unit/cron_test.rb
Expand Up @@ -184,7 +184,7 @@ class CronTest < Test::Unit::TestCase
should "parse a :symbol into the correct shortcut" do
assert_equal '@reboot', parse_time(:reboot)
assert_equal '@annually', parse_time(:annually)
assert_equal '@annually', parse_time(:yearly)
assert_equal '@yearly', parse_time(:yearly)
assert_equal '@daily', parse_time(:daily)
assert_equal '@midnight', parse_time(:midnight)
assert_equal '@monthly', parse_time(:monthly)
Expand Down Expand Up @@ -217,7 +217,9 @@ class CronTest < Test::Unit::TestCase

context "When given raw cron sytax" do
should "return the same cron sytax" do
crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *']
crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *',
'@reboot', '@yearly', '@annually', '@monthly', '@weekly',
'@daily', '@midnight', '@hourly']
crons.each do |cron|
assert_equal cron, parse_time(cron)
end
Expand Down

0 comments on commit 4cabc62

Please sign in to comment.