Class: Attractor::DurationParser
- Inherits:
-
Object
- Object
- Attractor::DurationParser
- 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
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
Instance Method Summary collapse
-
#initialize(input) ⇒ DurationParser
constructor
A new instance of DurationParser.
- #parse ⇒ Object
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
#duration ⇒ Object (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
#parse ⇒ Object
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 |