Class: GoDuration::Parser
- Inherits:
-
Object
- Object
- GoDuration::Parser
- Defined in:
- lib/go_duration/parser.rb
Constant Summary collapse
- PART_REGEXP =
%r{\d+[hms]}.freeze
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(input) ⇒ Parser
constructor
A new instance of Parser.
- #parts ⇒ Object
- #seconds ⇒ Object
Constructor Details
#initialize(input) ⇒ Parser
Returns a new instance of Parser.
15 16 17 |
# File 'lib/go_duration/parser.rb', line 15 def initialize(input) @input = input end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
13 14 15 |
# File 'lib/go_duration/parser.rb', line 13 def input @input end |
Class Method Details
.parse_part(part) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/go_duration/parser.rb', line 5 def self.parse_part(part) case when part.end_with?(SECOND_SUFFIX) then part.to_i when part.end_with?(MINUTE_SUFFIX) then part.to_i * MINUTE when part.end_with?(HOUR_SUFFIX) then part.to_i * HOUR end end |
Instance Method Details
#parts ⇒ Object
23 24 25 |
# File 'lib/go_duration/parser.rb', line 23 def parts @parts ||= input.scan(PART_REGEXP) end |
#seconds ⇒ Object
19 20 21 |
# File 'lib/go_duration/parser.rb', line 19 def seconds parts.map { |part| self.class.parse_part(part) }.reduce(:+) end |