calcpace 1.1.0 → 1.2.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: 4076fd00047c9b763ee7c4d38d5b3c75b638ec851c0af06895c70d050a122b10
4
- data.tar.gz: 2c69b89a9f5508cd58a5f34b33f67517f6ac9a37218ae0e01fbbed600a269712
3
+ metadata.gz: 3f95d90f68ffe230e0f84e3d16b4221de5ffe61004e76ad2f2d2a18260ad9c30
4
+ data.tar.gz: fe206da9ac7d89e6df053fa0b6c6aff02227d06da7e81f48e6955cba183a720d
5
5
  SHA512:
6
- metadata.gz: '029f28e39b899c3d2ddc13c873db6ed76a3a3b743080c4c167263a592550c56f1dd68bb19e4bb353c15846c63a339d6ba07d7e1a33c1ce83ff8d3159f059d5e7'
7
- data.tar.gz: 2878a6024448fd28264e9b7df4d26fc19f2f41d02016dab82caa7c87f46b8b3bce0de55e5edaff627ca5ffa1b945e2461e5a3d49188df12f533a0cf003011eeb
6
+ metadata.gz: 8e93948a4751a908473f92cf526c09b6c8dc084978cb9c99a3e2a1977bd2ae7da7a14548631c689da95a0e10dcc361a512d1967f610f60cf38297e51fb7cdad6
7
+ data.tar.gz: bbf563ced661d193b79d5c90dd77ce63262488ceaf42fba06033b0154cebb0139a2f63f1749d1ed400985e854b1d716e54872a5a7b83756fabd2be16e081f7c8
@@ -1,31 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bigdecimal'
2
4
 
3
5
  module Calculator
4
- def pace(time, distance, bigdecimal = false)
5
- pace_in_seconds = pace_seconds(time, distance, bigdecimal)
6
+ def pace(time, distance)
7
+ pace_in_seconds = pace_seconds(time, distance)
6
8
  convert_to_clocktime(pace_in_seconds)
7
9
  end
8
10
 
9
- def pace_seconds(time, distance, bigdecimal = false)
11
+ def pace_seconds(time, distance)
10
12
  check_time(time)
11
13
  check_distance(distance)
12
14
  seconds = convert_to_seconds(time)
13
15
  bigdecimal ? seconds / BigDecimal(distance.to_s) : seconds / distance
14
16
  end
15
17
 
16
- def total_time(pace, distance, bigdecimal = false)
17
- total_time_in_seconds = total_time_seconds(pace, distance, bigdecimal)
18
+ def total_time(pace, distance)
19
+ total_time_in_seconds = total_time_seconds(pace, distance)
18
20
  convert_to_clocktime(total_time_in_seconds)
19
21
  end
20
22
 
21
- def total_time_seconds(pace, distance, bigdecimal = false)
23
+ def total_time_seconds(pace, distance)
22
24
  check_time(pace)
23
25
  check_distance(distance)
24
26
  pace_seconds = convert_to_seconds(pace)
25
- bigdecimal ? pace_seconds * BigDecimal(distance.to_s) : pace_seconds * distance
27
+ @bigdecimal ? pace_seconds * BigDecimal(distance.to_s) : pace_seconds * distance
26
28
  end
27
29
 
28
- def distance(time, pace, bigdecimal = false)
30
+ def distance(time, pace)
29
31
  check_time(time)
30
32
  check_time(pace)
31
33
  if bigdecimal
@@ -37,4 +39,4 @@ module Calculator
37
39
  end
38
40
  time_seconds / pace_seconds
39
41
  end
40
- end
42
+ end
@@ -3,8 +3,10 @@
3
3
  require 'bigdecimal'
4
4
 
5
5
  module Converter
6
- KM_TO_MI = BigDecimal("0.621371")
7
- MI_TO_KM = BigDecimal("1.60934")
6
+ KM_TO_MI_BIGDECIMAL = BigDecimal('0.621371')
7
+ KM_TO_MI = 0.621371
8
+ MI_TO_KM_BIGDECIMAL = BigDecimal('1.60934')
9
+ MI_TO_KM = 1.60934
8
10
 
9
11
  def to_seconds(time)
10
12
  check_time(time)
@@ -16,11 +18,18 @@ module Converter
16
18
  convert_to_clocktime(seconds)
17
19
  end
18
20
 
19
- def convert(distance, unit, round_limit = 2)
21
+ def convert_to_miles(distance)
22
+ convert(distance, 'mi')
23
+ end
24
+
25
+ def convert_to_km(distance)
26
+ convert(distance, 'km')
27
+ end
28
+
29
+ def convert(distance, unit)
20
30
  check_distance(distance)
