calcpace 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d2fca26e59f2126bc2c6912786ea9808f8e73af4bc2860de63c5ab85ccf4b01
4
- data.tar.gz: 11907a7faff02d37df75a1db8d2540f114f46afe70bdb6a426ed3ed6f6bd1a81
3
+ metadata.gz: 5f044557de907efbc86496d835e45575a5302a965de92cfcb38f52eb64f0e861
4
+ data.tar.gz: 6c676c4d91b34b38593a1f696697d70c8da4025cb3fe34492f98bdd554f54c3c
5
5
  SHA512:
6
- metadata.gz: 451d2883e1b75c140bf5fbd85e9d1ec5b03dc770a471a173a8698ad6243b025736221ea157288c0ad23e5796287db4941f9177dfb3c7130deed50ac0bac6e126
7
- data.tar.gz: 7141e05845a0d88fcd52fb50dfef307ab3f8c73bbc8b332108fb3dfd13d4a3294ae3ae93b1eb983b98b2134ec67407aa8ba49659e38b169eddce342381bfd8c0
6
+ metadata.gz: 8d1878a80782cec3056945bd66aae3b185dd6889fd0de6d330fbf0de990f35f2779196b7032d2dd53364df2fcfef8ee4313132d045a9521fbc72e2134ba9d15d
7
+ data.tar.gz: c578de2a7229f1b2be9fd01e5b0f825b8a03ae6b54b0ca9bacf78923916a28ebd779a50e77811710f4845456a573d65dc30c943ed372597506b915219701bb8d
@@ -2,18 +2,18 @@
2
2
 
3
3
  require 'bigdecimal'
4
4
 
5
- # Module to calculate time, distance and velocity
5
+ # Module to calculate time, distance, pace and velocity
6
6
  module Calculator
7
7
  def velocity(time, distance)
8
- time / distance
8
+ check_positive(distance)
9
+ check_positive(time)
10
+ distance.to_f / time
9
11
  end
10
12
 
11
13
  def checked_velocity(time, distance)
12
14
  check_time(time)
13
- check_positive(distance)
14
- distance_to_calc = convert_to_bigdecimal(distance)
15
- seconds = convert_to_bigdecimal(convert_to_seconds(time))
16
- velocity(seconds, distance_to_calc)
15
+ seconds = convert_to_seconds(time)
16
+ velocity(seconds, distance)
17
17
  end
18
18
 
19
19
  def clock_velocity(time, distance)
@@ -21,16 +21,33 @@ module Calculator
21
21
  convert_to_clocktime(velocity_in_seconds)
22
22
  end
23
23
 
24
+ def pace(time, distance)
25
+ check_positive(distance)
26
+ check_positive(time)
27
+ time.to_f / distance
28
+ end
29
+
30
+ def checked_pace(time, distance)
31
+ check_time(time)
32
+ seconds = convert_to_seconds(time)
33
+ pace(seconds, distance)
34
+ end
35
+
36
+ def clock_pace(time, distance)
37
+ velocity_in_seconds = checked_pace(time, distance)
38
+ convert_to_clocktime(velocity_in_seconds)
39
+ end
40
+
24
41
  def time(velocity, distance)
42
+ check_positive(distance)
43
+ check_positive(velocity)
25
44
  velocity * distance
26
45
  end
27
46
 
28
47
  def checked_time(velocity, distance)
29
48
  check_time(velocity)
30
- check_positive(distance)
31
- distance_to_calc = convert_to_bigdecimal(distance)
32
- velocity_seconds = convert_to_bigdecimal(convert_to_seconds(velocity))
33
- time(velocity_seconds, distance_to_calc)
49
+ velocity_seconds = convert_to_seconds(velocity)
50
+ time(velocity_seconds, distance)
34
51
  end
35
52
 
36
53
  def clock_time(velocity, distance)
@@ -39,14 +56,16 @@ module Calculator
39
56
  end
40
57
 
41
58
  def distance(time, velocity)
42
- time / velocity
59
+ check_positive(time)
60
+ check_positive(velocity)
61
+ time.to_f / velocity
43
62
  end
44
63
 
45
64
  def checked_distance(time, velocity)
46
65
  check_time(time)
47
66
  check_time(velocity)
