Class: CronCalc::Parser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_partsObject (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_stringObject (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.

Parameters:

  • period (Range)

    The time period for which to calculate cron job occurrences.

Returns:

  • (Array<Time>)

    An array of Time instances representing each occurrence.



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.

Parameters:

  • count (Integer) (defaults to: 1)

    The number of past occurrences to calculate.

  • before (Time) (defaults to: Time.now)

    The end time until which to calculate occurrences.

  • max_years (Integer) (defaults to: 5)

    The maximum number of years to consider for the period.

Returns:

  • (Array<Time>)

    An array of the last ‘n’ occurrences.



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.

Parameters:

  • count (Integer) (defaults to: 1)

    The number of occurrences to calculate.

  • after (Time) (defaults to: Time.now)

    The start time from which to calculate occurrences.

  • max_years (Integer) (defaults to: 5)

    The maximum number of years to consider for the period.

Returns:

  • (Array<Time>)

    An array of the next ‘n’ occurrences.



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