21
- check_unit(unit)
22
- check_integer(round_limit)
23
- convert_the_distance(BigDecimal(distance.to_s), unit, round_limit)
31
+ bigdecimal ? distance_to_convert = BigDecimal(distance.to_s) : distance_to_convert = distance
32
+ convert_the_distance(distance_to_convert, unit)
24
33
  end
25
34
 
26
35
  def convert_to_seconds(time)
@@ -33,12 +42,14 @@ module Converter
33
42
  Time.at(seconds.to_i).utc.strftime(format)
34
43
  end
35
44
 
36
- def convert_the_distance(distance, unit, round_limit = 2)
45
+ def convert_the_distance(distance, unit)
37
46
  case unit
38
- when 'km'
39
- (distance * KM_TO_MI).round(round_limit)
40
47
  when 'mi'
41
- (distance * MI_TO_KM).round(round_limit)
48
+ bigdecimal ? km_to_mi = KM_TO_MI_BIGDECIMAL : km_to_mi = KM_TO_MI
49
+ (distance * km_to_mi)
50
+ when 'km'
51
+ bigdecimal ? mi_to_km = MI_TO_KM_BIGDECIMAL : mi_to_km = MI_TO_KM
52
+ (distance * mi_to_km)
42
53
  end
43
54
  end
44
- end
55
+ end
data/lib/calcpace.rb CHANGED
@@ -9,5 +9,9 @@ class Calcpace
9
9
  include Checker
10
10
  include Converter
11
11
 
12
- def initialize; end
12
+ attr_reader :bigdecimal
13
+
14
+ def initialize(bigdecimal = false)
15
+ @bigdecimal = bigdecimal
16
+ end
13
17
  end
@@ -7,6 +7,7 @@ require_relative '../../lib/calcpace'
7
7
  class TestCalculator < Minitest::Test
8
8
  def setup
9
9
  @checker = Calcpace.new
10
+ @checker_bigdecimal = Calcpace.new(bigdecimal = true)
10
11
  end
11
12
 
12
13
  def test_pace
@@ -18,8 +19,12 @@ class TestCalculator < Minitest::Test
18
19
  assert_equal '00:07:54', @checker.pace('01:37:21', 12.3)
19
20
  end
20
21
 
22
+ def test_pace_with_bigdecimal_precision
23
+ assert_equal '00:07:54', @checker_bigdecimal.pace('01:37:21', 12.3)
24
+ end
25
+
21
26
  def test_pace_without_bigdecimal_precision
22
- assert_equal '00:07:54', @checker.pace('01:37:21', 12.3, false)
27
+ assert_equal '00:07:54', @checker.pace('01:37:21', 12.3)
23
28
  end
24
29
 
25
30
  def test_pace_seconds
@@ -27,11 +32,11 @@ class TestCalculator < Minitest::Test
27
32
  assert_raises(RuntimeError) { @checker.pace_seconds('invalid', 10) }
28
33
  assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', 0) }
29
34
  assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', -1) }
30
- assert_equal BigDecimal('474.8780487804878'), @checker.pace_seconds('01:37:21', 12.3)
35
+ assert_equal 474.8780487804878, @checker.pace_seconds('01:37:21', 12.3)
31
36
  end
32
37
 
33
38
  def test_pace_seconds_with_bigdecimal_precision
34
- assert_equal BigDecimal('0.474878048780487804878048780487804878049e3'), @checker.pace_seconds('01:37:21', 12.3, true)
39
+ assert_equal BigDecimal('0.474878048780487804878048780487804878049e3'), @checker_bigdecimal.pace_seconds('01:37:21', 12.3)
35
40
  end
36
41
 
37
42
  def test_total_time
@@ -44,15 +49,15 @@ class TestCalculator < Minitest::Test
44
49
 
45
50
  def test_total_time_seconds
46
51
  assert_raises(RuntimeError) { @checker.total_time_seconds('', 10) }
47
- assert_raises(RuntimeError) { @checker.total_time_seconds('invalid', 10) }
52
+ assert_raises(RuntimeError) { @checker.total_time_seconds('invalid', 10) }
48
53
  assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', 0) }
49
54
  assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', -1) }
50
55
  assert_equal 3600, @checker.total_time_seconds('00:05:00', 12)
51
- assert_equal 71844.3, @checker.total_time_seconds('01:37:21', 12.3)
56
+ assert_equal 71_844.3, @checker.total_time_seconds('01:37:21', 12.3)
52
57
  end
53
58
 
54
59
  def test_total_time_seconds_with_bigdecimal_precision
55
- assert_equal BigDecimal('0.718443e5'), @checker.total_time_seconds('01:37:21', 12.3, true)
60
+ assert_equal BigDecimal('0.718443e5'), @checker_bigdecimal.total_time_seconds('01:37:21', 12.3)
56
61
  end
