Sha256: 3c5028174a690b49c9c8491ca056ce21b4b2d3c88ffaa6d57b4f82dc04e51d24

Contents?: true

Size: 485 Bytes

Versions: 3

Compression:

Stored size: 485 Bytes

Contents

# frozen_string_literal: true

module Attractor
  # converts a duration string into an amount of days
  class DurationParser
    TOKENS = {
      "d" => 1,
      "w" => 7,
      "m" => 30,
      "y" => 365
    }.freeze

    attr_reader :duration

    def initialize(input)
      @input = input
      @duration = 0
      parse
    end

    def parse
      @input.scan(/(\d+)(\w)/).each do |amount, measure|
        @duration += amount.to_i * TOKENS[measure]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attractor-2.3.0 lib/attractor/duration_parser.rb~
attractor-2.2.0 lib/attractor/duration_parser.rb
attractor-2.1.0 lib/attractor/duration_parser.rb