calcpace 1.0.0 → 1.1.1
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 +27 -6
- data/lib/calcpace/converter.rb +44 -39
- data/test/calcpace/test_calculator.rb +68 -32
- data/test/calcpace/test_converter.rb +49 -49
- metadata +12 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08aafc87c8fa3b75db3dfac8e186f803cbfec584991945e9849648a0d91c7341'
|
4
|
+
data.tar.gz: 886d0b4b83bc2bbc07b3f2dc5d4b3b605459cc15df13a51c22e4ba87f2620ff4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e3b69e165c445f5c2885c91a8b866020095ba3de87431817eed0d75df2f6f3e65da15cb73205904cff8c3844d590ce3b3edeabdf91cd74f31bbeafe84ac085c
|
7
|
+
data.tar.gz: 311f84fa56b4f5d3bec6e0c16cac2d6650a488828058f169c8bf1f12c410bff8a91c9be667ffd30ed6dcf40a856f5c64a63aa4b9386095f9a0fdbd7c83548844
|
data/lib/calcpace/calculator.rb
CHANGED
@@ -1,21 +1,42 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
3
5
|
module Calculator
|
4
|
-
def pace(time, distance)
|
6
|
+
def pace(time, distance, bigdecimal = false)
|
7
|
+
pace_in_seconds = pace_seconds(time, distance, bigdecimal)
|
8
|
+
convert_to_clocktime(pace_in_seconds)
|
9
|
+
end
|
10
|
+
|
11
|
+
def pace_seconds(time, distance, bigdecimal = false)
|
5
12
|
check_time(time)
|
6
13
|
check_distance(distance)
|
7
|
-
|
14
|
+
seconds = convert_to_seconds(time)
|
15
|
+
bigdecimal ? seconds / BigDecimal(distance.to_s) : seconds / distance
|
16
|
+
end
|
17
|
+
|
18
|
+
def total_time(pace, distance, bigdecimal = false)
|
19
|
+
total_time_in_seconds = total_time_seconds(pace, distance, bigdecimal)
|
20
|
+
convert_to_clocktime(total_time_in_seconds)
|
8
21
|
end
|
9
22
|
|
10
|
-
def
|
23
|
+
def total_time_seconds(pace, distance, bigdecimal = false)
|
11
24
|
check_time(pace)
|
12
25
|
check_distance(distance)
|
13
|
-
|
26
|
+
pace_seconds = convert_to_seconds(pace)
|
27
|
+
bigdecimal ? pace_seconds * BigDecimal(distance.to_s) : pace_seconds * distance
|
14
28
|
end
|
15
29
|
|
16
|
-
def distance(time, pace)
|
30
|
+
def distance(time, pace, bigdecimal = false)
|
17
31
|
check_time(time)
|
18
32
|
check_time(pace)
|
19
|
-
|
33
|
+
if bigdecimal
|
34
|
+
time_seconds = BigDecimal(convert_to_seconds(time).to_s)
|
35
|
+
pace_seconds = BigDecimal(convert_to_seconds(pace).to_s)
|
36
|
+
else
|
37
|
+
time_seconds = convert_to_seconds(time)
|
38
|
+
pace_seconds = convert_to_seconds(pace)
|
39
|
+
end
|
40
|
+
time_seconds / pace_seconds
|
20
41
|
end
|
21
42
|
end
|
data/lib/calcpace/converter.rb
CHANGED
@@ -1,39 +1,44 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
(
|
24
|
-
end
|
25
|
-
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
5
|
+
module Converter
|
6
|
+
KM_TO_MI = BigDecimal('0.621371')
|
7
|
+
MI_TO_KM = BigDecimal('1.60934')
|
8
|
+
|
9
|
+
def to_seconds(time)
|
10
|
+
check_time(time)
|
11
|
+
convert_to_seconds(time)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_clocktime(seconds)
|
15
|
+
check_integer(seconds)
|
16
|
+
convert_to_clocktime(seconds)
|
17
|
+
end
|
18
|
+
|
19
|
+
def convert(distance, unit, round_limit = 2)
|
20
|
+
check_distance(distance)
|
21
|
+
check_unit(unit)
|
22
|
+
check_integer(round_limit)
|
23
|
+
convert_the_distance(BigDecimal(distance.to_s), unit, round_limit)
|
24
|
+
end
|
25
|
+
|
26
|
+
def convert_to_seconds(time)
|
27
|
+
hour, minute, seconds = time.split(':').map(&:to_i)
|
28
|
+
(hour * 3600) + (minute * 60) + seconds
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_to_clocktime(seconds)
|
32
|
+
format = seconds >= 86_400 ? '%d %H:%M:%S' : '%H:%M:%S'
|
33
|
+
Time.at(seconds.to_i).utc.strftime(format)
|
34
|
+
end
|
35
|
+
|
36
|
+
def convert_the_distance(distance, unit, round_limit = 2)
|
37
|
+
case unit
|
38
|
+
when 'km'
|
39
|
+
(distance * KM_TO_MI).round(round_limit)
|
40
|
+
when 'mi'
|
41
|
+
(distance * MI_TO_KM).round(round_limit)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,32 +1,68 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
assert_raises(RuntimeError) { @checker.pace('
|
14
|
-
assert_raises(RuntimeError) { @checker.pace('
|
15
|
-
assert_raises(RuntimeError) { @checker.pace('00:00:00',
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
assert_raises(RuntimeError) { @checker.
|
29
|
-
assert_raises(RuntimeError) { @checker.
|
30
|
-
assert_equal
|
31
|
-
end
|
32
|
-
|
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
|
+
end
|
11
|
+
|
12
|
+
def test_pace
|
13
|
+
assert_raises(RuntimeError) { @checker.pace('', 10) }
|
14
|
+
assert_raises(RuntimeError) { @checker.pace('invalid', 10) }
|
15
|
+
assert_raises(RuntimeError) { @checker.pace('00:00:00', 0) }
|
16
|
+
assert_raises(RuntimeError) { @checker.pace('00:00:00', -1) }
|
17
|
+
assert_equal '00:06:00', @checker.pace('01:00:00', 10)
|
18
|
+
assert_equal '00:07:54', @checker.pace('01:37:21', 12.3)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_pace_without_bigdecimal_precision
|
22
|
+
assert_equal '00:07:54', @checker.pace('01:37:21', 12.3, false)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_pace_seconds
|
26
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('', 10) }
|
27
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('invalid', 10) }
|
28
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', 0) }
|
29
|
+
assert_raises(RuntimeError) { @checker.pace_seconds('00:00:00', -1) }
|
30
|
+
assert_equal BigDecimal('474.8780487804878'), @checker.pace_seconds('01:37:21', 12.3)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_pace_seconds_with_bigdecimal_precision
|
34
|
+
assert_equal BigDecimal('0.474878048780487804878048780487804878049e3'), @checker.pace_seconds('01:37:21', 12.3, true)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_total_time
|
38
|
+
assert_raises(RuntimeError) { @checker.total_time('', 10) }
|
39
|
+
assert_raises(RuntimeError) { @checker.total_time('invalid', 10) }
|
40
|
+
assert_raises(RuntimeError) { @checker.total_time('00:00:00', 0) }
|
41
|
+
assert_raises(RuntimeError) { @checker.total_time('00:00:00', -1) }
|
42
|
+
assert_equal '01:00:00', @checker.total_time('00:05:00', 12)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_total_time_seconds
|
46
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('', 10) }
|
47
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('invalid', 10) }
|
48
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', 0) }
|
49
|
+
assert_raises(RuntimeError) { @checker.total_time_seconds('00:00:00', -1) }
|
50
|
+
assert_equal 3600, @checker.total_time_seconds('00:05:00', 12)
|
51
|
+
assert_equal 71_844.3, @checker.total_time_seconds('01:37:21', 12.3)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_total_time_seconds_with_bigdecimal_precision
|
55
|
+
assert_equal BigDecimal('0.718443e5'), @checker.total_time_seconds('01:37:21', 12.3, true)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_distance
|
59
|
+
assert_raises(RuntimeError) { @checker.distance('', '00:05:00') }
|
60
|
+
assert_raises(RuntimeError) { @checker.distance('01:00:00', '') }
|
61
|
+
assert_equal 18.0, @checker.distance('01:30:00', '00:05:00')
|
62
|
+
assert_equal 15.0, @checker.distance('01:37:21', '00:06:17')
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_distance_with_bigdecimal_precision
|
66
|
+
assert_equal BigDecimal('0.15493368700265251989389920424403183024e2'), @checker.distance('01:37:21', '00:06:17', true)
|
67
|
+
end
|
68
|
+
end
|
@@ -1,49 +1,49 @@
|
|
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
|
-
end
|
10
|
-
|
11
|
-
def test_convert_to_seconds
|
12
|
-
assert_equal 4262, @checker.convert_to_seconds('01:11:02')
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_to_seconds
|
16
|
-
assert_equal 3600, @checker.to_seconds('01:00:00')
|
17
|
-
assert_raises(RuntimeError) { @checker.to_seconds('invalid') }
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_convert_to_clocktime
|
21
|
-
assert_equal '01:11:02', @checker.convert_to_clocktime(4262)
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_to_clocktime
|
25
|
-
assert_equal '01:00:00', @checker.to_clocktime(3600)
|
26
|
-
assert_equal '02 01:00:00', @checker.to_clocktime(
|
27
|
-
assert_raises(RuntimeError) { @checker.to_clocktime(-1) }
|
28
|
-
assert_raises(RuntimeError) { @checker.to_clocktime(0) }
|
29
|
-
assert_raises(RuntimeError) { @checker.to_clocktime('invalid') }
|
30
|
-
end
|
31
|
-
|
32
|
-
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')
|
37
|
-
end
|
38
|
-
|
39
|
-
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') }
|
44
|
-
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') }
|
48
|
-
end
|
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
|
+
end
|
10
|
+
|
11
|
+
def test_convert_to_seconds
|
12
|
+
assert_equal 4262, @checker.convert_to_seconds('01:11:02')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_to_seconds
|
16
|
+
assert_equal 3600, @checker.to_seconds('01:00:00')
|
17
|
+
assert_raises(RuntimeError) { @checker.to_seconds('invalid') }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_convert_to_clocktime
|
21
|
+
assert_equal '01:11:02', @checker.convert_to_clocktime(4262)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_to_clocktime
|
25
|
+
assert_equal '01:00:00', @checker.to_clocktime(3600)
|
26
|
+
assert_equal '02 01:00:00', @checker.to_clocktime(90_000)
|
27
|
+
assert_raises(RuntimeError) { @checker.to_clocktime(-1) }
|
28
|
+
assert_raises(RuntimeError) { @checker.to_clocktime(0) }
|
29
|
+
assert_raises(RuntimeError) { @checker.to_clocktime('invalid') }
|
30
|
+
end
|
31
|
+
|
32
|
+
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')
|
37
|
+
end
|
38
|
+
|
39
|
+
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') }
|
44
|
+
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') }
|
48
|
+
end
|
49
|
+
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.1.1
|
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-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -94,9 +94,12 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.11'
|
97
|
-
description:
|
98
|
-
|
99
|
-
|
97
|
+
description: 'Calcpace is a Ruby gem that helps with calculations related to running/cycling
|
98
|
+
activities or general purposes involving distance and time. It can calculate pace,
|
99
|
+
total time, and distance. It also converts distances between miles and kilometers
|
100
|
+
and check formats of time and distance. The results are provided in a readable format,
|
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, '
|
100
103
|
email: joaogilberto@tuta.io
|
101
104
|
executables: []
|
102
105
|
extensions: []
|
@@ -114,8 +117,7 @@ licenses:
|
|
114
117
|
- MIT
|
115
118
|
metadata:
|
116
119
|
source_code_uri: https://github.com/0jonjo/calcpace
|
117
|
-
post_install_message: It's time to
|
118
|
-
exercising! Thank you for installing Calcpace!
|
120
|
+
post_install_message: It's time to calculate! Thank you for installing Calcpace.
|
119
121
|
rdoc_options: []
|
120
122
|
require_paths:
|
121
123
|
- lib
|
@@ -130,11 +132,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
132
|
- !ruby/object:Gem::Version
|
131
133
|
version: '0'
|
132
134
|
requirements: []
|
133
|
-
rubygems_version: 3.
|
135
|
+
rubygems_version: 3.5.16
|
134
136
|
signing_key:
|
135
137
|
specification_version: 4
|
136
|
-
summary: 'Calcpace: calculate time
|
137
|
-
|
138
|
+
summary: 'Calcpace: calculate time, distance, pace, velocity and convert distances
|
139
|
+
in an easy and precise way.'
|
138
140
|
test_files:
|
139
141
|
- test/calcpace/test_calculator.rb
|
140
142
|
- test/calcpace/test_checker.rb
|