Class: AbstractCrontabField

Inherits:
Object
  • Object
show all
Defined in:
lib/crontab_fields.rb

Constant Summary collapse

@@CRONTAB_FIELD_REGEX =
/^(\*|(\d)+)(-(\d+))?(\/(\d+))?$/
@@START_GROUP_NUM =
1
@@STOP_GROUP_NUM =
4
@@STEP_GROUP_NUM =
6
@@ASTERISK_FIELD_REGEX =
/^\*(\/(\d+))?$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, stop = nil, step = nil) ⇒ AbstractCrontabField

Returns a new instance of AbstractCrontabField.



8
9
10
11
12
13
14
15
# File 'lib/crontab_fields.rb', line 8

def initialize(start, stop=nil, step=nil)
  @start = start.to_i
  @stop = stop.nil? ? @start : stop.to_i
  @step = step.nil? ? 1 : step.to_i
  if @step > 1 and @stop == @start
    raise "Stepping must be used with ranges or asterisk only"
  end
end

Class Method Details

.create_from_string(field_string) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/crontab_fields.rb', line 16

def self.create_from_string(field_string)
  md = @@CRONTAB_FIELD_REGEX.match(field_string)
  if md
    asterisk_md = @@ASTERISK_FIELD_REGEX.match(md[@@START_GROUP_NUM])
    if asterisk_md
      step = md[@@STEP_GROUP_NUM]
      CrontabAsterisk.new(step)
    else
      start = md[@@START_GROUP_NUM]
      stop = md[@@STOP_GROUP_NUM]
      step = md[@@STEP_GROUP_NUM]
      new(start, stop, step)
    end
  else
    raise "Can't parse crontab field \"#{field_string}\""
  end
end

Instance Method Details

#to_sObject



33
34
35
36
37
38
# File 'lib/crontab_fields.rb', line 33

def to_s
  as_s = @start.to_s
  as_s += "-#{@stop}" if @stop > @start
  as_s += "/#{@step}" if @step > 1
  as_s
end