Class: Moment::CronTrigger

Inherits:
Trigger
  • Object
show all
Defined in:
lib/cron_trigger.rb

Constant Summary collapse

LeapYearMonthDays =
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
CommonYearMonthDays =
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr) ⇒ CronTrigger

Returns a new instance of CronTrigger.



8
9
10
11
# File 'lib/cron_trigger.rb', line 8

def initialize(expr)
  super()
  self.cron_expr = expr
end

Instance Attribute Details

#cron_exprObject

Returns the value of attribute cron_expr.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def cron_expr
  @cron_expr
end

#dayObject

Returns the value of attribute day.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def day
  @day
end

#hourObject

Returns the value of attribute hour.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def hour
  @hour
end

#minObject

Returns the value of attribute min.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def min
  @min
end

#monthObject

Returns the value of attribute month.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def month
  @month
end

#secObject

Returns the value of attribute sec.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def sec
  @sec
end

#wdayObject

Returns the value of attribute wday.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def wday
  @wday
end

#yearObject

Returns the value of attribute year.



6
7
8
# File 'lib/cron_trigger.rb', line 6

def year
  @year
end

Instance Method Details

#fire_time_after(time) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
# File 'lib/cron_trigger.rb', line 19

def fire_time_after(time)
  sec, min, hour, day, month, year, wday, yday, isdst, zone = time.to_a

  loop do
    # year
    unless @year.nil? or @year.include?(year)
      return nil  if year > @year.max
      year = @year.detect do |y| y > year end  # next allowable year
    end

    # month
    unless @month.include?(month)
      # next allowable month
      next_month = @month.detect(lambda { @month.min }) do |m| m > month end
      # reset everything lower
      day, hour, min, sec = @day.min, @hour.min, @min.min, @sec.min
      # carry case
      if next_month < month
        month = next_month
        year += 1
        retry
      end
      month = next_month
    end

    # day
    month_days = (1 .. month_days(year, month))
    days = @day.select do |d| month_days === d end
    unless days.include?(day)
      next_day = days.detect(lambda { days.min }) do |d| d > day end
      hour, min, sec = @hour.min, @min.min, @sec.min
      if next_day.nil? or next_day < day
        day = next_day.nil? ? @day.min : next_day
        month += 1
        retry
      end
      day = next_day
    end

    # hour
    unless @hour.include?(hour)
      next_hour = @hour.detect(lambda { @hour.min }) do |h| h > hour end
      min, sec = @min.min, @sec.min
      if next_hour < hour
        hour = next_hour
        day += 1
        retry
      end
      hour = next_hour
    end

    # min
    unless @min.include?(min)
      next_min = @min.detect(lambda { @min.min }) do |m| m > min end
      sec = @sec.min
      if next_min < min
        min = next_min
        hour += 1
        retry
      end
      min = next_min
    end

    # sec
    unless @sec.include?(sec)
      next_sec = @sec.detect(lambda { @sec.min }) do |s| s > sec end
      if next_sec < sec
        sec = next_sec
        min += 1
        retry
      end
      sec = next_sec
    end

    break
  end

  Time.local sec, min, hour, day, month, year, wday, yday, isdst, zone
end