Module: Converter

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

Overview

Module to convert units

Constant Summary collapse

KM_TO_MI =
0.621371
MI_TO_KM =
1.60934
NAUTICAL_MI_TO_KM =
1.852
KM_TO_NAUTICAL_MI =
0.539957
METERS_TO_KM =
0.001
KM_TO_METERS =
1000
METERS_TO_MI =
0.000621371
MI_TO_METERS =
1609.34
METERS_TO_FEET =
3.28084
FEET_TO_METERS =
0.3048
METERS_TO_YARDS =
1.09361
YARDS_TO_METERS =
0.9144
METERS_TO_INCHES =
39.3701
INCHES_TO_METERS =
0.0254
M_S_TO_KM_H =
3.6
KM_H_TO_M_S =
0.277778
M_S_TO_MI_H =
2.23694
MI_H_TO_M_S =
0.44704
M_S_TO_NAUTICAL_MI_H =
1.94384
NAUTICAL_MI_H_TO_M_S =
0.514444
M_S_TO_FEET_S =
3.28084
FEET_S_TO_M_S =
0.3048
M_S_TO_KNOTS =
1.94384
KNOTS_TO_M_S =
0.514444
KM_H_TO_MI_H =
0.621371
MI_H_TO_KM_H =
1.60934

Instance Method Summary collapse

Instance Method Details

#constant(symbol) ⇒ Object



51
52
53
# File 'lib/calcpace/converter.rb', line 51

def constant(symbol)
  Converter.const_get(symbol.to_s.upcase)
end

#convert(value, unit) ⇒ Object



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

def convert(value, unit)
  check_positive(value)
  unit_constant = constant(unit)
  value * unit_constant
end

#convert_to_clocktime(seconds) ⇒ Object



46
47
48
49
# File 'lib/calcpace/converter.rb', line 46

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



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

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

#list_constantsObject



55
56
57
# File 'lib/calcpace/converter.rb', line 55

def list_constants
  Converter.constants.map { |c| c.downcase.to_sym }
end