Module: Btape::Duration

Defined in:
lib/btape/duration.rb

Overview

The duration literals .tape scripts use ("500ms", "1.5s"). The parser, the runner and Settings all need the same format, so it lives here once rather than as a regexp repeated at each call site.

Constant Summary collapse

PATTERN =
/\A(\d+(?:\.\d+)?)(ms|s)\z/
DESCRIPTION =
'must use ms or s (for example, 500ms or 1.5s)'

Class Method Summary collapse

Class Method Details

.parse(value) ⇒ Object

Returns the duration in seconds.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/btape/duration.rb', line 18

def parse(value)
  match = PATTERN.match(value.to_s)
  raise ArgumentError, "duration #{DESCRIPTION}" unless match

  amount, unit = match.captures
  amount.to_f / (unit == 'ms' ? 1000 : 1)
end

.valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/btape/duration.rb', line 13

def valid?(value)
  PATTERN.match?(value.to_s)
end