Class: SayWhen::CronExpression
- Inherits:
-
Object
- Object
- SayWhen::CronExpression
- Defined in:
- lib/say_when/cron_expression.rb
Overview
Based on the extended cron capabilties www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-06.html
Instance Attribute Summary collapse
-
#days_of_month ⇒ Object
Returns the value of attribute days_of_month.
-
#days_of_week ⇒ Object
Returns the value of attribute days_of_week.
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#hours ⇒ Object
Returns the value of attribute hours.
-
#minutes ⇒ Object
Returns the value of attribute minutes.
-
#months ⇒ Object
Returns the value of attribute months.
-
#seconds ⇒ Object
Returns the value of attribute seconds.
-
#time_zone ⇒ Object
Returns the value of attribute time_zone.
-
#years ⇒ Object
Returns the value of attribute years.
Instance Method Summary collapse
-
#initialize(expression = {}, time_zone = nil) ⇒ CronExpression
constructor
A new instance of CronExpression.
- #last_fire_at(time = nil) ⇒ Object
- #next_fire_at(time = nil) ⇒ Object
- #parse ⇒ Object
- #to_s ⇒ Object
- #validate ⇒ Object
- #will_fire_on?(date) ⇒ Boolean
Constructor Details
#initialize(expression = {}, time_zone = nil) ⇒ CronExpression
Returns a new instance of CronExpression.
12 13 14 15 16 17 18 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/say_when/cron_expression.rb', line 12 def initialize(expression = {}, time_zone = nil) if expression.is_a?(Hash) opts = expression @expression = if opts[:expression] opts[:expression] else [:days_of_month, :days_of_week].each do |f| opts[f] ||= '?' end [:seconds, :minutes, :hours, :days_of_month, :months, :days_of_week, :years].each do |f| opts[f] ||= '*' end "#{opts[:seconds]} #{opts[:minutes]} #{opts[:hours]} #{opts[:days_of_month]} #{opts[:months]} #{opts[:days_of_week]} #{opts[:years]}" end @time_zone = opts[:time_zone] else @expression = expression end @time_zone ||= time_zone if @time_zone.blank? @time_zone = Time.zone.try(:name) || "UTC" end parse validate end |
Instance Attribute Details
#days_of_month ⇒ Object
Returns the value of attribute days_of_month.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def days_of_month @days_of_month end |
#days_of_week ⇒ Object
Returns the value of attribute days_of_week.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def days_of_week @days_of_week end |
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
9 10 11 |
# File 'lib/say_when/cron_expression.rb', line 9 def expression @expression end |
#hours ⇒ Object
Returns the value of attribute hours.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def hours @hours end |
#minutes ⇒ Object
Returns the value of attribute minutes.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def minutes @minutes end |
#months ⇒ Object
Returns the value of attribute months.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def months @months end |
#seconds ⇒ Object
Returns the value of attribute seconds.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def seconds @seconds end |
#time_zone ⇒ Object
Returns the value of attribute time_zone.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def time_zone @time_zone end |
#years ⇒ Object
Returns the value of attribute years.
10 11 12 |
# File 'lib/say_when/cron_expression.rb', line 10 def years @years end |
Instance Method Details
#last_fire_at(time = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/say_when/cron_expression.rb', line 85 def last_fire_at(time=nil) Time.zone = time_zone before = time.nil? ? Time.zone.now : time.in_time_zone(@time_zone) while (true) [years, months, days_of_month, days_of_week, hours, minutes, seconds].each do |cron_value| before, changed = move_to_last(cron_value, before) return if before.nil? break if changed end break if will_fire_on?(before) end before end |
#next_fire_at(time = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/say_when/cron_expression.rb', line 69 def next_fire_at(time=nil) Time.zone = time_zone after = time.nil? ? Time.zone.now : time.in_time_zone(@time_zone) while (true) [years, months, days_of_month, days_of_week, hours, minutes, seconds].each do |cron_value| after, changed = move_to_next(cron_value, after) return if after.nil? break if changed end break if will_fire_on?(after) end after end |
#parse ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/say_when/cron_expression.rb', line 44 def parse return if expression.blank? vals = expression.split.map{ |word| word.upcase.gsub(/\s/, '') } self.seconds = SecondsCronValue.new(vals[0]) self.minutes = MinutesCronValue.new(vals[1]) self.hours = HoursCronValue.new(vals[2]) self.days_of_month = DaysOfMonthCronValue.new(vals[3]) self.months = MonthsCronValue.new(vals[4]) self.days_of_week = DaysOfWeekCronValue.new(vals[5]) self.years = YearsCronValue.new(vals[6] || "*") end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/say_when/cron_expression.rb', line 61 def to_s "s:#{seconds}m:#{minutes}h:#{hours}dom:#{days_of_month}m:#{months}dow:#{days_of_week}y:#{years}" end |
#validate ⇒ Object
56 57 58 59 |
# File 'lib/say_when/cron_expression.rb', line 56 def validate return if expression.blank? raise "days_of_week or days_of_month needs to be ?" if (days_of_month.is_specified && days_of_week.is_specified) end |
#will_fire_on?(date) ⇒ Boolean
65 66 67 |
# File 'lib/say_when/cron_expression.rb', line 65 def will_fire_on?(date) [seconds, minutes, hours, days_of_month, months, days_of_week, years].detect { |part| !part.include?(date) }.nil? end |