Class: When::Cron::Validator
- Inherits:
-
Object
- Object
- When::Cron::Validator
- 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
-
#cron ⇒ Object
readonly
Returns the value of attribute cron.
Class Method Summary collapse
Instance Method Summary collapse
- #correct_number_of_parts? ⇒ Boolean
- #day ⇒ Object
- #day_correct? ⇒ Boolean
- #hour ⇒ Object
- #hour_correct? ⇒ Boolean
-
#initialize(cron) ⇒ Validator
constructor
A new instance of Validator.
- #min ⇒ Object
- #min_correct? ⇒ Boolean
- #month ⇒ Object
- #month_correct? ⇒ Boolean
- #parts ⇒ Object
- #wday ⇒ Object
- #wday_correct? ⇒ Boolean
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
#cron ⇒ Object (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
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
72 73 74 |
# File 'lib/when-cron/cron/validator.rb', line 72 def correct_number_of_parts? cron.split(' ').count == 5 end |
#day ⇒ Object
60 61 62 |
# File 'lib/when-cron/cron/validator.rb', line 60 def day parts[2] end |
#day_correct? ⇒ 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 |
#hour ⇒ Object
56 57 58 |
# File 'lib/when-cron/cron/validator.rb', line 56 def hour parts[1] end |
#hour_correct? ⇒ 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 |
#min ⇒ Object
52 53 54 |
# File 'lib/when-cron/cron/validator.rb', line 52 def min parts[0] end |
#min_correct? ⇒ 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 |
#month ⇒ Object
64 65 66 |
# File 'lib/when-cron/cron/validator.rb', line 64 def month parts[3] end |
#month_correct? ⇒ 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 |
#parts ⇒ Object
48 49 50 |
# File 'lib/when-cron/cron/validator.rb', line 48 def parts @parts ||= cron.split(' ') end |
#wday ⇒ Object
68 69 70 |
# File 'lib/when-cron/cron/validator.rb', line 68 def wday parts[4] end |
#wday_correct? ⇒ 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 |