Class: Chrono::NextTime

Inherits:
Object
  • Object
show all
Defined in:
lib/chrono/next_time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ NextTime

Returns a new instance of NextTime.



18
19
20
21
# File 'lib/chrono/next_time.rb', line 18

def initialize(options)
  @now = options[:now]
  @source = options[:source]
end

Instance Attribute Details

#nowObject (readonly)

Returns the value of attribute now.



14
15
16
# File 'lib/chrono/next_time.rb', line 14

def now
  @now
end

#sourceObject (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/chrono/next_time.rb', line 14

def source
  @source
end

#time=(value) ⇒ Object

Sets the attribute time

Parameters:

  • value

    the value to set the attribute time to.



16
17
18
# File 'lib/chrono/next_time.rb', line 16

def time=(value)
  @time = value
end

Instance Method Details

#to_timeObject

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chrono/next_time.rb', line 23

def to_time
  # the longest cycle is 4 years (leap year)
  # Note that the combination of day-month and wday is OR
  max_time = time + (365 * 3 + 366).days
  while @time < max_time
    case
    when !scheduled_in_this_month?
      carry_month
    when !scheduled_in_this_day?
      carry_day
    when !scheduled_in_this_hour?
      carry_hour
    when !scheduled_in_this_minute?
      carry_minute
    else
      return @time
    end
  end
  raise ArgumentError, "invalid cron string '#{@source}'"
end