Module: Converter

Included in:
Calcpace
Defined in:
lib/calcpace/converter.rb

Constant Summary collapse

KM_TO_MI =
BigDecimal("0.621371")
MI_TO_KM =
BigDecimal("1.60934")

Instance Method Summary collapse

Instance Method Details

#convert(distance, unit, round_limit = 2) ⇒ Object



19
20
21
22
23
24
# File 'lib/calcpace/converter.rb', line 19

def convert(distance, unit, round_limit = 2)
  check_distance(distance)
  check_unit(unit)
  check_integer(round_limit)
  convert_the_distance(BigDecimal(distance.to_s), unit, round_limit)
end

#convert_the_distance(distance, unit, round_limit = 2) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/calcpace/converter.rb', line 36

def convert_the_distance(distance, unit, round_limit = 2)
  case unit
  when 'km'
    (distance * KM_TO_MI).round(round_limit)
  when 'mi'
    (distance * MI_TO_KM).round(round_limit)
  end
end

#convert_to_clocktime(seconds) ⇒ Object



31
32
33
34
# File 'lib/calcpace/converter.rb', line 31

def convert_to_clocktime(seconds)
  format = seconds >= 86_400 ? '%d %H:%M:%S' : '%H:%M:%S'
  Time.at(seconds.to_i).utc.strftime(format)
end

#convert_to_seconds(time) ⇒ Object



26
27
28
29
# File 'lib/calcpace/converter.rb', line 26

def convert_to_seconds(time)
  hour, minute, seconds = time.split(':').map(&:to_i)
  (hour * 3600) + (minute * 60) + seconds
end

#to_clocktime(seconds) ⇒ Object



14
15
16
17
# File 'lib/calcpace/converter.rb', line 14

def to_clocktime(seconds)
  check_integer(seconds)
  convert_to_clocktime(seconds)
end

#to_seconds(time) ⇒ Object



9
10
11
12
# File 'lib/calcpace/converter.rb', line 9

def to_seconds(time)
  check_time(time)
  convert_to_seconds(time)
end