Sha256: 6d9abfc6d3d47b9e3da096fe10881181c6e7e9362b0143fd6f152d5cd4e56dd1

Contents?: true

Size: 835 Bytes

Versions: 10

Compression:

Stored size: 835 Bytes

Contents

# Copied from https://github.com/Shopify/spy/blob/ac7bfb9550bfd7bafd191bc31f1bcd9dc4ce9ee6/lib/spy/duration.rb
# and edited accordingly

require 'active_support/core_ext/module'
require 'active_support/core_ext/integer'

module Duration
  DURATION_FORMAT = /
    \A
    (?<days>\d+d)?
    (?<hours>\d+h)?
    (?<minutes>\d+m)?
    (?<seconds>\d+s)?
    \z
  /x
  DURATION_UNITS = {
    's' => :seconds,
    'm' => :minutes,
    'h' => :hours,
    'd' => :days,
  }

  def self.parse(value)
    unless match = DURATION_FORMAT.match(value)
      raise ArgumentError, "not a duration: #{value.inspect}, "\
      "use digits followed by a unit (#{DURATION_UNITS.map { |k, v| "#{k} for #{v}" }.join(', ')})"
    end
    DURATION_UNITS.values.inject(0) do |sum, unit|
      sum + match[unit].to_i.public_send(unit)
    end.seconds
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
doggy-2.0.42 lib/doggy/duration.rb
doggy-2.0.41 lib/doggy/duration.rb
doggy-2.0.40 lib/doggy/duration.rb
doggy-2.0.39 lib/doggy/duration.rb
doggy-2.0.38 lib/doggy/duration.rb
doggy-2.0.37 lib/doggy/duration.rb
doggy-2.0.36 lib/doggy/duration.rb
doggy-2.0.35 lib/doggy/duration.rb
doggy-2.0.34 lib/doggy/duration.rb
doggy-2.0.33 lib/doggy/duration.rb