Module: Converter

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

Constant Summary collapse

KM_TO_MI_BIGDECIMAL =
BigDecimal('0.621371')
KM_TO_MI =
0.621371
MI_TO_KM_BIGDECIMAL =
BigDecimal('1.60934')
MI_TO_KM =
1.60934

Instance Method Summary collapse

Instance Method Details

#convert(distance, unit) ⇒ Object



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

def convert(distance, unit)
  check_distance(distance)
  bigdecimal ? distance_to_convert = BigDecimal(distance.to_s) : distance_to_convert = distance
  convert_the_distance(distance_to_convert, unit)
end

#convert_the_distance(distance, unit) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/calcpace/converter.rb', line 45

def convert_the_distance(distance, unit)
  case unit
  when 'mi'
    bigdecimal ? km_to_mi = KM_TO_MI_BIGDECIMAL : km_to_mi = KM_TO_MI
    (distance * km_to_mi)
  when 'km'
    bigdecimal ? mi_to_km = MI_TO_KM_BIGDECIMAL : mi_to_km = MI_TO_KM
    (distance * mi_to_km)
  end
end

#convert_to_clocktime(seconds) ⇒ Object



40
41
42
43
# File 'lib/calcpace/converter.rb', line 40

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_km(distance) ⇒ Object



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

def convert_to_km(distance)
  convert(distance, 'km')
end

#convert_to_miles(distance) ⇒ Object



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

def convert_to_miles(distance)
  convert(distance, 'mi')
end

#convert_to_seconds(time) ⇒ Object



35
36
37
38
# File 'lib/calcpace/converter.rb', line 35

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

#to_clocktime(seconds) ⇒ Object



16
17
18
19
# File 'lib/calcpace/converter.rb', line 16

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

#to_seconds(time) ⇒ Object



11
12
13
14
# File 'lib/calcpace/converter.rb', line 11

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