Sha256: 80352be19063795fb41d7ec58b91704561811bd14e5a60d156a9f01f29cac44a

Contents?: true

Size: 772 Bytes

Versions: 1

Compression:

Stored size: 772 Bytes

Contents

# frozen_string_literal: true

module Expire
  # Provide a pseudo constructor for rules that require a time range
  module FromRangeValue
    using RefineAllAndNone

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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
expire-0.2.0 lib/expire/from_range_value.rb