57
62
 
58
63
  def test_distance
@@ -63,6 +68,6 @@ class TestCalculator < Minitest::Test
63
68
  end
64
69
 
65
70
  def test_distance_with_bigdecimal_precision
66
- assert_equal BigDecimal('0.15493368700265251989389920424403183024e2'), @checker.distance('01:37:21', '00:06:17', true)
71
+ assert_equal BigDecimal('0.15493368700265251989389920424403183024e2'), @checker_bigdecimal.distance('01:37:21', '00:06:17')
67
72
  end
68
73
  end
@@ -6,6 +6,7 @@ require_relative '../../lib/calcpace'
6
6
  class TestConverter < Minitest::Test
7
7
  def setup
8
8
  @checker = Calcpace.new
9
+ @checker_bigdecimal = Calcpace.new(bigdecimal = true)
9
10
  end
10
11
 
11
12
  def test_convert_to_seconds
@@ -23,27 +24,35 @@ class TestConverter < Minitest::Test
23
24
 
24
25
  def test_to_clocktime
25
26
  assert_equal '01:00:00', @checker.to_clocktime(3600)
26
- assert_equal '02 01:00:00', @checker.to_clocktime(90000)
27
+ assert_equal '02 01:00:00', @checker.to_clocktime(90_000)
27
28
  assert_raises(RuntimeError) { @checker.to_clocktime(-1) }
28
29
  assert_raises(RuntimeError) { @checker.to_clocktime(0) }
29
30
  assert_raises(RuntimeError) { @checker.to_clocktime('invalid') }
30
31
  end
31
32
 
32
33
  def test_convert_the_distance
33
- assert_equal 6.21, @checker.convert_the_distance(10, 'km')
34
- assert_equal 6.2, @checker.convert_the_distance(10, 'km', 1)
35
- assert_equal 6.214, @checker.convert_the_distance(10, 'km', 3)
36
- assert_equal 16.09, @checker.convert_the_distance(10, 'mi')
34
+ assert_equal 6.21, @checker.convert_the_distance(10, 'mi').round(2)
35
+ assert_equal 6.2, @checker.convert_the_distance(10, 'mi').round(1)
36
+ assert_equal 6.214, @checker.convert_the_distance(10, 'mi').round(3)
37
+ assert_equal 16.09, @checker.convert_the_distance(10, 'km').round(2)
38
+ end
39
+
40
+ def test_convert_the_distance_with_bigdecimal_precision
41
+ assert_equal BigDecimal('6.21371'), @checker_bigdecimal.convert_the_distance(10, 'mi')
37
42
  end
38
43
 
39
44
  def test_convert
40
- assert_equal 6.21, @checker.convert(10, 'km')
41
- assert_equal 16.09, @checker.convert(10, 'mi')
42
- assert_equal 6.2, @checker.convert(10, 'km', 1)
43
- assert_raises(RuntimeError) { @checker.convert(10, 'invalid') }
45
+ assert_equal 6.21, @checker.convert(10, 'mi').round(2)
46
+ assert_equal 16.09, @checker.convert(10, 'km').round(2)
47
+ assert_equal 6.2, @checker.convert(10, 'mi').round(1)
44
48
  assert_raises(RuntimeError) { @checker.convert(-1, 'km') }
45
- assert_raises(RuntimeError) { @checker.convert(10, 'km', -2) }
46
- assert_raises(RuntimeError) { @checker.convert(10, 'km', 0) }
47
- assert_raises(RuntimeError) { @checker.convert(10, 'km', 'invalid') }
49
+ end
50
+
51
+ def test_convert_to_miles
52
+ assert_equal 6.21, @checker.convert_to_miles(10).round(2)
53
+ end
54
+
55
+ def test_convert_to_km
56
+ assert_equal 16.09, @checker.convert_to_km(10).round(2)
48
57
  end
49
58
  end
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.1.0
4
+ version: 1.2.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-07-21 00:00:00.000000000 Z
11
+ date: 2024-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -94,12 +94,12 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.11'
97
- description: 'Calcpace is a Ruby gem that helps with calculations related to running/cycling
97
+ description: Calcpace is a Ruby gem that helps with calculations related to running/cycling
98
98
  activities or general purposes involving distance and time. It can calculate pace,
99
99
  total time, and distance. It also converts distances between miles and kilometers
100
100
  and check formats of time and distance. The results are provided in a readable format,
101
101
  with times in HH:MM:SS or seconds and distances in X.X format. If you need, the
102
- gem supports BigDecimal to handle the calculations, '
102
+ gem supports BigDecimal to handle the calculations with more precision.
103
103
  email: joaogilberto@tuta.io
104
104
  executables: []
105
105
  extensions: []