Class: Crono::Period

Inherits:
Object
  • Object
show all
Defined in:
lib/crono/period.rb

Overview

Period describe frequency of jobs

Constant Summary collapse

DAYS =
[:monday, :tuesday, :wednesday, :thursday, :friday, :saturday,
:sunday]

Instance Method Summary collapse

Constructor Details

#initialize(period, at: nil, on: nil, within: nil) ⇒ Period

Returns a new instance of Period.



7
8
9
10
11
12
# File 'lib/crono/period.rb', line 7

def initialize(period, at: nil, on: nil, within: nil)
  @period = period
  @at_hour, @at_min = parse_at(at) if at
  @interval = Interval.parse(within) if within
  @on = parse_on(on) if on
end

Instance Method Details

#descriptionObject



33
34
35
36
37
38
39
# File 'lib/crono/period.rb', line 33

def description
  desc = "every #{@period.inspect}"
  desc += " between #{@interval.from} and #{@interval.to} UTC" if @interval
  desc += format(' at %.2i:%.2i', @at_hour, @at_min) if @at_hour && @at_min
  desc += " on #{DAYS[@on].capitalize}" if @on
  desc
end

#next(since: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/crono/period.rb', line 14

def next(since: nil)
  if @interval
    if since
      @next = @interval.next_within(since, @period)
    else
      return initial_next if @interval.within?(initial_next)
      @next = @interval.next_within(initial_next, @period)
    end
  else
    return initial_next unless since
    @next = @period.since(since)
  end

  @next = @next.beginning_of_week.advance(days: @on) if @on
  @next = @next.change(time_atts)
  return @next if @next.future?
  Time.now
end