module KingKonf::DurationDecoder

Decodes specially formatted duration strings.

Constants

PART

Either a number or a time unit.

UNITS
VALID_DURATION

One or more parts, possibly separated by whitespace.

Public Class Methods

decode(value) click to toggle source
# File lib/king_konf/duration_decoder.rb, line 20
def self.decode(value)
  if value !~ VALID_DURATION
    raise ConfigError, "#{value.inspect} is not a duration: must be e.g. `1h 30m`"
  end

  value.scan(PART).flatten.each_slice(2).map {|number, letter|
    number.to_i * UNITS.fetch(letter)
  }.sum
end