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
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.
47 48 49 50 51 52 53 54 |
# File 'lib/cron_calc.rb', line 47 def initialize(cron_string) @cron_string = cron_string raise 'Cron expression is not supported or invalid' unless cron_string_valid? @cron_string = normalize_with(cron_string, PREDEFINED_DEFINITIONS) if @cron_string.start_with? '@' @cron_parts = split_cron_string end |
Instance Attribute Details
#cron_parts ⇒ Object (readonly)
Returns the value of attribute cron_parts.
45 46 47 |
# File 'lib/cron_calc.rb', line 45 def cron_parts @cron_parts end |
#cron_string ⇒ Object (readonly)
Returns the value of attribute cron_string.
45 46 47 |
# File 'lib/cron_calc.rb', line 45 def cron_string @cron_string end |
Instance Method Details
#in(period) ⇒ Array<Time>
Calculates cron job occurrences within a given time period.
59 60 61 |
# File 'lib/cron_calc.rb', line 59 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.
80 81 82 83 84 85 86 |
# File 'lib/cron_calc.rb', line 80 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.
68 69 70 71 72 73 |
# File 'lib/cron_calc.rb', line 68 def next(count = 1, after: Time.now, max_years: 5) occurrences( after..(after + (60 * 60 * 24 * 365 * max_years)), count ) end |