Class: Allora::CronLine
- Inherits:
-
Object
- Object
- Allora::CronLine
- Defined in:
- lib/allora/cron_line.rb
Overview
Parses a crontab string to determine the times it represents.
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#monthdays ⇒ Object
readonly
Returns the value of attribute monthdays.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
-
#timezone ⇒ Object
readonly
Returns the value of attribute timezone.
-
#weekdays ⇒ Object
readonly
Returns the value of attribute weekdays.
Instance Method Summary collapse
-
#initialize(line) ⇒ CronLine
constructor
A new instance of CronLine.
-
#next_time(time) ⇒ Time
Returns the next time that this cron line is supposed to ‘fire’.
- #to_array ⇒ Object
Constructor Details
#initialize(line) ⇒ CronLine
Returns a new instance of CronLine.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/allora/cron_line.rb', line 38 def initialize(line) items = line.split raise ArgumentError.new("not a valid cronline : '#{line}'") \ unless items.length == 5 or items.length == 6 offset = items.length - 5 @seconds = offset == 1 ? parse_item(items[0], 0, 59) : [0] @minutes = parse_item(items[0 + offset], 0, 59) @hours = parse_item(items[1 + offset], 0, 24) @days = parse_item(items[2 + offset], 1, 31) @months = parse_item(items[3 + offset], 1, 12) @weekdays, @monthdays = parse_weekdays(items[4 + offset]) end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
32 33 34 |
# File 'lib/allora/cron_line.rb', line 32 def days @days end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
31 32 33 |
# File 'lib/allora/cron_line.rb', line 31 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
30 31 32 |
# File 'lib/allora/cron_line.rb', line 30 def minutes @minutes end |
#monthdays ⇒ Object (readonly)
Returns the value of attribute monthdays.
35 36 37 |
# File 'lib/allora/cron_line.rb', line 35 def monthdays @monthdays end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
33 34 35 |
# File 'lib/allora/cron_line.rb', line 33 def months @months end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
29 30 31 |
# File 'lib/allora/cron_line.rb', line 29 def seconds @seconds end |
#timezone ⇒ Object (readonly)
Returns the value of attribute timezone.
36 37 38 |
# File 'lib/allora/cron_line.rb', line 36 def timezone @timezone end |
#weekdays ⇒ Object (readonly)
Returns the value of attribute weekdays.
34 35 36 |
# File 'lib/allora/cron_line.rb', line 34 def weekdays @weekdays end |
Instance Method Details
#next_time(time) ⇒ Time
Returns the next time that this cron line is supposed to ‘fire’
Note that the time instance returned will be in the same time zone that the given start point Time (thus a result in the local time zone will be passed if no start time is specified (search start time set to Time.now))
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/allora/cron_line.rb', line 81 def next_time(time) # little adjustment before starting time = time + 1 loop do unless date_match?(time) time += (24 - time.hour) * 3600 - time.min * 60 - time.sec next end unless sub_match?(time.hour, @hours) time += (60 - time.min) * 60 - time.sec next end unless sub_match?(time.min, @minutes) time += 60 - time.sec next end unless sub_match?(time.sec, @seconds) time += 1 next end break end time end |
#to_array ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/allora/cron_line.rb', line 109 def to_array [ @seconds, @minutes, @hours, @days, @months, @weekdays, @monthdays ] end |