Class: Crontab::Schedule
- Inherits:
-
Object
- Object
- Crontab::Schedule
- Includes:
- Enumerable
- Defined in:
- lib/crontab/schedule.rb
Overview
A class which represents schedules in crontab(5).
Constant Summary collapse
- MONTH_NAMES =
%w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
- DAY_OF_WEEK_NAMES =
%w(Sun Mon Tue Wed Thu Fri Sat)
- DAYS_IN_MONTH =
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Instance Attribute Summary collapse
-
#day_of_months ⇒ Object
readonly
Returns the value of attribute day_of_months.
-
#day_of_weeks ⇒ Object
readonly
Returns the value of attribute day_of_weeks.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#start ⇒ Object
Returns the value of attribute start.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#each ⇒ Object
Iterates over timings specified in this schedule, starting at
@start
. -
#from(time_or_date) ⇒ Object
Creates new schedule which starts at the given time_or_date.
- #hash ⇒ Object
-
#initialize(spec, start = Time.now) ⇒ Schedule
constructor
Creates a cron job schedule.
-
#until(time_or_date) ⇒ Object
Iterates over timings from
@start
until given time_or_date.
Constructor Details
#initialize(spec, start = Time.now) ⇒ Schedule
Creates a cron job schedule.
-
spec
schedule specifier defined in crontab(5). Both numeric and named spec are supported. -
start
the time or date this schedule begins.
Supported named specs are:
-
@yearly
,@annually
-
@monthly
-
@weekly
-
@daily
,@midnight
-
@hourly
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/crontab/schedule.rb', line 24 def initialize(spec, start=Time.now) raise ArgumentError, 'empty spec' if spec == '' or spec.nil? @start = ensure_time(start) spec = spec.strip if spec.start_with?('@') parse_symbolic_spec(spec) else parse_spec(spec) end end |
Instance Attribute Details
#day_of_months ⇒ Object (readonly)
Returns the value of attribute day_of_months.
50 51 52 |
# File 'lib/crontab/schedule.rb', line 50 def day_of_months @day_of_months end |
#day_of_weeks ⇒ Object (readonly)
Returns the value of attribute day_of_weeks.
50 51 52 |
# File 'lib/crontab/schedule.rb', line 50 def day_of_weeks @day_of_weeks end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
50 51 52 |
# File 'lib/crontab/schedule.rb', line 50 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
50 51 52 |
# File 'lib/crontab/schedule.rb', line 50 def minutes @minutes end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
50 51 52 |
# File 'lib/crontab/schedule.rb', line 50 def months @months end |
#start ⇒ Object
Returns the value of attribute start.
50 51 52 |
# File 'lib/crontab/schedule.rb', line 50 def start @start end |
Instance Method Details
#==(other) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/crontab/schedule.rb', line 35 def ==(other) self.minutes == other.minutes && self.hours == other.hours && self.day_of_months == other.day_of_months && self.months == other.months && self.day_of_months_given? == other.day_of_months_given? && self.day_of_weeks == other.day_of_weeks && self.day_of_weeks_given? == other.day_of_weeks_given? && self.start == other.start end |
#each ⇒ Object
Iterates over timings specified in this schedule, starting at @start
.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/crontab/schedule.rb', line 73 def each return to_enum unless block_given? year = @start.year seeking = Hash.new {|h,k| h[k] = true } loop do @months.each do |month| next if seeking[:month] and month < @start.month and @start.month <= @months.max seeking[:month] = false days = matching_days(year, month) days.each do |day_of_month| next if seeking[:day_of_month] and day_of_month < @start.day and @start.day <= days.max seeking[:day_of_month] = false @hours.each do |hour| next if seeking[:hour] and hour < @start.hour and @start.hour <= @hours.max seeking[:hour] = false @minutes.each do |minute| begin t = Time.local(year, month, day_of_month, hour, minute) rescue ArgumentError raise StopIteration end yield(t) if @start <= t end end end end year += 1 end end |
#from(time_or_date) ⇒ Object
Creates new schedule which starts at the given time_or_date.
58 59 60 |
# File 'lib/crontab/schedule.rb', line 58 def from(time_or_date) self.dup.tap {|new_schedule| new_schedule.start = ensure_time(time_or_date) } end |
#hash ⇒ Object
46 47 48 |
# File 'lib/crontab/schedule.rb', line 46 def hash [ minutes, hours, day_of_months, months, day_of_weeks, day_of_months_given?, day_of_weeks_given?, start ].map(&:hash).inject(:^) end |
#until(time_or_date) ⇒ Object
Iterates over timings from @start
until given time_or_date.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/crontab/schedule.rb', line 106 def until(time_or_date) time = ensure_time(time_or_date) if block_given? each do |t| break if time < t yield(t) end else inject([]) do |timings, t| break timings if time < t timings << t end end end |