Module: Textus::Domain::Duration
- Defined in:
- lib/textus/domain/duration.rb
Overview
Parses a duration value into whole seconds. Accepts a bare integer (or integer-string) of seconds, or ‘<n><unit>` with unit s/m/h/d. Returns nil for nil or any unparseable value.
Constant Summary collapse
- UNIT_SECONDS =
{ "s" => 1, "m" => 60, "h" => 3600, "d" => 86_400 }.freeze
Class Method Summary collapse
Class Method Details
.seconds(value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/textus/domain/duration.rb', line 9 def self.seconds(value) return nil if value.nil? str = value.to_s.strip return str.to_i if str.match?(/\A\d+\z/) m = str.match(/\A(\d+)\s*([smhd])\z/) return nil unless m m[1].to_i * UNIT_SECONDS.fetch(m[2]) end |