Class: Philiprehberger::CronParser::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/cron_parser/field.rb

Overview

Parses a single cron field (minute, hour, day, month, weekday)

Constant Summary collapse

MONTH_NAMES =
{
  '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
WEEKDAY_NAMES =
{
  'SUN' => 0, 'MON' => 1, 'TUE' => 2, 'WED' => 3,
  'THU' => 4, 'FRI' => 5, 'SAT' => 6
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr, min:, max:, names: nil) ⇒ Field

Returns a new instance of Field.

Parameters:

  • expr (String)

    the field expression (e.g. “*/5”, “1,3,5”, “1-10”)

  • min (Integer)

    the minimum valid value

  • max (Integer)

    the maximum valid value

  • names (Hash, nil) (defaults to: nil)

    optional name-to-value mapping for this field

Raises:

  • (Error)

    if the expression is invalid



26
27
28
29
30
31
# File 'lib/philiprehberger/cron_parser/field.rb', line 26

def initialize(expr, min:, max:, names: nil)
  @min = min
  @max = max
  @names = names
  @values = parse(expr).sort.freeze
end

Instance Attribute Details

#valuesArray<Integer> (readonly)

Returns the expanded set of valid values.

Returns:

  • (Array<Integer>)

    the expanded set of valid values



19
20
21
# File 'lib/philiprehberger/cron_parser/field.rb', line 19

def values
  @values
end

Instance Method Details

#matches?(value) ⇒ Boolean

Check if a value matches this field

Parameters:

  • value (Integer)

    the value to check

Returns:

  • (Boolean)


37
38
39
# File 'lib/philiprehberger/cron_parser/field.rb', line 37

def matches?(value)
  @values.include?(value)
end