Class: Philiprehberger::CronKit::Expression
- Inherits:
-
Object
- Object
- Philiprehberger::CronKit::Expression
- 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::DAY_NAMES, Parser::DOW_SUNDAY_ALIAS, Parser::FIELD_NAMES, Parser::FIELD_RANGES, Parser::MONTH_NAMES
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#timezone ⇒ Object
readonly
Returns the value of attribute timezone.
Class Method Summary collapse
-
.valid?(expression, timezone: nil) ⇒ Boolean
Return true if the given expression parses without error.
Instance Method Summary collapse
-
#initialize(expression, timezone: nil) ⇒ Expression
constructor
A new instance of Expression.
- #match?(time) ⇒ Boolean
-
#matches_any?(times) ⇒ Boolean
Return true if any Time in the given enumerable matches the expression.
- #next_at(from: Time.now) ⇒ Object
- #next_runs(count: 5, from: Time.now) ⇒ Object
- #previous_run(from: Time.now) ⇒ Object
- #to_s ⇒ Object
Methods included from Parser
Constructor Details
#initialize(expression, timezone: nil) ⇒ Expression
Returns a new instance of Expression.
28 29 30 31 32 33 34 35 36 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 28 def initialize(expression, timezone: nil) @raw = Aliases.(expression.to_s.strip) @timezone = timezone @utc_offset = Timezone.utc_offset_for(timezone) @fields = parse_expression(@raw) parts = @raw.split(/\s+/) @dom_restricted = parts[2] != '*' @dow_restricted = parts[4] != '*' end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
17 18 19 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 17 def raw @raw end |
#timezone ⇒ Object (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.
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
38 39 40 41 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 38 def match?(time) time = coerce_time(time) values_match?([time.min, time.hour, time.day, time.month, time.wday]) end |
#matches_any?(times) ⇒ Boolean
Return true if any Time in the given enumerable matches the expression. Short-circuits on the first match.
45 46 47 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 45 def matches_any?(times) times.any? { |time| match?(time) } end |
#next_at(from: Time.now) ⇒ Object
49 50 51 52 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 49 def next_at(from: Time.now) time = start_time(from, 60) scan_forward(time) end |
#next_runs(count: 5, from: Time.now) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 54 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
64 65 66 67 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 64 def previous_run(from: Time.now) time = start_time_back(from) scan_backward(time) end |
#to_s ⇒ Object
69 70 71 |
# File 'lib/philiprehberger/cron_kit/expression.rb', line 69 def to_s @raw end |