Class: CronTab

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

Defined Under Namespace

Classes: Field, FieldSet, NextSeeker, YearField

Constant Summary collapse

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

#initialize(str) ⇒ CronTab

Returns a new instance of CronTab.



10
11
12
13
14
15
16
17
# File 'lib/cron_tab.rb', line 10

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

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/cron_tab.rb', line 5

def command
  @command
end

#hourObject

Returns the value of attribute hour.



5
6
7
# File 'lib/cron_tab.rb', line 5

def hour
  @hour
end

#mdayObject

Returns the value of attribute mday.



5
6
7
# File 'lib/cron_tab.rb', line 5

def mday
  @mday
end

#minObject

Returns the value of attribute min.



5
6
7
# File 'lib/cron_tab.rb', line 5

def min
  @min
end

#monObject

Returns the value of attribute mon.



5
6
7
# File 'lib/cron_tab.rb', line 5

def mon
  @mon
end

#wdayObject

Returns the value of attribute wday.



5
6
7
# File 'lib/cron_tab.rb', line 5

def wday
  @wday
end

Class Method Details

.parse_field(str, first, last) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/cron_tab.rb', line 165

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

.parse_timedate(str) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cron_tab.rb', line 98

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

#===(rhs) ⇒ Object Also known as: include?



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

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

#nexttime(nowtime = Time.now) ⇒ Object

/YearField



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cron_tab.rb', line 77

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

#waitsec(nowtime = Time.now) ⇒ Object



94
95
96
# File 'lib/cron_tab.rb', line 94

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