Module: Converter

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

Overview

Module to convert units

Defined Under Namespace

Modules: Distance, Speed

Instance Method Summary collapse

Instance Method Details

#constant(symbol) ⇒ Object



69
70
71
72
73
# File 'lib/calcpace/converter.rb', line 69

def constant(symbol)
  Distance.const_get(symbol.to_s.upcase)
rescue NameError
  Speed.const_get(symbol.to_s.upcase)
end

#convert(value, unit) ⇒ Object



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

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

#convert_to_clocktime(seconds) ⇒ Object



64
65
66
67
# File 'lib/calcpace/converter.rb', line 64

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



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

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

#list_allObject



75
76
77
# File 'lib/calcpace/converter.rb', line 75

def list_all
  (Distance.constants + Speed.constants).map { |c| c.downcase.to_sym }
end

#list_distanceObject



83
84
85
# File 'lib/calcpace/converter.rb', line 83

def list_distance
  Distance.constants.map { |c| c.downcase.to_sym }
end

#list_speedObject



79
80
81
# File 'lib/calcpace/converter.rb', line 79

def list_speed
  Speed.constants.map { |c| c.downcase.to_sym }
end