Module: Twuckoo::DurationString

Defined in:
lib/twuckoo/duration_string.rb

Constant Summary collapse

MULTIPLIERS =
{ "s" => 1, "m" => 60, "h" => 60 * 60, "d" => 60 * 60 * 24, "w" => 60 * 60 * 24 * 7 }

Class Method Summary collapse

Class Method Details

.to_seconds(duration) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/twuckoo/duration_string.rb', line 5

def to_seconds(duration)
  duration.scan(/(\d+)([smhdw])/).inject(0) do |seconds, match|
    num, dur_chr = match
    multiplier = MULTIPLIERS[dur_chr]
    seconds + num.to_i * multiplier
  end
end