48
- time_seconds = convert_to_bigdecimal(convert_to_seconds(time))
49
- velocity_seconds = convert_to_bigdecimal(convert_to_seconds(velocity))
67
+ time_seconds = convert_to_seconds(time)
68
+ velocity_seconds = convert_to_seconds(velocity)
50
69
  distance(time_seconds, velocity_seconds)
51
70
  end
52
71
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Module to check if the input is valid
3
+ # Module to check if the input is valid or of the correct type
4
4
  module Checker
5
5
  def check_positive(number)
6
6
  raise 'It must be a X.X positive number' unless number.positive?
@@ -35,9 +35,7 @@ module Converter
35
35
  def convert(value, unit)
36
36
  check_positive(value)
37
37
  unit_constant = constant(unit)
38
- value_to_convert = convert_to_bigdecimal(value)
39
- unit_to_convert = convert_to_bigdecimal(unit_constant)
40
- value_to_convert * unit_to_convert
38
+ value * unit_constant
41
39
  end
42
40
 
43
41
  def convert_to_seconds(time)
@@ -50,15 +48,11 @@ module Converter
50
48
  Time.at(seconds.to_i).utc.strftime(format)
51
49
  end
52
50
 
53
- def convert_to_bigdecimal(value)
54
- bigdecimal ? BigDecimal(value.to_s) : value
55
- end
56
-
57
- def constant(string)
58
- Converter.const_get(string.upcase.gsub(' ', '_'))
51
+ def constant(symbol)
52
+ Converter.const_get(symbol.to_s.upcase)
59
53
  end
60
54
 
61
55
  def list_constants
62
- Converter.constants
56
+ Converter.constants.map { |c| c.downcase.to_sym }
63
57
  end
64
58
  end
data/lib/calcpace.rb CHANGED
@@ -4,7 +4,7 @@ require_relative 'calcpace/calculator'
4
4
  require_relative 'calcpace/checker'
5
5
  require_relative 'calcpace/converter'
6
6
 
7
- # Main class to calculate velocity, time, distance and velocity
7
+ # Main class to calculate velocity, pace, time, distance and velocity
8
8
  class Calcpace
9
9
  include Calculator
10
10
  include Checker
@@ -12,7 +12,5 @@ class Calcpace
12
12
 
13
13
  attr_reader :bigdecimal
14
14
 
15
- def initialize(bigdecimal = false)
16
- @bigdecimal = bigdecimal
17
- end
15
+ def initialize; end
18
16
  end
@@ -7,12 +7,14 @@ require_relative '../../lib/calcpace'
7
7
  class TestCalculator < Minitest::Test
8
8
  def setup
9
9
  @checker = Calcpace.new
10
- @checker_bigdecimal = Calcpace.new(true)
11
10
  end
12
11
 
13
12
  def test_velocity
14
- assert_equal 300, @checker.velocity(3600, 12)
15
- assert_equal 122.81076923076924, @checker.velocity(71_844.3, 585.0)
13
+ assert_equal 3.333, @checker.velocity(3600, 12_000).round(3)
14
+ assert_equal 12.3, @checker.velocity(5841, 71_844.3)
15
+ assert_equal 3.6, @checker.velocity(10_000, 36_000.0)
16
+ assert_raises(RuntimeError) { @checker.velocity(0, 10) }
17
+ assert_raises(RuntimeError) { @checker.velocity(10, -1) }
16
18
  end
17
19
 
18
20
  def test_checked_velocity
@@ -20,13 +22,9 @@ class TestCalculator < Minitest::Test
20
22
  assert_raises(RuntimeError) { @checker.checked_velocity('invalid', 10) }
21
23
  assert_raises(RuntimeError) { @checker.checked_velocity('00:00:00', 0) }
22
24
  assert_raises(RuntimeError) { @checker.checked_velocity('00:00:00', -1) }
23
- assert_equal 360, @checker.checked_velocity('01:00:00', 10)
24
- assert_equal 474.8780487804878, @checker.checked_velocity('01:37:21', 12.3)
25
- end
26
-
27
- def test_checked_velocity_with_bigdecimal_precision
28
- assert_equal BigDecimal('0.474878048780487804878048780487804878049e3'),
29
- @checker_bigdecimal.checked_velocity('01:37:21', 12.3)
25
+ assert_equal 2.778, @checker.checked_velocity('01:00:00', 10_000).round(3)
26
+ assert_equal 10, @checker.checked_velocity('00:00:01', 10)
27
+ assert_equal 12.3, @checker.checked_velocity('01:37:21', 71_844.3)
30
28
  end
