Class: SayWhen::CronExpression

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

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_monthObject

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_weekObject

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

#expressionObject (readonly)

Returns the value of attribute expression.



9
10
11
# File 'lib/say_when/cron_expression.rb', line 9

def expression
  @expression
end

#hoursObject

Returns the value of attribute hours.



10
11
12
# File 'lib/say_when/cron_expression.rb', line 10

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes.



10
11
12
# File 'lib/say_when/cron_expression.rb', line 10

def minutes
  @minutes
end

#monthsObject

Returns the value of attribute months.



10
11
12
# File 'lib/say_when/cron_expression.rb', line 10

def months
  @months
end

#secondsObject

Returns the value of attribute seconds.



10
11
12
# File 'lib/say_when/cron_expression.rb', line 10

def seconds
  @seconds
end

#time_zoneObject

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

#yearsObject

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

#parseObject



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_sObject



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

#validateObject



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

Returns:

  • (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