lib/rufus/scheduler/cronline.rb in rufus-scheduler-1.0.13 vs lib/rufus/scheduler/cronline.rb in rufus-scheduler-1.0.14
- old
+ new
@@ -1,8 +1,7 @@
-#
#--
-# Copyright (c) 2006-2008, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2006-2009, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -17,18 +16,14 @@
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
-#++
#
+# Made in Japan.
+#++
-#
-# "made in Japan"
-#
-# John Mettraux at openwfe.org
-#
module Rufus
#
# A 'cron line' is a line in the sense of a crontab
@@ -164,127 +159,127 @@
time
end
private
- #--
- # adjust values to Ruby
+ #--
+ # adjust values to Ruby
+ #
+ #def adjust_arrays()
+ # @hours = @hours.collect { |h|
+ # if h == 24
+ # 0
+ # else
+ # h
+ # end
+ # } if @hours
+ # @weekdays = @weekdays.collect { |wd|
+ # wd - 1
+ # } if @weekdays
+ #end
#
- #def adjust_arrays()
- # @hours = @hours.collect { |h|
- # if h == 24
- # 0
- # else
- # h
- # end
- # } if @hours
- # @weekdays = @weekdays.collect { |wd|
- # wd - 1
- # } if @weekdays
- #end
- #
- # dead code, keeping it as a reminder
- #++
+ # dead code, keeping it as a reminder
+ #++
- WDS = [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ]
- #
- # used by parse_weekday()
+ WDS = %w[ sun mon tue wed thu fri sat ]
+ #
+ # used by parse_weekday()
- def parse_weekdays (item)
+ def parse_weekdays (item)
- item = item.downcase
+ item = item.downcase
- WDS.each_with_index do |day, index|
- item = item.gsub day, "#{index}"
- end
-
- r = parse_item item, 0, 7
-
- return r unless r.is_a?(Array)
-
- r.collect { |e| e == 7 ? 0 : e }.uniq
+ WDS.each_with_index do |day, index|
+ item = item.gsub day, "#{index}"
end
- def parse_item (item, min, max)
+ r = parse_item item, 0, 7
- return nil \
- if item == "*"
- return parse_list(item, min, max) \
- if item.index(",")
- return parse_range(item, min, max) \
- if item.index("*") or item.index("-")
+ return r unless r.is_a?(Array)
- i = Integer(item)
+ r.collect { |e| e == 7 ? 0 : e }.uniq
+ end
- i = min if i < min
- i = max if i > max
+ def parse_item (item, min, max)
- [ i ]
- end
+ return nil \
+ if item == "*"
+ return parse_list(item, min, max) \
+ if item.index(",")
+ return parse_range(item, min, max) \
+ if item.index("*") or item.index("-")
- def parse_list (item, min, max)
+ i = Integer(item)
- items = item.split(",")
+ i = min if i < min
+ i = max if i > max
- items.inject([]) { |r, i| r.push(parse_range(i, min, max)) }.flatten
- end
+ [ i ]
+ end
- def parse_range (item, min, max)
+ def parse_list (item, min, max)
- i = item.index("-")
- j = item.index("/")
+ items = item.split(",")
- return item.to_i if (not i and not j)
+ items.inject([]) { |r, i| r.push(parse_range(i, min, max)) }.flatten
+ end
- inc = 1
+ def parse_range (item, min, max)
- inc = Integer(item[j+1..-1]) if j
+ i = item.index("-")
+ j = item.index("/")
- istart = -1
- iend = -1
+ return item.to_i if (not i and not j)
- if i
+ inc = 1
- istart = Integer(item[0..i-1])
+ inc = Integer(item[j+1..-1]) if j
- if j
- iend = Integer(item[i+1..j])
- else
- iend = Integer(item[i+1..-1])
- end
+ istart = -1
+ iend = -1
- else # case */x
+ if i
- istart = min
- iend = max
+ istart = Integer(item[0..i-1])
+
+ if j
+ iend = Integer(item[i+1..j])
+ else
+ iend = Integer(item[i+1..-1])
end
- istart = min if istart < min
- iend = max if iend > max
+ else # case */x
- result = []
+ istart = min
+ iend = max
+ end
- value = istart
- loop do
+ istart = min if istart < min
+ iend = max if iend > max
- result << value
- value = value + inc
- break if value > iend
- end
+ result = []
- result
- end
+ value = istart
+ loop do
- def sub_match? value, values
- values.nil? || values.include?(value)
+ result << value
+ value = value + inc
+ break if value > iend
end
- def date_match? date
- return false unless sub_match? date.day, @days
- return false unless sub_match? date.month, @months
- return false unless sub_match? date.wday, @weekdays
- true
- end
+ result
+ end
+
+ def sub_match? value, values
+ values.nil? || values.include?(value)
+ end
+
+ def date_match? date
+ return false unless sub_match? date.day, @days
+ return false unless sub_match? date.month, @months
+ return false unless sub_match? date.wday, @weekdays
+ true
+ end
end
end