31
29
 
32
30
  def test_clock_velocity
@@ -34,13 +32,40 @@ class TestCalculator < Minitest::Test
34
32
  assert_raises(RuntimeError) { @checker.clock_velocity('invalid', 10) }
35
33
  assert_raises(RuntimeError) { @checker.clock_velocity('00:00:00', 0) }
36
34
  assert_raises(RuntimeError) { @checker.clock_velocity('00:00:00', -1) }
37
- assert_equal '00:06:00', @checker.clock_velocity('01:00:00', 10)
38
- assert_equal '00:07:54', @checker.clock_velocity('01:37:21', 12.3)
35
+ assert_equal '00:00:02', @checker.clock_velocity('01:00:00', 10_000)
36
+ assert_equal '00:00:12', @checker.clock_velocity('01:37:21', 71_844.3)
37
+ end
38
+
39
+ def test_pace
40
+ assert_equal 300, @checker.pace(3600, 12)
41
+ assert_equal 122.81076923076924, @checker.pace(71_844.3, 585.0)
42
+ assert_raises(RuntimeError) { @checker.pace(0, 10) }
43
+ assert_raises(RuntimeError) { @checker.pace(10, -1) }
44
+ end
45
+
46
+ def test_checked_pace
47
+ assert_raises(RuntimeError) { @checker.checked_pace('', 10) }
48
+ assert_raises(RuntimeError) { @checker.checked_pace('invalid', 10) }
49
+ assert_raises(RuntimeError) { @checker.checked_pace('00:00:00', 0) }
50
+ assert_raises(RuntimeError) { @checker.checked_pace('00:00:00', -1) }
51
+ assert_equal 360, @checker.checked_pace('01:00:00', 10)
52
+ assert_equal 474.8780487804878, @checker.checked_pace('01:37:21', 12.3)
53
+ end
54
+
55
+ def test_clock_pace
56
+ assert_raises(RuntimeError) { @checker.clock_pace('', 10) }
57
+ assert_raises(RuntimeError) { @checker.clock_pace('invalid', 10) }
58
+ assert_raises(RuntimeError) { @checker.clock_pace('00:00:00', 0) }
59
+ assert_raises(RuntimeError) { @checker.clock_pace('00:00:00', -1) }
60
+ assert_equal '00:06:00', @checker.clock_pace('01:00:00', 10)
61
+ assert_equal '00:07:54', @checker.clock_pace('01:37:21', 12.3)
39
62
  end
40
63
 
41
64
  def test_time
42
65
  assert_equal 43_200, @checker.time(3600, 12)
43
66
  assert_equal 5841.0, @checker.time(12.3, 474.8780487804878)
67
+ assert_raises(RuntimeError) { @checker.time(0, 10) }
68
+ assert_raises(RuntimeError) { @checker.time(10, -1) }
44
69
  end
45
70
 
46
71
  def test_checked_time
@@ -52,10 +77,6 @@ class TestCalculator < Minitest::Test
52
77
  assert_equal 71_844.3, @checker.checked_time('01:37:21', 12.3)
53
78
  end
54
79
 
55
- def test_checked_time_with_bigdecimal_precision
56
- assert_equal BigDecimal('0.718443e5'), @checker_bigdecimal.checked_time('01:37:21', 12.3)
57
- end
58
-
59
80
  def test_clock_time
60
81
  assert_raises(RuntimeError) { @checker.clock_time('', 10) }
61
82
  assert_raises(RuntimeError) { @checker.clock_time('invalid', 10) }
@@ -67,17 +88,31 @@ class TestCalculator < Minitest::Test
67
88
  def test_distance
68
89
  assert_equal 30, @checker.distance(3600, 120)
69
90
  assert_equal 12.3, @checker.distance(5841.0, 474.8780487804878)
91
+ assert_raises(RuntimeError) { @checker.distance(0, 10) }
92
+ assert_raises(RuntimeError) { @checker.distance(10, -1) }
70
93
  end
71
94
 
72
95
  def test_checked_distance
73
96
  assert_raises(RuntimeError) { @checker.checked_distance('', '00:05:00') }
74
97
  assert_raises(RuntimeError) { @checker.checked_distance('01:00:00', '') }
75
98
  assert_equal 18.0, @checker.checked_distance('01:30:00', '00:05:00')
