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



57
58
59
# File 'lib/calcpace/converter.rb', line 57

def constant(string)
  Converter.const_get(string.upcase.gsub(' ', '_'))
end

#convert(value, unit) ⇒ Object



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

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

#convert_to_bigdecimal(value) ⇒ Object



53
54
55
# File 'lib/calcpace/converter.rb', line 53

def convert_to_bigdecimal(value)
  bigdecimal ? BigDecimal(value.to_s) : value
end

#convert_to_clocktime(seconds) ⇒ Object



48
49
50
51
# File 'lib/calcpace/converter.rb', line 48

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



43
44
45
46
# File 'lib/calcpace/converter.rb', line 43

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

#list_constantsObject



61
62
63
# File 'lib/calcpace/converter.rb', line 61

def list_constants
  Converter.constants
end