Class: When::Cron::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/when-cron/cron/validator.rb

Constant Summary collapse

MONTHS =
{
  'JAN' => '1',
  'FEB' => '2',
  'MAR' => '3',
  'APR' => '4',
  'MAY' => '5',
  'JUN' => '6',
  'JUL' => '7',
  'AUG' => '8',
  'SEP' => '9',
  'OCT' => '10',
  'NOV' => '11',
  'DEC' => '12',
}
WDAYS =
{
  'SUN' => '0',
  'MON' => '1',
  'TUE' => '2',
  'WED' => '3',
  'THU' => '4',
  'FRI' => '5',
  'SAT' => '6',
}
NOT_BASIC_CHARS =
/[^\d,\/\*\-]/
NOT_BASIC_OR_LETTERS =
/[^\d,\/\*\-a-zA-Z]/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cron) ⇒ Validator

Returns a new instance of Validator.



44
45
46
# File 'lib/when-cron/cron/validator.rb', line 44

def initialize(cron)
  @cron = cron
end

Instance Attribute Details

#cronObject (readonly)

Returns the value of attribute cron.



42
43
44
# File 'lib/when-cron/cron/validator.rb', line 42

def cron
  @cron
end

Class Method Details

.valid?(cron) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/when-cron/cron/validator.rb', line 32

def self.valid?(cron)
  v = new(cron)
  v.correct_number_of_parts? &&
  v.min_correct? &&
  v.hour_correct? &&
  v.day_correct? &&
  v.month_correct? &&
  v.wday_correct?
end

Instance Method Details

#correct_number_of_parts?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/when-cron/cron/validator.rb', line 72

def correct_number_of_parts?
  cron.split(' ').count == 5
end

#dayObject



60
61
62
# File 'lib/when-cron/cron/validator.rb', line 60

def day
  parts[2]
end

#day_correct?Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/when-cron/cron/validator.rb', line 86

def day_correct?
  correct_range?(day, 1..31) &&
  !(day =~ NOT_BASIC_CHARS)
end

#hourObject



56
57
58
# File 'lib/when-cron/cron/validator.rb', line 56

def hour
  parts[1]
end

#hour_correct?Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/when-cron/cron/validator.rb', line 81

def hour_correct?
  correct_range?(hour, 0..23) &&
  !(hour =~ NOT_BASIC_CHARS)
end

#minObject



52
53
54
# File 'lib/when-cron/cron/validator.rb', line 52

def min
  parts[0]
end

#min_correct?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/when-cron/cron/validator.rb', line 76

def min_correct?
  correct_range?(min, 0..59) &&
  !(min =~ NOT_BASIC_CHARS)
end

#monthObject



64
65
66
# File 'lib/when-cron/cron/validator.rb', line 64

def month
  parts[3]
end

#month_correct?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/when-cron/cron/validator.rb', line 91

def month_correct?
  correct_range?(month, 1..12) &&
  month.scan(/[a-zA-Z]+/).all? { |w| MONTHS[w.upcase] } &&
  !(month =~ NOT_BASIC_OR_LETTERS)
end

#partsObject



48
49
50
# File 'lib/when-cron/cron/validator.rb', line 48

def parts
  @parts ||= cron.split(' ')
end

#wdayObject



68
69
70
# File 'lib/when-cron/cron/validator.rb', line 68

def wday
  parts[4]
end

#wday_correct?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/when-cron/cron/validator.rb', line 97

def wday_correct?
  correct_range?(wday, 0..6) &&
  wday.scan(/[a-zA-Z]+/).all? { |w| WDAYS[w.upcase] } &&
  !(wday =~ NOT_BASIC_OR_LETTERS)
end