76
- assert_equal 15.0, @checker.checked_distance('01:37:21', '00:06:17')
99
+ assert_equal 15.493, @checker.checked_distance('01:37:21', '00:06:17').round(3)
100
+ end
101
+
102
+ def test_readme_examples_one
103
+ assert_equal 3.386206896551724, @checker.velocity(3625, 12_275)
104
+ assert_equal 305.4166666666667, @checker.pace(3665, 12)
105
+ assert_equal 2520.0, @checker.time(210, 12)
106
+ assert_equal 80.5, @checker.distance(9660, 120)
77
107
  end
78
108
 
79
- def test_checked_distance_with_bigdecimal_precision
80
- assert_equal BigDecimal('0.15493368700265251989389920424403183024e2'),
81
- @checker_bigdecimal.checked_distance('01:37:21', '00:06:17')
109
+ def test_readme_examples_two
110
+ assert_equal 2.8658333333333332, @checker.checked_velocity('01:00:00', 10_317)
111
+ assert_equal '00:00:02', @checker.clock_velocity('01:00:00', 10_317)
112
+ assert_equal 489.2, @checker.checked_pace('01:21:32', 10)
113
+ assert_equal '00:08:09', @checker.clock_pace('01:21:32', 10)
114
+ assert_equal 4170.599999999999, @checker.checked_time('00:05:31', 12.6)
115
+ assert_equal '01:09:30', @checker.clock_time('00:05:31', 12.6)
116
+ assert_equal 12.640826873385013, @checker.checked_distance('01:21:32', '00:06:27')
82
117
  end
83
118
  end
@@ -6,7 +6,6 @@ require_relative '../../lib/calcpace'
6
6
  class TestConverter < Minitest::Test
7
7
  def setup
8
8
  @checker = Calcpace.new
9
- @checker_bigdecimal = Calcpace.new(true)
10
9
  end
11
10
 
12
11
  def test_convert_to_seconds
@@ -17,40 +16,49 @@ class TestConverter < Minitest::Test
17
16
  assert_equal '01:11:02', @checker.convert_to_clocktime(4262)
18
17
  end
19
18
 
