calcpace 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/calcpace/calculator.rb +8 -8
- data/lib/calcpace/converter.rb +55 -44
- data/lib/calcpace.rb +5 -1
- data/test/calcpace/test_calculator.rb +73 -68
- data/test/calcpace/test_converter.rb +58 -49
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f95d90f68ffe230e0f84e3d16b4221de5ffe61004e76ad2f2d2a18260ad9c30
|
4
|
+
data.tar.gz: fe206da9ac7d89e6df053fa0b6c6aff02227d06da7e81f48e6955cba183a720d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e93948a4751a908473f92cf526c09b6c8dc084978cb9c99a3e2a1977bd2ae7da7a14548631c689da95a0e10dcc361a512d1967f610f60cf38297e51fb7cdad6
|
7
|
+
data.tar.gz: bbf563ced661d193b79d5c90dd77ce63262488ceaf42fba06033b0154cebb0139a2f63f1749d1ed400985e854b1d716e54872a5a7b83756fabd2be16e081f7c8
|
data/lib/calcpace/calculator.rb
CHANGED
@@ -3,31 +3,31 @@
|
|
3
3
|
require 'bigdecimal'
|
4
4
|
|
5
5
|
module Calculator
|
6
|
-
def pace(time, distance
|
7
|
-
pace_in_seconds = pace_seconds(time, distance
|
6
|
+
def pace(time, distance)
|
7
|
+
pace_in_seconds = pace_seconds(time, distance)
|
8
8
|
convert_to_clocktime(pace_in_seconds)
|
9
9
|
end
|
10
10
|
|
11
|
-
def pace_seconds(time, distance
|
11
|
+
def pace_seconds(time, distance)
|
12
12
|
check_time(time)
|
13
13
|
check_distance(distance)
|
14
14
|
seconds = convert_to_seconds(time)
|
15
15
|
bigdecimal ? seconds / BigDecimal(distance.to_s) : seconds / distance
|
16
16
|
end
|
17
17
|
|
18
|
-
def total_time(pace, distance
|
19
|
-
total_time_in_seconds = total_time_seconds(pace, distance
|
18
|
+
def total_time(pace, distance)
|
19
|
+
total_time_in_seconds = total_time_seconds(pace, distance)
|
20
20
|
convert_to_clocktime(total_time_in_seconds)
|
21
21
|
end
|
22
22
|
|
23
|
-
def total_time_seconds(pace, distance
|
23
|
+
def total_time_seconds(pace, distance)
|
24
24
|
check_time(pace)
|
25
25
|
check_distance(distance)
|
26
26
|
pace_seconds = convert_to_seconds(pace)
|
27
|
-
bigdecimal ? pace_seconds * BigDecimal(distance.to_s) : pace_seconds * distance
|
27
|
+
@bigdecimal ? pace_seconds * BigDecimal(distance.to_s) : pace_seconds * distance
|
28
28
|
end
|
29
29
|
|
30
|
-
def distance(time, pace
|
30
|
+
def distance(time, pace)
|
31
31
|
check_time(time)
|
32
32
|
check_time(pace)
|
33
33
|
if bigdecimal
|
data/lib/calcpace/converter.rb
CHANGED
@@ -1,44 +1,55 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'bigdecimal'
|
4
|
-
|
5
|
-
module Converter
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
5
|
+
module Converter
|
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
|
10
|
+
|
11
|
+
def to_seconds(time)
|
12
|
+
check_time(time)
|
13
|
+
convert_to_seconds(time)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_clocktime(seconds)
|
17
|
+
check_integer(seconds)
|
18
|
+
convert_to_clocktime(seconds)
|
19
|
+
end
|
20
|
+
|
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)
|
30
|
+
check_distance(distance)
|
31
|
+
bigdecimal ? distance_to_convert = BigDecimal(distance.to_s) : distance_to_convert = distance
|
32
|
+
convert_the_distance(distance_to_convert, unit)
|
33
|
+
end
|
34
|
+
|
35
|
+
def convert_to_seconds(time)
|
36
|
+
hour, minute, seconds = time.split(':').map(&:to_i)
|
37
|
+
(hour * 3600) + (minute * 60) + seconds
|
38
|
+
end
|
39
|
+
|
40
|
+
def convert_to_clocktime(seconds)
|
41
|
+
format = seconds >= 86_400 ? '%d %H:%M:%S' : '%H:%M:%S'
|
42
|
+
Time.at(seconds.to_i).utc.strftime(format)
|
43
|
+
end
|
44
|
+
|
45
|
+
def convert_the_distance(distance, unit)
|
46
|
+
case unit
|
47
|
+
when 'mi'
|
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)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/calcpace.rb
CHANGED
@@ -1,68 +1,73 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
require 'bigdecimal'
|
5
|
-
require_relative '../../lib/calcpace'
|
6
|
-
|
7
|
-
class TestCalculator < Minitest::Test
|
8
|
-
def setup
|
9
|
-
@checker = Calcpace.new
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
assert_raises(RuntimeError) { @checker.pace('
|
15
|
-
assert_raises(RuntimeError) { @checker.pace('
|
16
|
-
assert_raises(RuntimeError) { @checker.pace('00:00:00',
|
17
|
-
|
18
|
-
assert_equal '00:
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
assert_raises(RuntimeError) { @checker.
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
assert_equal
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
assert_equal
|
67
|
-
|
68
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'bigdecimal'
|
5
|
+
require_relative '../../lib/calcpace'
|
6
|
+
|
7
|
+
class TestCalculator < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@checker = Calcpace.new
|
10
|
+
@checker_bigdecimal = Calcpace.new(bigdecimal = true)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_pace
|
14
|
+
assert_raises(RuntimeError) { @checker.pace('', 10) }
|
15
|
+
assert_raises(RuntimeError) { @checker.pace('invalid', 10) }
|
16
|
+
assert_raises(RuntimeError) { @checker.pace('00:00:00', 0) }
|
17
|
+
assert_raises(RuntimeError) { @checker.pace('00:00:00', -1) }
|
18
|
+
assert_equal '00:06:00', @checker.pace('01:00:00', 10)
|
19
|
+
assert_equal '00:07:54', @checker.pace('01:37:21', 12.3)
|
20
|
+
end
|
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
|
+
|
26
|
+
def test_pace_without_bigdecimal_precision
|
27
|
+
assert_equal '00:07:54', @checker.pace('01:37:21', 12.3)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_pace_seconds
|
31
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('', 10) }
|
32
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('invalid', 10) }
|
33
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', 0) }
|
34
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', -1) }
|
35
|
+
assert_equal 474.8780487804878, @checker.pace_seconds('01:37:21', 12.3)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_pace_seconds_with_bigdecimal_precision
|
39
|
+
assert_equal BigDecimal('0.474878048780487804878048780487804878049e3'), @checker_bigdecimal.pace_seconds('01:37:21', 12.3)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_total_time
|
43
|
+
assert_raises(RuntimeError) { @checker.total_time('', 10) }
|
44
|
+
assert_raises(RuntimeError) { @checker.total_time('invalid', 10) }
|
45
|
+
assert_raises(RuntimeError) { @checker.total_time('00:00:00', 0) }
|
46
|
+
assert_raises(RuntimeError) { @checker.total_time('00:00:00', -1) }
|
47
|
+
assert_equal '01:00:00', @checker.total_time('00:05:00', 12)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_total_time_seconds
|
51
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('', 10) }
|
52
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('invalid', 10) }
|
53
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', 0) }
|
54
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', -1) }
|
55
|
+
assert_equal 3600, @checker.total_time_seconds('00:05:00', 12)
|
56
|
+
assert_equal 71_844.3, @checker.total_time_seconds('01:37:21', 12.3)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_total_time_seconds_with_bigdecimal_precision
|
60
|
+
assert_equal BigDecimal('0.718443e5'), @checker_bigdecimal.total_time_seconds('01:37:21', 12.3)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_distance
|
64
|
+
assert_raises(RuntimeError) { @checker.distance('', '00:05:00') }
|
65
|
+
assert_raises(RuntimeError) { @checker.distance('01:00:00', '') }
|
66
|
+
assert_equal 18.0, @checker.distance('01:30:00', '00:05:00')
|
67
|
+
assert_equal 15.0, @checker.distance('01:37:21', '00:06:17')
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_distance_with_bigdecimal_precision
|
71
|
+
assert_equal BigDecimal('0.15493368700265251989389920424403183024e2'), @checker_bigdecimal.distance('01:37:21', '00:06:17')
|
72
|
+
end
|
73
|
+
end
|
@@ -1,49 +1,58 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
require_relative '../../lib/calcpace'
|
5
|
-
|
6
|
-
class TestConverter < Minitest::Test
|
7
|
-
def setup
|
8
|
-
@checker = Calcpace.new
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
assert_equal '
|
27
|
-
|
28
|
-
assert_raises(RuntimeError) { @checker.to_clocktime(
|
29
|
-
assert_raises(RuntimeError) { @checker.to_clocktime(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
assert_equal 6.
|
35
|
-
assert_equal 6.
|
36
|
-
assert_equal
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
assert_equal
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require_relative '../../lib/calcpace'
|
5
|
+
|
6
|
+
class TestConverter < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@checker = Calcpace.new
|
9
|
+
@checker_bigdecimal = Calcpace.new(bigdecimal = true)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_convert_to_seconds
|
13
|
+
assert_equal 4262, @checker.convert_to_seconds('01:11:02')
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_to_seconds
|
17
|
+
assert_equal 3600, @checker.to_seconds('01:00:00')
|
18
|
+
assert_raises(RuntimeError) { @checker.to_seconds('invalid') }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_convert_to_clocktime
|
22
|
+
assert_equal '01:11:02', @checker.convert_to_clocktime(4262)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_to_clocktime
|
26
|
+
assert_equal '01:00:00', @checker.to_clocktime(3600)
|
27
|
+
assert_equal '02 01:00:00', @checker.to_clocktime(90_000)
|
28
|
+
assert_raises(RuntimeError) { @checker.to_clocktime(-1) }
|
29
|
+
assert_raises(RuntimeError) { @checker.to_clocktime(0) }
|
30
|
+
assert_raises(RuntimeError) { @checker.to_clocktime('invalid') }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_convert_the_distance
|
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')
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_convert
|
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)
|
48
|
+
assert_raises(RuntimeError) { @checker.convert(-1, 'km') }
|
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)
|
57
|
+
end
|
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.
|
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-
|
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:
|
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: []
|