Class: Flightdeck::CronSchedule

Inherits:
Object
  • Object
show all
Defined in:
app/models/flightdeck/cron_schedule.rb

Overview

Turns the common cron shapes into a readable sentence.

Fugit parses cron but does not describe it, so this is ours. It only claims to understand patterns it genuinely does: anything else returns nil and the view shows the raw cron on its own. A confidently wrong description of a schedule is worse than no description.

Constant Summary collapse

DAYS =
%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].freeze
MONTHS =
%w[- January February March April May June July August September October November December].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ CronSchedule

Returns a new instance of CronSchedule.



16
17
18
# File 'app/models/flightdeck/cron_schedule.rb', line 16

def initialize(raw)
  @raw = raw.to_s.strip
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



14
15
16
# File 'app/models/flightdeck/cron_schedule.rb', line 14

def raw
  @raw
end

Class Method Details

.humanize(raw) ⇒ Object



20
# File 'app/models/flightdeck/cron_schedule.rb', line 20

def self.humanize(raw) = new(raw).to_human

Instance Method Details

#fieldsObject

Cron fields, plus an optional trailing timezone that Solid Queue allows.



23
24
25
26
27
28
# File 'app/models/flightdeck/cron_schedule.rb', line 23

def fields
  @fields ||= begin
    parts = raw.split(/\s+/)
    parts.size.between?(5, 6) ? parts : nil
  end
end

#timezoneObject



30
31
32
# File 'app/models/flightdeck/cron_schedule.rb', line 30

def timezone
  fields && fields.size == 6 ? fields[5] : nil
end

#to_humanObject



34
35
36
37
38
39
40
41
# File 'app/models/flightdeck/cron_schedule.rb', line 34

def to_human
  return nil unless fields

  minute, hour, day_of_month, month, day_of_week = fields.first(5)
  return nil unless simple?(minute, hour, day_of_month, month, day_of_week)

  [ frequency(minute, hour, day_of_month, month, day_of_week), timezone ].compact.join(" ").presence
end