20
- def test_convert_distance
21
- assert_equal 0.621371, @checker.convert(1, 'KM_TO_MI')
22
- assert_equal 1.60934, @checker.convert(1, 'MI_TO_KM')
23
- assert_equal 1.852, @checker.convert(1, 'NAUTICAL_MI_TO_KM')
24
- assert_equal 0.539957, @checker.convert(1, 'KM_TO_NAUTICAL_MI')
25
- assert_equal 0.001, @checker.convert(1, 'METERS_TO_KM')
26
- assert_equal 1000, @checker.convert(1, 'KM_TO_METERS')
27
- assert_equal 0.000621371, @checker.convert(1, 'METERS_TO_MI')
28
- assert_equal 1609.34, @checker.convert(1, 'MI_TO_METERS')
29
- assert_equal 3.28084, @checker.convert(1, 'METERS_TO_FEET')
30
- assert_equal 0.3048, @checker.convert(1, 'FEET_TO_METERS')
31
- assert_equal 1.09361, @checker.convert(1, 'METERS_TO_YARDS')
32
- assert_equal 0.9144, @checker.convert(1, 'YARDS_TO_METERS')
33
- assert_equal 39.3701, @checker.convert(1, 'METERS_TO_INCHES')
34
- assert_equal 0.0254, @checker.convert(1, 'INCHES_TO_METERS')
35
- end
36
-
37
- def test_convert_velocity
38
- assert_equal 3.60, @checker.convert(1, 'M_S_TO_KM_H')
39
- assert_equal 0.277778, @checker.convert(1, 'KM_H_TO_M_S')
40
- assert_equal 2.23694, @checker.convert(1, 'M_S_TO_MI_H')
41
- assert_equal 0.44704, @checker.convert(1, 'MI_H_TO_M_S')
42
- assert_equal 1.94384, @checker.convert(1, 'M_S_TO_NAUTICAL_MI_H')
43
- assert_equal 0.514444, @checker.convert(1, 'NAUTICAL_MI_H_TO_M_S')
44
- assert_equal 0.621371, @checker.convert(1, 'KM_H_TO_MI_H')
45
- assert_equal 1.60934, @checker.convert(1, 'MI_H_TO_KM_H')
46
- assert_equal 1.94384, @checker.convert(1, 'M_S_TO_KNOTS')
47
- assert_equal 0.514444, @checker.convert(1, 'KNOTS_TO_M_S')
19
+ def test_convert_distance_one
20
+ assert_equal 0.621371, @checker.convert(1, :km_to_mi)
21
+ assert_equal 1.60934, @checker.convert(1, :mi_to_km)
22
+ assert_equal 1.852, @checker.convert(1, :nautical_mi_to_km)
23
+ assert_equal 0.539957, @checker.convert(1, :km_to_nautical_mi)
24
+ assert_equal 0.001, @checker.convert(1, :meters_to_km)
25
+ assert_equal 1000, @checker.convert(1, :km_to_meters)
26
+ end
27
+
28
+ def test_convert_distance_two
29
+ assert_equal 0.000621371, @checker.convert(1, :meters_to_mi)
30
+ assert_equal 1609.34, @checker.convert(1, :mi_to_meters)
31
+ assert_equal 3.28084, @checker.convert(1, :meters_to_feet)
32
+ assert_equal 0.3048, @checker.convert(1, :feet_to_meters)
33
+ assert_equal 1.09361, @checker.convert(1, :meters_to_yards)
34
+ end
35
+
36
+ def test_convert_distance_three
37
+ assert_equal 0.9144, @checker.convert(1, :yards_to_meters)
38
+ assert_equal 39.3701, @checker.convert(1, :meters_to_inches)
39
+ assert_equal 0.0254, @checker.convert(1, :inches_to_meters)
40
+ end
41
+
42
+ def test_convert_velocity_one
43
+ assert_equal 3.60, @checker.convert(1, :m_s_to_km_h)
44
+ assert_equal 0.277778, @checker.convert(1, :km_h_to_m_s)
45
+ assert_equal 2.23694, @checker.convert(1, :m_s_to_mi_h)
46
+ assert_equal 0.44704, @checker.convert(1, :mi_h_to_m_s)
47
+ assert_equal 1.94384, @checker.convert(1, :m_s_to_nautical_mi_h)
48
+ end
49
+
50
+ def test_convert_velocity_two
51
+ assert_equal 0.514444, @checker.convert(1, :nautical_mi_h_to_m_s)
52
+ assert_equal 0.621371, @checker.convert(1, :km_h_to_mi_h)
53
+ assert_equal 1.60934, @checker.convert(1, :mi_h_to_km_h)
54
+ assert_equal 1.94384, @checker.convert(1, :m_s_to_knots)
55
+ assert_equal 0.514444, @checker.convert(1, :knots_to_m_s)
48
56
  end
49
57
 
50
58
  def test_constant
51
- assert_equal 0.621371, @checker.constant('KM_TO_MI')
52
- assert_equal 1.60934, @checker.constant('MI_TO_KM')
53
- assert_equal 1.852, @checker.constant('NAUTICAL_MI_TO_KM')
59
+ assert_equal 0.621371, @checker.constant(:km_to_mi)
60
+ assert_equal 1.60934, @checker.constant(:mi_to_km)
61
+ assert_equal 1.852, @checker.constant(:nautical_mi_to_km)
54
62
  end
55
63
 
56
64
  def test_list_constants
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calcpace
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joao Gilberto Saraiva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-08 00:00:00.000000000 Z
11
+ date: 2024-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -80,9 +80,9 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.66'
83
- description: Calcpace is a Ruby gem that helps with calculations related to distance
84
- and time. It can calculate velocity, total time, and distance. It also converts
85
- distance and velocity, check formats of time and can handle calculus with BigDecimal.
83
+ description: Calcpace is designed for calculations and conversions related to distance
84
+ and time. It can calculate velocity, pace, total time, and distance and it supports
85
+ conversion to 26 different units, including kilometers, miles, meters, and feet.
86
86
  email: joaogilberto@tuta.io
87
87
  executables: []
88
88
  extensions: []
@@ -115,11 +115,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
117
  requirements: []
118
- rubygems_version: 3.5.16
118
+ rubygems_version: 3.4.6
119
119
  signing_key:
120
120
  specification_version: 4
121
121
  summary: 'Calcpace: calculate total, distance, velocity and convert distances in an
122
- easy and precise way.'
122
+ easy way.'
123
123
  test_files:
124
124
  - test/calcpace/test_calculator.rb
125
125
  - test/calcpace/test_checker.rb