calcpace 0.2.0 → 1.0.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: b4525dc354de18e5ae106e84f2628eb649a28457deff01f9a4ecdf692a210d81
4
- data.tar.gz: b16f270b98de14001122a466d2d19d11e5316262ef5319944c25ac236ac56615
3
+ metadata.gz: 200091226ac840d686c80783b1ffdd25880e33d055023584bcf0d6ad4f80942b
4
+ data.tar.gz: 983b752d6d4569accd2deebeecf0d071dd7d5d7c4d86ad13949b0dc57992efcb
5
5
  SHA512:
6
- metadata.gz: 7827da6cf956685513b3f56728a9475f9d5d5843ea2808b51a9a83800787779e71d4417c45d4e03779e6eb077a3158b1ca19c841c1ded861682ac8fa8f72beab
7
- data.tar.gz: e17f9a94e680266f856fe94d36c57cea4988d10c4941dd415af7e0efe5b52d801a2e4721dd8758a2ae1d0228685f4c6a27208fad462b6a1d081e384152bbd248
6
+ metadata.gz: bc18311a99f36de42c7684bab706665d875abf9d579ca31a05d2cebbd8ae75be66f6c34d5242d6d76538f78198c7ef4931a427c43a62b444abf1e0c8cfdc89da
7
+ data.tar.gz: '0086fe8432f284529da6c6c7ded77488895360da55542f8ca6f50c8fd2976e3d8c2bf2e22d9c56cd10f9a560a4ad4666a12fc34655dbea263cd4f698eafb4e9c'
@@ -1,25 +1,21 @@
1
- # frozen_string_literal: true
2
-
3
- module Calculator
4
- def pace(time, distance)
5
- check_digits(time, distance)
6
- convert_to_clocktime(convert_to_seconds(time) / distance.to_f)
7
- end
8
-
9
- def total_time(pace, distance)
10
- check_digits(pace, distance)
11
- convert_to_clocktime(convert_to_seconds(pace) * distance.to_f)
12
- end
13
-
14
- def distance(time, pace)
15
- check_digits_time(time)
16
- check_digits_time(pace)
17
- convert_to_seconds(time).to_f / convert_to_seconds(pace).round(2)
18
- end
19
-
20
- def convert_distance(distance, unit)
21
- check_digits_distance(distance)
22
- check_unit(unit)
23
- convert(distance, unit)
24
- end
25
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Calculator
4
+ def pace(time, distance)
5
+ check_time(time)
6
+ check_distance(distance)
7
+ convert_to_clocktime(convert_to_seconds(time) / distance.to_f)
8
+ end
9
+
10
+ def total_time(pace, distance)
11
+ check_time(pace)
12
+ check_distance(distance)
13
+ convert_to_clocktime(convert_to_seconds(pace) * distance.to_f)
14
+ end
15
+
16
+ def distance(time, pace)
17
+ check_time(time)
18
+ check_time(pace)
19
+ convert_to_seconds(time).to_f / convert_to_seconds(pace).round(2)
20
+ end
21
+ end
@@ -1,20 +1,19 @@
1
- # frozen_string_literal: true
2
-
3
- module Checker
4
- def check_digits(time, distance)
5
- check_digits_time(time)
6
- check_digits_distance(distance)
7
- end
8
-
9
- def check_digits_distance(distance)
10
- raise 'It must be a X.X positive number' unless distance.positive?
11
- end
12
-
13
- def check_digits_time(time_string)
14
- raise 'It must be a XX:XX:XX time' unless time_string =~ /\d{0,2}(:)*?\d{1,2}(:)\d{1,2}/
15
- end
16
-
17
- def check_unit(unit)
18
- raise 'It must be km or mi' unless %w[km mi].include?(unit)
19
- end
20
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Checker
4
+ def check_distance(distance)
5
+ raise 'It must be a X.X positive number' unless distance.positive?
6
+ end
7
+
8
+ def check_time(time_string)
9
+ raise 'It must be a XX:XX:XX time' unless time_string =~ /\d{0,2}(:)*?\d{1,2}(:)\d{1,2}/
10
+ end
11
+
12
+ def check_integer(second)
13
+ raise 'It must be a positive number' unless second.is_a?(Integer) && second.positive?
14
+ end
15
+
16
+ def check_unit(unit)
17
+ raise 'It must be km or mi' unless %w[km mi].include?(unit)
18
+ end
19
+ end
@@ -1,22 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Converter
4
+ def to_seconds(time)
5
+ check_time(time)
6
+ convert_to_seconds(time)
7
+ end
8
+
9
+ def to_clocktime(seconds)
10
+ check_integer(seconds)
11
+ convert_to_clocktime(seconds)
12
+ end
13
+
14
+ def convert(distance, unit, round_limit = 2)
15
+ check_distance(distance)
16
+ check_unit(unit)
17
+ check_integer(round_limit)
18
+ convert_the_distance(distance, unit, round_limit)
19
+ end
20
+
4
21
  def convert_to_seconds(time)
