Module: Expire::FromRangeValue

Included in:
FromNowKeepAdjectiveForRuleBase, FromNowKeepMostRecentForRule, KeepMostRecentForRule
Defined in:
lib/expire/from_range_value.rb

Overview

Provide a pseudo constructor for rules that require a time range

Constant Summary collapse

FROM_VALUE_REGEX =
/
\A
(([0-9](_[0-9]+){0,})+)
(\s+|\.)
(hour|day|week|month|year)s?
\z
/x

Instance Method Summary collapse

Instance Method Details

#from_value(string, **args) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/expire/from_range_value.rb', line 16

def from_value(string, **args)
  # return new(args.merge({ amount: 0, unit: nil })) if string.none?
  return new(**args.merge(amount: 0, unit: nil)) if string.none?

  stripped_down = string.strip.downcase
  match = stripped_down.match FROM_VALUE_REGEX
  raise ArgumentError, "#{string} is not a valid range value" unless match

  amount = Integer(match[1])
  unit = match[5]
  new(**args.merge(amount: amount, unit: unit))
end