class CronSpec::CronSpecification

Encapsulates the interpretation of a cron specification and provides a method which determines if the specified time is effective with respect to the specification.

Constants

ANNUALLY
DAILY
DAY
DOW
HOUR
HOURLY
MIDNIGHT
MINUTE
MONTH
MONTHLY
SHORTCUT_CRONS
SHORTCUT_PATTERN
TimeMethods
WEEKLY
YEARLY

Shortcut patterns

Attributes

raw_specification[R]

Public Class Methods

new(raw_specification) click to toggle source

Constructs a new CronSpecification with a textual cron specificiation.

A broad cron syntax is supported:

*    *    *    *    *      
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- day of week (0 - 6) (Sunday=0)
|    |    |    +---------- month (1 - 12)
|    |    +--------------- day of month (1 - 31)
|    +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)

The following named entries can be used:

  • Day of week - sun, mon, tue, wed, thu, fri, sat

  • Month - jan feb mar apr may jun jul aug sep oct nov dec

The following constructs are supported:

  • Ranges are supported (e.g. 2-10 or mon-fri)

  • Multiple values are supported (e.g. 2,3,8 or mon,wed,fri)

  • Wildcards are supported (e.g. *)

  • Step values are supported (e.g. */4)

  • Combinations of all but wildcard are supported (e.g. 2,*/3,8-10)

A single space is required between each group.

# File lib/cron-spec/cron_specification.rb, line 69
def initialize(raw_specification)
  raise "Must specify a cron specification" if raw_specification.nil?

  @raw_specification = (raw_specification =~ SHORTCUT_PATTERN) ? SHORTCUT_CRONS[$1] : raw_specification

  specification = @raw_specification.split(' ')
  raise "Invalid cron specification" if specification.size != 5

  @cron_values = []
  @cron_values << parse_specification(specification[MINUTE], MinuteFactory.new)
  @cron_values << parse_specification(specification[HOUR], HourFactory.new)
  @cron_values << parse_specification(specification[DAY], DayFactory.new)
  @cron_values << parse_specification(specification[MONTH], MonthFactory.new)
  @cron_values << parse_specification(specification[DOW], DowFactory.new)
end

Public Instance Methods

is_specification_in_effect?(time=Time.now) click to toggle source

Return true if the specified time falls within the definition of the CronSpecification. The parameter defaults to the current time.

# File lib/cron-spec/cron_specification.rb, line 89
def is_specification_in_effect?(time=Time.now)
  idx = 0
  test_results = @cron_values.collect do | cvalues |
    time_value = time.send(TimeMethods[idx])
    idx += 1
    !cvalues.detect { | cv | cv.is_effective?(time_value) }.nil?
  end.all?

end

Private Instance Methods

parse_specification(specification, factory) click to toggle source
# File lib/cron-spec/cron_specification.rb, line 101
def parse_specification(specification, factory)
  specification.split(',').collect do |spec_component|
    factory.parse(spec_component)
  end
end