class Cron::Parser

Constants

FIELDS
VERSION

Attributes

fields[R]
meaning[R]
pattern[R]
warnings[R]

Public Class Methods

new(pattern = nil) click to toggle source
# File lib/cron/parser.rb, line 10
def initialize(pattern = nil)
  @pattern = pattern
  @fields  = {}; FIELDS.map { |field| @fields[field.to_sym] = nil }
  validate! # and don't ask to dissect it!
  @meaning = humanize # being human, wtf!
end
parse(pattern = nil) click to toggle source

Alias of ‘new` method.

# File lib/cron/parser.rb, line 46
def self.parse(pattern = nil) # oh, that sounds ridiculous!
  self.new(pattern).humanize
end

Public Instance Methods

humanize() click to toggle source

Alias of ‘meaning` getter method. Returns human readable meaning for cron pattern after parsing it.

# File lib/cron/parser.rb, line 31
def humanize
  @fields.collect do |_, field|
    field.meaning
  end.join("; ").chomp("; ")
end
inspect() click to toggle source

Inspects the parsed pattern, displays fields in pattern along with their meanings.

# File lib/cron/parser.rb, line 19
def inspect
  %Q{
      #<#{self.class.name}:#{Object::o_hexy_id(self)}>
      {
        :pattern => "#@pattern",
        :fields  => #{@fields.inspect}
      }
  }.squish
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/cron/parser.rb, line 50
def method_missing(method, *args, &block)
  super unless field_methods.include? method
  self.class.send(:define_method, method) do
    @fields[method.to_s.sub('_field', '').to_sym]
  end and self.send(method, *args)
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/cron/parser.rb, line 57
def respond_to_missing?(method, include_private = false)
  field_methods.include? method || super
end

Private Instance Methods

field_methods() click to toggle source
# File lib/cron/parser.rb, line 98
def field_methods
  self.fields.keys.map{|f| (f.to_s + "_field").to_sym }
end
fix_common_typos!() click to toggle source

Internal method to clean up the frequently made typos and mistakes in the cron pattern.

# File lib/cron/parser.rb, line 93
def fix_common_typos!
  # well, I don't know what kind of typos people do!
  @pattern.squish!
end
validate!() click to toggle source

Internal method to validate the cron pattern. Raises errors if pattern is invalid.

# File lib/cron/parser.rb, line 65
def validate!
  unless @pattern.kind_of? String
    raise InvalidCronPatternError.new("cron pattern must be a string".squish)
  end
  fix_common_typos! # how nasty!
  # do you know that cron has some pretty good fields, huh?
  # go and,
  validate_fields!
end
validate_fields!() click to toggle source

Internal method to validate each field in the pattern.

# File lib/cron/parser.rb, line 76
def validate_fields!
  pattern_fields = @pattern.split
  if pattern_fields.size != FIELDS.size
    raise InvalidCronPatternError.new("cron pattern must contain exact
                    #{FIELDS.size} fields seperated by whitespaces".squish)
  end
  FIELDS.map.with_index do |field, index|
    field_class           = self.class.name + "::" + field.split("_"). \
                            map(&:capitalize).join + "Field".squish.   \
                            classify
    @fields[field.to_sym] = field_class.safe_constantize.              \
                            new(pattern_fields[index])
  end
end