Module: Jets::Event::Dsl::RateExpression

Included in:
CLI::Schedule::Base, ScheduledEvent
Defined in:
lib/jets/event/dsl/rate_expression.rb

Instance Method Summary collapse

Instance Method Details

#rate_expression(expr) ⇒ Object

normalizes the rate expression



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jets/event/dsl/rate_expression.rb', line 6

def rate_expression(expr)
  duration = Fugit::Duration.parse(expr)
  map = {
    sec: "second",
    min: "minute",
    hou: "hour",
    day: "day",
    wee: "week",
    mon: "month",
    yea: "year"
  }
  # duration.h has a hash like {:hou=>1}. unit is truncated to 3 characters
  h = duration.h # IE: {:hou=>1}
  value = h.values.first
  unit = h.keys.first
  unit = map[unit]
  # Fix the unit to be singular or plural for user
  unit = (value > 1) ? unit.pluralize : unit.singularize
  "#{value} #{unit}"
end