Class: Longleaf::ServiceDateHelper
- Inherits:
-
Object
- Object
- Longleaf::ServiceDateHelper
- Defined in:
- lib/longleaf/helpers/service_date_helper.rb
Overview
Helper methods for interacting with dates/timestamps on services
Class Method Summary collapse
-
.add_to_timestamp(timestamp, modifier) ⇒ String
Adds the amount of time from modifier to the provided timestamp “<quantity> <time unit>”, where quantity must be a positive whole number and time unit must be second, minute, hour, day, week, month or year (unit may be plural).
-
.formatted_timestamp(timestamp = Time.now) ⇒ String
Get a timestamp in the format expected for service timestamps.
-
.next_run_needed(md_rec, service_def) ⇒ String
Get the timestamp for the next time the provided service would need to be run for the object described by md_rec.
Class Method Details
.add_to_timestamp(timestamp, modifier) ⇒ String
Adds the amount of time from modifier to the provided timestamp “<quantity> <time unit>”, where quantity must be a positive whole number and time unit must be second, minute, hour, day, week, month or year (unit may be plural). Any info after a comma will be ignored.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/longleaf/helpers/service_date_helper.rb', line 13 def self.(, modifier) if modifier =~ /^(\d+) *(second|minute|hour|day|week|month|year)s?(,.*)?/ value = $1.to_i unit = $2 else raise ArgumentError.new("Cannot parse time modifier #{modifier}") end datetime = Time.iso8601() case unit when 'second' unit_modifier = 1 when 'minute' unit_modifier = 60 when 'hour' unit_modifier = 3600 when 'day' unit_modifier = 24 * 3600 when 'week' unit_modifier = 7 * 24 * 3600 when 'month' unit_modifier = 30 * 24 * 3600 when 'year' unit_modifier = 365 * 24 * 3600 end modified_time = datetime + (value * unit_modifier) modified_time.iso8601(3) end |
.formatted_timestamp(timestamp = Time.now) ⇒ String
Get a timestamp in the format expected for service timestamps.
46 47 48 |
# File 'lib/longleaf/helpers/service_date_helper.rb', line 46 def self.( = Time.now) .utc.iso8601(3).to_s end |
.next_run_needed(md_rec, service_def) ⇒ String
Get the timestamp for the next time the provided service would need to be run for the object described by md_rec
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/longleaf/helpers/service_date_helper.rb', line 56 def self.next_run_needed(md_rec, service_def) raise ArgumentError.new('Must provide a md_rec parameter') if md_rec.nil? raise ArgumentError.new('Must provide a service_def parameter') if service_def.nil? service_name = service_def.name service_rec = md_rec.service(service_name) if service_rec.nil? || service_rec..nil? if service_def.delay.nil? return md_rec.registered else return ServiceDateHelper.(md_rec.registered, service_def.delay) end end if service_def.frequency.nil? return nil else return ServiceDateHelper.(service_rec., service_def.frequency) end end |