5
22
  hour, minute, seconds = time.split(':').map(&:to_i)
6
23
  (hour * 3600) + (minute * 60) + seconds
7
24
  end
8
25
 
9
26
  def convert_to_clocktime(seconds)
10
- Time.at(seconds).utc.strftime('%H:%M:%S')
27
+ seconds >= 86_400 ? time = '%d %H:%M:%S' : time = '%H:%M:%S'
28
+ Time.at(seconds).utc.strftime(time)
11
29
  end
12
30
 
13
- # tem que chamar as checagens de digitos antes de chamar o convert_distance
14
- def convert(distance, unit)
31
+ def convert_the_distance(distance, unit, round_limit = 2)
15
32
  case unit
16
33
  when 'km'
17
- (distance * 0.621371).round(2)
34
+ (distance * 0.621371).round(round_limit)
18
35
  when 'mi'
19
- (distance * 1.60934).round(2)
36
+ (distance * 1.60934).round(round_limit)
20
37
  end
21
38
  end
22
39
  end
data/lib/calcpace.rb CHANGED
@@ -1,13 +1,13 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'calcpace/calculator'
4
- require_relative 'calcpace/checker'
5
- require_relative 'calcpace/converter'
6
-
7
- class Calcpace
8
- include Calculator
9
- include Checker
10
- include Converter
11
-
12
- def initialize; end
13
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'calcpace/calculator'
4
+ require_relative 'calcpace/checker'
5
+ require_relative 'calcpace/converter'
6
+
7
+ class Calcpace
8
+ include Calculator
9
+ include Checker
10
+ include Converter
11
+
12
+ def initialize; end
13
+ end
@@ -1,40 +1,32 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require_relative '../../lib/calcpace'
5
-
6
- class TestCalculator < Minitest::Test
7
- def setup
8
- @checker = Calcpace.new
9
- end
10
-
11
- def test_pace
12
- assert_raises(RuntimeError) { @checker.pace('', 10) }
13
- assert_raises(RuntimeError) { @checker.pace('invalid', 10) }
14
- assert_raises(RuntimeError) { @checker.pace('00:00:00', 0) }
15
- assert_raises(RuntimeError) { @checker.pace('00:00:00', -1) }
16
- assert_equal '00:06:00', @checker.pace('01:00:00', 10)
17
- end
18
-
19
- def test_total_time
20
- assert_raises(RuntimeError) { @checker.total_time('', 10) }
21
- assert_raises(RuntimeError) { @checker.total_time('invalid', 10) }
22
- assert_raises(RuntimeError) { @checker.total_time('00:00:00', 0) }
23
- assert_raises(RuntimeError) { @checker.total_time('00:00:00', -1) }
24
- assert_equal '01:00:00', @checker.total_time('00:05:00', 12)
25
- end
26
-
27
- def test_distance
28
- assert_raises(RuntimeError) { @checker.distance('', '00:05:00') }
29
- assert_raises(RuntimeError) { @checker.distance('01:00:00', '') }
30
- assert_equal 18.0, @checker.distance('01:30:00', '00:05:00')
31
- end
32
-
33
- def test_convert_distance
34
- assert_raises(RuntimeError) { @checker.convert_distance(10, 'invalid') }
35
- assert_raises(RuntimeError) { @checker.convert_distance(-1, 'km') }
36
- assert_raises(RuntimeError) { @checker.convert_distance(0, 'km') }
37
- assert_equal 6.21, @checker.convert_distance(10, 'km')
38
- assert_equal 16.09, @checker.convert_distance(10, 'mi')
39
- end
40
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require_relative '../../lib/calcpace'
5
+
6
+ class TestCalculator < Minitest::Test
7
+ def setup
8
+ @checker = Calcpace.new
9
+ end
10
+
11
+ def test_pace
12
+ assert_raises(RuntimeError) { @checker.pace('', 10) }
13
+ assert_raises(RuntimeError) { @checker.pace('invalid', 10) }
14
+ assert_raises(RuntimeError) { @checker.pace('00:00:00', 0) }
15
+ assert_raises(RuntimeError) { @checker.pace('00:00:00', -1) }
16
+ assert_equal '00:06:00', @checker.pace('01:00:00', 10)
17
+ end
18
+
19
+ def test_total_time
20
+ assert_raises(RuntimeError) { @checker.total_time('', 10) }
21
+ assert_raises(RuntimeError) { @checker.total_time('invalid', 10) }
22
+ assert_raises(RuntimeError) { @checker.total_time('00:00:00', 0) }
23
+ assert_raises(RuntimeError) { @checker.total_time('00:00:00', -1) }
24
+ assert_equal '01:00:00', @checker.total_time('00:05:00', 12)
25
+ end
26
+
27
+ def test_distance
28
+ assert_raises(RuntimeError) { @checker.distance('', '00:05:00') }
29
+ assert_raises(RuntimeError) { @checker.distance('01:00:00', '') }
30
+ assert_equal 18.0, @checker.distance('01:30:00', '00:05:00')
31
+ end
32
+ end
@@ -1,32 +1,27 @@
1
- # frozen_string_literal: true
2
-
3
- require 'minitest/autorun'
4
- require_relative '../../lib/calcpace'
5
-
6
- class TestChecker < Minitest::Test
7
- def setup
8
- @checker = Calcpace.new
9
- end
10
-
11
- def test_check_digits_distance
12
- assert_raises(RuntimeError) { @checker.check_digits_distance(-1) }
13
- assert_raises(RuntimeError) { @checker.check_digits_distance(0) }
14
- assert_nil @checker.check_digits_distance(1)
15
- end
16
-
17
- def test_check_digits_time
18
- assert_raises(RuntimeError) { @checker.check_digits_time('') }
19
- assert_nil @checker.check_digits_time('00:00:00')
20
- end
21
-
22
- def test_check_digits
23
- assert_raises(RuntimeError) { @checker.check_digits('00:00:00', 0) }
24
- assert_raises(RuntimeError) { @checker.check_digits('', 1) }
25
- assert_nil @checker.check_digits('00:00:00', 1)
26
- end
27
-
28
- def test_convert
29
- assert_equal 6.21, @checker.convert(10, 'km')
30
- assert_equal 16.09, @checker.convert(10, 'mi')
31
- end
32
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require_relative '../../lib/calcpace'
5
+
6
+ class TestChecker < Minitest::Test
7
+ def setup
8
+ @checker = Calcpace.new
9
+ end
10
+
11
+ def test_check_distance
12
+ assert_raises(RuntimeError) { @checker.check_distance(-1) }
13
+ assert_raises(RuntimeError) { @checker.check_distance(0) }
14
+ assert_nil @checker.check_distance(1)
15
+ end
16
+
17
+ def test_check_time
18
+ assert_raises(RuntimeError) { @checker.check_time('') }
19
+ assert_nil @checker.check_time('00:00:00')
20
+ end
21
+
22
+ def test_check_integer
23
+ assert_raises(RuntimeError) { @checker.check_integer(-1) }
24
+ assert_raises(RuntimeError) { @checker.check_integer(0) }
25
+ assert_nil @checker.check_integer(1)
26
+ end
27
+ end
@@ -1,24 +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_convert_to_clocktime
16
- assert_equal '01:11:02', @checker.convert_to_clocktime(4262)
17
- end
18
-
19
- def test_convert_distance_km
20
- assert_equal 6.21, @checker.convert_distance(10, 'km')
21
- assert_equal 16.09, @checker.convert_distance(10, 'mi')
22
- assert_raises(RuntimeError) { @checker.convert_distance(10, 'invalid') }
23
- end
24
- 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(90000)
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: 0.2.0
4
+ version: 1.0.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-05-25 00:00:00.000000000 Z
11
+ date: 2024-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  requirements: []
133
- rubygems_version: 3.2.33
133
+ rubygems_version: 3.4.6
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: 'Calcpace: calculate time and distances for activities such as running and