Skip to content

Commit

Permalink
44 create list speed and list distance methods (#46)
Browse files Browse the repository at this point in the history
* Change from RuntimeError to ArgumentError

* Refactoring methods of calculation

* Add options to list methods

* Add new distance and speed constants

* bump gems

* change version of gem to 1.5.0

* Update readme with list options

* Update gemspec, remove bigdecimal attr read

* Fix code scanning alert no. 1: Badly anchored regular expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
  • Loading branch information
0jonjo and github-advanced-security[bot] authored Oct 26, 2024
1 parent f50fc57 commit 328dbfc
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 120 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
json (2.7.2)
json (2.7.4)
language_server-protocol (3.17.0.3)
minitest (5.23.1)
minitest (5.25.1)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
Expand All @@ -14,12 +14,12 @@ GEM
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
rake-compiler (1.2.7)
rake-compiler (1.2.8)
rake
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.9.2)
rubocop (1.66.1)
rubocop (1.67.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand All @@ -33,7 +33,7 @@ GEM
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
stringio (3.1.1)
unicode-display_width (2.5.0)
unicode-display_width (2.6.0)

PLATFORMS
ruby
Expand Down
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Calcpace [![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&r=r&ts=1683906897&type=6e&v=1.4.0&x2=0)](https://badge.fury.io/rb/calcpace)
# Calcpace [![Gem Version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=rb&r=r&ts=1683906897&type=6e&v=1.5.0&x2=0)](https://badge.fury.io/rb/calcpace)

Calcpace is a Ruby gem designed for calculations and conversions related to distance and time. It can calculate velocity, pace, total time, and distance, accepting time in various formats, including HH:MM:SS. The gem supports conversion to 26 different units, including kilometers, miles, meters, and feet. It also provides methods to validate input.
Calcpace is a Ruby gem designed for calculations and conversions related to distance and time. It can calculate velocity, pace, total time, and distance, accepting time in various formats, including HH:MM:SS. The gem supports conversion to 42 different units, including kilometers, miles, meters, and feet. It also provides methods to validate input.

## Installation

### Add to your Gemfile

```ruby
gem 'calcpace', '~> 1.4.0'
gem 'calcpace', '~> 1.5.0'
```

Then run:
Expand Down Expand Up @@ -109,29 +109,21 @@ converter.convert(1, :m_s_to_mi_h) # => 2.23694
| :km_to_meters | Kilometers to Meters |
| :meters_to_mi | Meters to Miles |
| :mi_to_meters | Miles to Meters |
| :meters_to_feet | Meters to Feet |
| :feet_to_meters | Feet to Meters |
| :meters_to_yards | Meters to Yards |
| :yards_to_meters | Yards to Meters |
| :meters_to_inches | Meters to Inches |
| :inches_to_meters | Inches to Meters |
| :m_s_to_km_h | Meters per Second to Kilometers per Hour |
| :km_h_to_m_s | Kilometers per Hour to Meters per Second |
| :m_s_to_mi_h | Meters per Second to Miles per Hour |
| :mi_h_to_m_s | Miles per Hour to Meters per Second |
| :m_s_to_nautical_mi_h| Meters per Second to Nautical Miles per Hour |
| :nautical_mi_h_to_m_s| Nautical Miles per Hour to Meters per Second |
| :m_s_to_feet_s | Meters per Second to Feet per Second |
| :feet_s_to_m_s | Feet per Second to Meters per Second |
| :m_s_to_knots | Meters per Second to Knots |
| :knots_to_m_s | Knots to Meters per Second |
| :km_h_to_mi_h | Kilometers per Hour to Miles per Hour |
| :mi_h_to_km_h | Miles per Hour to Kilometers per Hour |

You can list all the available units using the `list_units` method:
You can list all the available units using `list` methods:

```ruby
converter.list_units
converter.list_all
converter.list_distance
converter.list_speed
```

### Other Useful Methods
Expand Down
6 changes: 3 additions & 3 deletions calcpace.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Gem::Specification.new do |s|
s.name = 'calcpace'
s.version = '1.4.0'
s.summary = 'Calcpace: calculate total, distance, velocity and convert distances in an easy way.'
s.description = 'Calcpace is designed for calculations and conversions related to distance and time. It can calculate velocity, pace, total time, and distance and it supports conversion to 26 different units, including kilometers, miles, meters, and feet.'
s.version = '1.5.0'
s.summary = 'Calcpace: calculate total, distance, speed, and convert distances and velocity in an easy way.'
s.description = 'It is designed for calculations related to distance, speed and time. The gem also supports conversion to 42 different units of distance and velocity, including metric, nautical and imperial units.'
s.authors = ['Joao Gilberto Saraiva']
s.email = 'joaogilberto@tuta.io'
s.files = ['lib/calcpace.rb', 'lib/calcpace/calculator.rb', 'lib/calcpace/checker.rb',
Expand Down
2 changes: 0 additions & 2 deletions lib/calcpace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ class Calcpace
include Checker
include Converter

attr_reader :bigdecimal

def initialize; end
end
47 changes: 23 additions & 24 deletions lib/calcpace/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,66 @@
# Module to calculate time, distance, pace and velocity
module Calculator
def velocity(time, distance)
check_positive(distance)
check_positive(time)
validate_positive(time, distance)
distance.to_f / time
end

def checked_velocity(time, distance)
check_time(time)
seconds = convert_to_seconds(time)
seconds = convert_to_seconds(validate_time(time))
velocity(seconds, distance)
end

def clock_velocity(time, distance)
velocity_in_seconds = checked_velocity(time, distance)
convert_to_clocktime(velocity_in_seconds)
convert_to_clocktime(checked_velocity(time, distance))
end

def pace(time, distance)
check_positive(distance)
check_positive(time)
validate_positive(time, distance)
time.to_f / distance
end

def checked_pace(time, distance)
check_time(time)
seconds = convert_to_seconds(time)
seconds = convert_to_seconds(validate_time(time))
pace(seconds, distance)
end

def clock_pace(time, distance)
velocity_in_seconds = checked_pace(time, distance)
convert_to_clocktime(velocity_in_seconds)
convert_to_clocktime(checked_pace(time, distance))
end

def time(velocity, distance)
check_positive(distance)
check_positive(velocity)
validate_positive(velocity, distance)
velocity * distance
end

def checked_time(velocity, distance)
check_time(velocity)
velocity_seconds = convert_to_seconds(velocity)
velocity_seconds = convert_to_seconds(validate_time(velocity))
time(velocity_seconds, distance)
end

def clock_time(velocity, distance)
total_time_in_seconds = checked_time(velocity, distance)
convert_to_clocktime(total_time_in_seconds)
convert_to_clocktime(checked_time(velocity, distance))
end

def distance(time, velocity)
check_positive(time)
check_positive(velocity)
validate_positive(time, velocity)
time.to_f / velocity
end

def checked_distance(time, velocity)
check_time(time)
check_time(velocity)
time_seconds = convert_to_seconds(time)
velocity_seconds = convert_to_seconds(velocity)
time_seconds = convert_to_seconds(validate_time(time))
velocity_seconds = convert_to_seconds(validate_time(velocity))
distance(time_seconds, velocity_seconds)
end

private

def validate_positive(*values)
values.each { |value| check_positive(value) }
end

def validate_time(time)
check_time(time)
time
end
end
4 changes: 2 additions & 2 deletions lib/calcpace/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Module to check if the input is valid or of the correct type
module Checker
def check_positive(number)
raise 'It must be a X.X positive number' unless number.positive?
raise ArgumentError, 'It must be a positive number' unless number.is_a?(Numeric) && number.positive?
end

def check_time(time_string)
raise 'It must be a XX:XX:XX time' unless time_string =~ /\d{0,2}(:)*?\d{1,2}(:)\d{1,2}/
raise ArgumentError, 'It must be a valid time in the format XX:XX:XX' unless time_string =~ /\A\d{1,2}:\d{2}:\d{2}\z/
end
end
90 changes: 60 additions & 30 deletions lib/calcpace/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,53 @@

# Module to convert units
module Converter
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
module Distance
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
KM_TO_YARDS = 1093.61
YARDS_TO_KM = 0.0009144
KM_TO_FEET = 3280.84
FEET_TO_KM = 0.0003048
KM_TO_INCHES = 39_370.1
INCHES_TO_KM = 0.0000254
MI_TO_YARDS = 1760
YARDS_TO_MI = 0.000568182
MI_TO_FEET = 5280
FEET_TO_MI = 0.000189394
MI_TO_INCHES = 63_360
INCHES_TO_MI = 0.0000157828
end

module Speed
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
KM_H_TO_NAUTICAL_MI_H = 0.539957
NAUTICAL_MI_H_TO_KM_H = 1.852
MI_H_TO_NAUTICAL_MI_H = 0.868976
NAUTICAL_MI_H_TO_MI_H = 1.15078
end

def convert(value, unit)
check_positive(value)
Expand All @@ -47,10 +67,20 @@ def convert_to_clocktime(seconds)
end

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

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

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

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

0 comments on commit 328dbfc

Please sign in to comment.