Module: Cosmo::Utils::Duration

Defined in:
lib/cosmo/utils/duration.rb,
sig/cosmo/utils/duration.rbs

Overview

Parses human-friendly duration strings ("20s", "1h", "7d") into seconds, so config values don't have to be spelled out as bare, uncommented integers. Numbers pass through unchanged, since existing config already uses plain seconds.

Constant Summary collapse

UNITS =

Returns:

  • (::Hash[::String, ::Integer])
{
  "s" => 1,
  "m" => 60,
  "h" => 3600,
  "d" => 86_400,
  "w" => 604_800,
  "mo" => 2_592_000, # 30 days
  "y" => 31_536_000
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(value) ⇒ Numeric

Parameters:

  • value (Numeric, ::String)

Returns:

  • (Numeric)

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/cosmo/utils/duration.rb', line 21

def parse(value)
  return value if value.is_a?(Numeric)

  str = value.to_s
  match = str.match(/\A(\d+)(mo|[smhdwy])\z/)
  return match[1].to_i * UNITS[match[2]] if match
  return str.to_f if str.match?(/\A\d+(\.\d+)?\z/)

  raise ArgumentError, "invalid duration: #{value}"
end

Instance Method Details

#parse(value) ⇒ Numeric

Parameters:

  • value (Numeric, ::String)

Returns:

  • (Numeric)

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/cosmo/utils/duration.rb', line 21

def parse(value)
  return value if value.is_a?(Numeric)

  str = value.to_s
  match = str.match(/\A(\d+)(mo|[smhdwy])\z/)
  return match[1].to_i * UNITS[match[2]] if match
  return str.to_f if str.match?(/\A\d+(\.\d+)?\z/)

  raise ArgumentError, "invalid duration: #{value}"
end