Class: CronCalc::Parser
- Inherits:
-
Object
- Object
- CronCalc::Parser
- Defined in:
- lib/cron_calc.rb
Overview
The Parser class provides functionality to parse and calculate occurrences of cron jobs within a given time period. It interprets cron strings and calculates when cron jobs will occur
Constant Summary collapse
- RANGE =
{ minutes: 0..59, hours: 0..23, days: 1..31, months: 1..12, wdays: 0..6 }.freeze
- WDAYS =
{ 'SUN' => '0', 'MON' => '1', 'TUE' => '2', 'WED' => '3', 'THU' => '4', 'FRI' => '5', 'SAT' => '6' }.freeze
- 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' }.freeze
Instance Attribute Summary collapse
-
#cron_parts ⇒ Object
readonly
Returns the value of attribute cron_parts.
-
#cron_string ⇒ Object
readonly
Returns the value of attribute cron_string.
Instance Method Summary collapse
-
#in(period) ⇒ Array<Time>
Calculates cron job occurrences within a given time period.
-
#initialize(cron_string) ⇒ Parser
constructor
A new instance of Parser.
-
#last(count = 1, before: Time.now, max_years: 5) ⇒ Array<Time>
Calculates the last ‘n’ occurrences of the cron job until a given end time.
-
#next(count = 1, after: Time.now, max_years: 5) ⇒ Array<Time>
Calculates the next ‘n’ occurrences of the cron job from a given start time.
Constructor Details
#initialize(cron_string) ⇒ Parser
Returns a new instance of Parser.
39 40 41 42 43 44 45 |
# File 'lib/cron_calc.rb', line 39 def initialize(cron_string) @cron_string = cron_string raise 'Cron expression is not supported or invalid' unless cron_string_valid? @cron_parts = split_cron_string end |
Instance Attribute Details
#cron_parts ⇒ Object (readonly)
Returns the value of attribute cron_parts.
18 19 20 |
# File 'lib/cron_calc.rb', line 18 def cron_parts @cron_parts end |
#cron_string ⇒ Object (readonly)
Returns the value of attribute cron_string.
18 19 20 |
# File 'lib/cron_calc.rb', line 18 def cron_string @cron_string end |
Instance Method Details
#in(period) ⇒ Array<Time>
Calculates cron job occurrences within a given time period.
50 51 52 |
# File 'lib/cron_calc.rb', line 50 def in(period) occurrences(period) end |
#last(count = 1, before: Time.now, max_years: 5) ⇒ Array<Time>
Calculates the last ‘n’ occurrences of the cron job until a given end time.
71 72 73 74 75 76 77 |
# File 'lib/cron_calc.rb', line 71 def last(count = 1, before: Time.now, max_years: 5) occurrences( (before - (60 * 60 * 24 * 365 * max_years))..before, count, reverse: true ) end |
#next(count = 1, after: Time.now, max_years: 5) ⇒ Array<Time>
Calculates the next ‘n’ occurrences of the cron job from a given start time.
59 60 61 62 63 64 |
# File 'lib/cron_calc.rb', line 59 def next(count = 1, after: Time.now, max_years: 5) occurrences( after..(after + (60 * 60 * 24 * 365 * max_years)), count ) end |