Class: Philiprehberger::CronKit::Expression

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/philiprehberger/cron_kit/expression.rb

Overview

Parses and evaluates 5-field cron expressions.

Supported fields: minute (0-59), hour (0-23), day-of-month (1-31), month (1-12), day-of-week (0-6, Sunday = 0).

Supported syntax: *, specific values, ranges (1-5), steps (*/5), lists (1,3,5). Non-standard aliases: @hourly, @daily, @weekly, @monthly, @yearly, @annually.

Constant Summary

Constants included from Parser

Parser::FIELD_NAMES, Parser::FIELD_RANGES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse_expression

Constructor Details

#initialize(expression, timezone: nil) ⇒ Expression

Returns a new instance of Expression.



28
29
30
31
32
33
# File 'lib/philiprehberger/cron_kit/expression.rb', line 28

def initialize(expression, timezone: nil)
  @raw = Aliases.expand(expression.to_s.strip)
  @timezone = timezone
  @utc_offset = Timezone.utc_offset_for(timezone)
  @fields = parse_expression(@raw)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



17
18
19
# File 'lib/philiprehberger/cron_kit/expression.rb', line 17

def raw
  @raw
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



17
18
19
# File 'lib/philiprehberger/cron_kit/expression.rb', line 17

def timezone
  @timezone
end

Class Method Details

.valid?(expression, timezone: nil) ⇒ Boolean

Return true if the given expression parses without error. Provides a non-raising alternative to rescuing ‘ParseError`.

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/philiprehberger/cron_kit/expression.rb', line 21

def self.valid?(expression, timezone: nil)
  new(expression, timezone: timezone)
  true
rescue ParseError, ArgumentError
  false
end

Instance Method Details

#match?(time) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/philiprehberger/cron_kit/expression.rb', line 35

def match?(time)
  time = coerce_time(time)
  values = [time.min, time.hour, time.day, time.month, time.wday]
  @fields.each_with_index.all? { |set, i| set.include?(values[i]) }
end

#next_at(from: Time.now) ⇒ Object



41
42
43
44
# File 'lib/philiprehberger/cron_kit/expression.rb', line 41

def next_at(from: Time.now)
  time = start_time(from, 60)
  scan_forward(time)
end

#next_runs(count: 5, from: Time.now) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/philiprehberger/cron_kit/expression.rb', line 46

def next_runs(count: 5, from: Time.now)
  result = []
  cursor = from
  count.times do
    cursor = next_at(from: cursor)
    result << cursor
  end
  result
end

#previous_run(from: Time.now) ⇒ Object



56
57
58
59
# File 'lib/philiprehberger/cron_kit/expression.rb', line 56

def previous_run(from: Time.now)
  time = start_time_back(from)
  scan_backward(time)
end

#to_sObject



61
62
63
# File 'lib/philiprehberger/cron_kit/expression.rb', line 61

def to_s
  @raw
end