Module: Converter
- Included in:
- Calcpace
- Defined in:
- lib/calcpace/converter.rb
Overview
Module to convert units
Defined Under Namespace
Instance Method Summary collapse
- #constant(symbol) ⇒ Object
- #convert(value, unit) ⇒ Object
- #convert_to_clocktime(seconds) ⇒ Object
- #convert_to_seconds(time) ⇒ Object
- #list_all ⇒ Object
- #list_distance ⇒ Object
- #list_speed ⇒ Object
Instance Method Details
#constant(symbol) ⇒ Object
76 77 78 79 80 |
# File 'lib/calcpace/converter.rb', line 76 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
70 71 72 73 74 |
# File 'lib/calcpace/converter.rb', line 70 def convert_to_clocktime(seconds) days = seconds / 86_400 format = days.to_i.positive? ? "#{days} %H:%M:%S" : '%H:%M:%S' Time.at(seconds).utc.strftime(format) end |
#convert_to_seconds(time) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/calcpace/converter.rb', line 59 def convert_to_seconds(time) parts = time.split(':').map(&:to_i) if parts.length == 2 minute, seconds = parts (minute * 60) + seconds else hour, minute, seconds = parts (hour * 3600) + (minute * 60) + seconds end end |
#list_all ⇒ Object
82 83 84 |
# File 'lib/calcpace/converter.rb', line 82 def list_all (Distance.constants + Speed.constants).map { |c| c.downcase.to_sym } end |
#list_distance ⇒ Object
90 91 92 |
# File 'lib/calcpace/converter.rb', line 90 def list_distance Distance.constants.map { |c| c.downcase.to_sym } end |
#list_speed ⇒ Object
86 87 88 |
# File 'lib/calcpace/converter.rb', line 86 def list_speed Speed.constants.map { |c| c.downcase.to_sym } end |