Class: AWSCronParser::Parsers::AWSRate

Inherits:
Base
  • Object
show all
Defined in:
lib/aws_cron_parser/parsers/aws_rate.rb

Instance Attribute Summary

Attributes inherited from Base

#original_source, #source, #time_source, #time_zone

Instance Method Summary collapse

Methods inherited from Base

#last

Methods included from Helpers

included

Constructor Details

#initialize(source, time_source = Time) ⇒ AWSRate

Returns a new instance of AWSRate.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/aws_cron_parser/parsers/aws_rate.rb', line 6

def initialize(source, time_source = Time)
  super(source, time_source)

  match = source.match(RATE_REGEX)
  raise ArgumentError, "Invalid rate format: #{source}" unless match

  value = match[1].to_i
  unit = match[2].downcase
  unit = unit.chomp('s') if unit.end_with?('s')

  raise ArgumentError, "Rate value must be positive: #{value}" if value <= 0

  @rate = { value: value, unit: unit }
end

Instance Method Details

#next(now = @time_source.now) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/aws_cron_parser/parsers/aws_rate.rb', line 21

def next(now = @time_source.now)
  current_time = ensure_time(now)
  seconds_to_add = case @rate[:unit]
                   when 'minute' then @rate[:value] * 60
                   when 'hour' then @rate[:value] * 3600
                   when 'day' then @rate[:value] * 86_400
                   else raise ArgumentError, "Unknown rate unit: #{@rate[:unit]}"
                   end
  current_time + seconds_to_add.seconds
end