Class: Slop::DurationOption

Inherits:
Option
  • Object
show all
Defined in:
lib/httpdisk/slop_duration.rb

Overview

Custom duration type for Slop, used for –expires. Raises aggressively because this is a tricky and lightly documented option.

Constant Summary collapse

UNITS =
{
  s: 1,
  m: 60,
  h: 60 * 60,
  d: 24 * 60 * 60,
  w: 7 * 24 * 60 * 60,
  y: 365 * 7 * 24 * 60 * 60,
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Object

Raises:

  • (Slop::Error)


16
17
18
19
20
21
22
# File 'lib/httpdisk/slop_duration.rb', line 16

def call(value)
  m = value.match(/^(\d+)([smhdwy])?$/)
  raise Slop::Error, "invalid --expires #{value.inspect}" if !m

  num, unit = m[1].to_i, (m[2] || 's').to_sym
  num * UNITS[unit]
end