Class: GoDuration::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/go_duration/parser.rb

Constant Summary collapse

PART_REGEXP =
%r{\d+[hms]}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#inputObject (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

#partsObject



23
24
25
# File 'lib/go_duration/parser.rb', line 23

def parts
  @parts ||= input.scan(PART_REGEXP)
end

#secondsObject



19
20
21
# File 'lib/go_duration/parser.rb', line 19

def seconds
  parts.map { |part| self.class.parse_part(part) }.reduce(:+)
end