Class: Attractor::DurationParser

Inherits:
Object
  • Object
show all
Defined in:
lib/attractor/duration_parser.rb

Overview

converts a duration string into an amount of days

Constant Summary collapse

TOKENS =
{
  "d" => 1,
  "w" => 7,
  "m" => 30,
  "y" => 365
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ DurationParser

Returns a new instance of DurationParser.



15
16
17
18
19
20
21
# File 'lib/attractor/duration_parser.rb', line 15

def initialize(input)
  @input = input
  @duration = @input.is_a?(Numeric) ? @input : 0
  return if @duration > 0

  parse
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



13
14
15
# File 'lib/attractor/duration_parser.rb', line 13

def duration
  @duration
end

Instance Method Details

#parseObject



23
24
25
26
27
# File 'lib/attractor/duration_parser.rb', line 23

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