Class: CronTab

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

Defined Under Namespace

Classes: Field, FieldSet, NextSeeker, YearField

Constant Summary

WDAY =
%w(sun mon tue wed thu fri sat)
FormatError =
Class.new(StandardError)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CronTab) initialize(str)

A new instance of CronTab



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

def initialize(str)
  super()

  self.min, self.hour, self.mday, self.mon, self.wday =
    CronTab.parse_timedate(str)

  self.command = str.scan( /(?:\S+\s+){5}(.*)/ ).shift
end

Instance Attribute Details

- (Object) command

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



4
5
6
# File 'lib/CronTab.rb', line 4

def command
  @command
end

- (Object) hour

Returns the value of attribute hour

Returns:

  • (Object)

    the current value of hour



4
5
6
# File 'lib/CronTab.rb', line 4

def hour
  @hour
end

- (Object) mday

Returns the value of attribute mday

Returns:

  • (Object)

    the current value of mday



4
5
6
# File 'lib/CronTab.rb', line 4

def mday
  @mday
end

- (Object) min

Returns the value of attribute min

Returns:

  • (Object)

    the current value of min



4
5
6
# File 'lib/CronTab.rb', line 4

def min
  @min
end

- (Object) mon

Returns the value of attribute mon

Returns:

  • (Object)

    the current value of mon



4
5
6
# File 'lib/CronTab.rb', line 4

def mon
  @mon
end

- (Object) wday

Returns the value of attribute wday

Returns:

  • (Object)

    the current value of wday



4
5
6
# File 'lib/CronTab.rb', line 4

def wday
  @wday
end

Class Method Details

+ (Object) parse_field(str, first, last)



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/CronTab.rb', line 142

def self.parse_field(str, first, last)
  list = str.split(",")
  list.map!{|r|
    r, every = r.split("/")
    every = every ? every.to_i : 1
    f,l = r.split("-")
    range = if f == "*"
              first..last
            elsif l.nil?
              f.to_i .. f.to_i
            elsif f.to_i < first
              raise FormatError.new("out of range (#{f} for #{first})")
            elsif last < l.to_i
              raise FormatError.new("out of range (#{l} for #{last})")
            else
              f.to_i .. l.to_i
            end
    Field.new(range, every)
  }
  FieldSet.new(list)
end

+ (Object) parse_timedate(str)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/CronTab.rb', line 88

def self.parse_timedate(str)
  minute, hour, day_of_month, month, day_of_week =
    str.scan(/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/).shift

  day_of_week = day_of_week.downcase.gsub(/#{WDAY.join("|")}/){
    WDAY.index($&)
  }

  [
    parse_field(minute,       0, 59),
    parse_field(hour,         0, 23),
    parse_field(day_of_month, 1, 31),
    parse_field(month,        1, 12),
    parse_field(day_of_week,  0, 6),
  ]
end

Instance Method Details

- (Object) ===(rhs) Also known as: include?



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/CronTab.rb', line 17

def ===(rhs)
  judge_date = proc {
    b = true
    b = b && (mday === rhs.mday)
    b = b && (mon === rhs.mon)
    b = b && (wday === rhs.wday)
  }
  judge_hour = proc {
    b = true
    b = b && (min === rhs.min)
    b = b && (hour === rhs.hour)
  }

  case rhs
  when Time
    judge_hour.call && judge_date.call
  when Dpklib::Hour
    judge_hour.call
  when Date
    judge_date.call
  else
    super
  end
end

- (Object) nexttime(nowtime = Time.now)

/YearField



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/CronTab.rb', line 67

def nexttime(nowtime = Time.now)
  nowmin = nowtime.min + 1

  seeker_min = NextSeeker.new(nowmin, min, nil)
  seeker_hour = NextSeeker.new(nowtime.hour, hour, seeker_min)
  seeker_mday = NextSeeker.new(nowtime.mday, mday, seeker_hour)
  seeker_mon = NextSeeker.new(nowtime.mon, mon, seeker_mday)
  seeker_year = NextSeeker.new(nowtime.year, YearField.new, seeker_mon)
  seeker_year.succ

  Time.local(seeker_year.scalar,
             seeker_mon.scalar,
             seeker_mday.scalar,
             seeker_hour.scalar,
             seeker_min.scalar, 0)
end

- (Object) waitsec(nowtime = Time.now)



84
85
86
# File 'lib/CronTab.rb', line 84

def waitsec(nowtime = Time.now)
  nexttime(nowtime).to_i - nowtime.to_i
end