calcpace 1.0.0 → 1.1.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 +27 -8
- data/lib/calcpace/converter.rb +11 -6
- data/test/calcpace/test_calculator.rb +36 -0
- 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: 4076fd00047c9b763ee7c4d38d5b3c75b638ec851c0af06895c70d050a122b10
|
4
|
+
data.tar.gz: 2c69b89a9f5508cd58a5f34b33f67517f6ac9a37218ae0e01fbbed600a269712
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '029f28e39b899c3d2ddc13c873db6ed76a3a3b743080c4c167263a592550c56f1dd68bb19e4bb353c15846c63a339d6ba07d7e1a33c1ce83ff8d3159f059d5e7'
|
7
|
+
data.tar.gz: 2878a6024448fd28264e9b7df4d26fc19f2f41d02016dab82caa7c87f46b8b3bce0de55e5edaff627ca5ffa1b945e2461e5a3d49188df12f533a0cf003011eeb
|
data/lib/calcpace/calculator.rb
CHANGED
@@ -1,21 +1,40 @@
|
|
1
|
-
|
1
|
+
require 'bigdecimal'
|
2
2
|
|
3
3
|
module Calculator
|
4
|
-
def pace(time, distance)
|
4
|
+
def pace(time, distance, bigdecimal = false)
|
5
|
+
pace_in_seconds = pace_seconds(time, distance, bigdecimal)
|
6
|
+
convert_to_clocktime(pace_in_seconds)
|
7
|
+
end
|
8
|
+
|
9
|
+
def pace_seconds(time, distance, bigdecimal = false)
|
5
10
|
check_time(time)
|
6
11
|
check_distance(distance)
|
7
|
-
|
12
|
+
seconds = convert_to_seconds(time)
|
13
|
+
bigdecimal ? seconds / BigDecimal(distance.to_s) : seconds / distance
|
14
|
+
end
|
15
|
+
|
16
|
+
def total_time(pace, distance, bigdecimal = false)
|
17
|
+
total_time_in_seconds = total_time_seconds(pace, distance, bigdecimal)
|
18
|
+
convert_to_clocktime(total_time_in_seconds)
|
8
19
|
end
|
9
20
|
|
10
|
-
def
|
21
|
+
def total_time_seconds(pace, distance, bigdecimal = false)
|
11
22
|
check_time(pace)
|
12
23
|
check_distance(distance)
|
13
|
-
|
24
|
+
pace_seconds = convert_to_seconds(pace)
|
25
|
+
bigdecimal ? pace_seconds * BigDecimal(distance.to_s) : pace_seconds * distance
|
14
26
|
end
|
15
27
|
|
16
|
-
def distance(time, pace)
|
28
|
+
def distance(time, pace, bigdecimal = false)
|
17
29
|
check_time(time)
|
18
30
|
check_time(pace)
|
19
|
-
|
31
|
+
if bigdecimal
|
32
|
+
time_seconds = BigDecimal(convert_to_seconds(time).to_s)
|
33
|
+
pace_seconds = BigDecimal(convert_to_seconds(pace).to_s)
|
34
|
+
else
|
35
|
+
time_seconds = convert_to_seconds(time)
|
36
|
+
pace_seconds = convert_to_seconds(pace)
|
37
|
+
end
|
38
|
+
time_seconds / pace_seconds
|
20
39
|
end
|
21
|
-
end
|
40
|
+
end
|
data/lib/calcpace/converter.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
3
5
|
module Converter
|
6
|
+
KM_TO_MI = BigDecimal("0.621371")
|
7
|
+
MI_TO_KM = BigDecimal("1.60934")
|
8
|
+
|
4
9
|
def to_seconds(time)
|
5
10
|
check_time(time)
|
6
11
|
convert_to_seconds(time)
|
@@ -15,7 +20,7 @@ module Converter
|
|
15
20
|
check_distance(distance)
|
16
21
|
check_unit(unit)
|
17
22
|
check_integer(round_limit)
|
18
|
-
convert_the_distance(distance, unit, round_limit)
|
23
|
+
convert_the_distance(BigDecimal(distance.to_s), unit, round_limit)
|
19
24
|
end
|
20
25
|
|
21
26
|
def convert_to_seconds(time)
|
@@ -24,16 +29,16 @@ module Converter
|
|
24
29
|
end
|
25
30
|
|
26
31
|
def convert_to_clocktime(seconds)
|
27
|
-
seconds >= 86_400 ?
|
28
|
-
Time.at(seconds).utc.strftime(
|
32
|
+
format = seconds >= 86_400 ? '%d %H:%M:%S' : '%H:%M:%S'
|
33
|
+
Time.at(seconds.to_i).utc.strftime(format)
|
29
34
|
end
|
30
35
|
|
31
36
|
def convert_the_distance(distance, unit, round_limit = 2)
|
32
37
|
case unit
|
33
38
|
when 'km'
|
34
|
-
(distance *
|
39
|
+
(distance * KM_TO_MI).round(round_limit)
|
35
40
|
when 'mi'
|
36
|
-
(distance *
|
41
|
+
(distance * MI_TO_KM).round(round_limit)
|
37
42
|
end
|
38
43
|
end
|
39
|
-
end
|
44
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'minitest/autorun'
|
4
|
+
require 'bigdecimal'
|
4
5
|
require_relative '../../lib/calcpace'
|
5
6
|
|
6
7
|
class TestCalculator < Minitest::Test
|
@@ -14,6 +15,23 @@ class TestCalculator < Minitest::Test
|
|
14
15
|
assert_raises(RuntimeError) { @checker.pace('00:00:00', 0) }
|
15
16
|
assert_raises(RuntimeError) { @checker.pace('00:00:00', -1) }
|
16
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)
|
17
35
|
end
|
18
36
|
|
19
37
|
def test_total_time
|
@@ -24,9 +42,27 @@ class TestCalculator < Minitest::Test
|
|
24
42
|
assert_equal '01:00:00', @checker.total_time('00:05:00', 12)
|
25
43
|
end
|
26
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 71844.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
|
+
|
27
58
|
def test_distance
|
28
59
|
assert_raises(RuntimeError) { @checker.distance('', '00:05:00') }
|
29
60
|
assert_raises(RuntimeError) { @checker.distance('01:00:00', '') }
|
30
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)
|
31
67
|
end
|
32
68
|
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.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-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
|