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



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

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
68
# File 'lib/calcpace/converter.rb', line 64

def convert_to_clocktime(seconds)
  days = seconds / 86_400
  format = days.to_i > 0 ? "#{days} %H:%M:%S" : '%H:%M:%S'
  Time.at(seconds).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



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

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

#list_distanceObject



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

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

#list_speedObject



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

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