runby_pace 0.6.146 → 0.6.147

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ce07ece04ad62c724f57a747d3d7a530ca8ca925
4
- data.tar.gz: 9051f2d65022fa56493b3aa12fc6a14eaf0f4679
3
+ metadata.gz: a23743b3322eb2563d9bacaf2d940fb3dd3ba7e4
4
+ data.tar.gz: a3185adb6de60b111f7dc12727f3407638f94cf5
5
5
  SHA512:
6
- metadata.gz: ad59c1d1f16666cd0c05086bc77d51cf7a8c3e89323f07e0b48b92a8569207fb3045d2b04cc718f00947a233f25f0dedd61ef25f19be7638b2838728da93bca3
7
- data.tar.gz: 2a9cd5bf6f551b757b05c55ad23093a0cba8fdc385f7722209cbd7cd816636c943e07c38fc0a3a1f725596166398be82fce2fb7eccab9a2948f4e5005b0dfe41
6
+ metadata.gz: 426b99ff4fc2dfac2252bfc7b99f5f6ce720686f28cd7196f4fdcf236d147c406d5d287594ad7839296ffa5990a7f2d0eb7db973b719bb4e58c753caafd4e487
7
+ data.tar.gz: 9e39d983f87a91430e0d04aee92200f3208810e5752bdf638791527830b2445d4fee1ab04c7b42b67a1c16657fd12c5344fecdb4f12b3482d41a333238c49384
@@ -30,6 +30,7 @@ module Runby
30
30
  end
31
31
 
32
32
  def self.parse(description)
33
+ return new description if description.is_a? Symbol
33
34
  description = description.strip.chomp.downcase
34
35
  found_uom = nil
35
36
  UOM_DEFINITIONS.each do |uom, details|
@@ -117,7 +117,7 @@ module Runby
117
117
  end
118
118
 
119
119
  def distance_covered_over_time(time)
120
- time = Runby::RunbyTime.new(time)
120
+ time = sanitize_arg(time).as(RunbyTime)
121
121
  if time.total_minutes.zero? || @distance.multiplier.zero?
122
122
  return Runby::Distance.new(@distance.uom, 0)
123
123
  end
@@ -41,19 +41,26 @@ module Runby
41
41
  # Calculate the prescribed pace for the given 5K time
42
42
  # @return [Pace]
43
43
  def calc(five_k_time, distance_units = :km)
44
- five_k_time = Runby::RunbyTime.new five_k_time
45
- distance_units = Runby::DistanceUnit.new distance_units
46
- x2 = ((five_k_time.total_minutes * 2) - (MIDPOINT_X - 1)) - 1
47
- minutes_per_km = slope * x2 + @fastest_pace_km.time.total_minutes + curve_minutes(x2)
48
- minutes_per_unit = minutes_per_km * distance_units.conversion_factor
44
+ five_k_time = sanitize_arg(five_k_time).as(RunbyTime)
45
+ distance_units = sanitize_arg(distance_units).as(DistanceUnit)
46
+
47
+ minutes_per_unit = calculate_minutes_per_unit(distance_units, five_k_time)
48
+ build_pace minutes_per_unit, distance_units
49
+ end
50
+
51
+ private
49
52
 
50
- # TODO: Is there a way to clean up all of this "newing up"?
53
+ def build_pace(minutes_per_unit, distance_units)
51
54
  time = RunbyTime.from_minutes(minutes_per_unit)
52
55
  distance = Distance.new distance_units, 1
53
56
  Pace.new time, distance
54
57
  end
55
58
 
56
- private
59
+ def calculate_minutes_per_unit(distance_units, five_k_time)
60
+ x2 = ((five_k_time.total_minutes * 2) - (MIDPOINT_X - 1)) - 1
61
+ minutes_per_km = slope * x2 + @fastest_pace_km.time.total_minutes + curve_minutes(x2)
62
+ minutes_per_km * distance_units.conversion_factor
63
+ end
57
64
 
58
65
  # Since the paces for each 5K time do not progress in a straight line
59
66
  # when plotted on a graph, but rather a curve with its highest point near
@@ -69,7 +76,7 @@ module Runby
69
76
  midpoint = MIDPOINT_X
70
77
  if midpoint_reduction > midpoint
71
78
  midpoint_reduction = midpoint - (midpoint_reduction - midpoint)
72
- midpoint_reduction = 0 if midpoint_reduction < 0
79
+ midpoint_reduction = 0 if midpoint_reduction.negative?
73
80
  end
74
81
  # TODO: Use an actual curve instead of a triangle to calculate the number of minutes to add.
75
82
  midpoint_reduction / @midpoint_radius_divisor / 60
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ def sanitize_arg(parameter)
4
+ Runby::ParameterSanitizer.sanitize(parameter)
5
+ end
6
+
7
+ module Runby
8
+ # Sanitizes method parameters
9
+ class ParameterSanitizer
10
+ attr_reader :parameter
11
+
12
+ def initialize(parameter)
13
+ @parameter = parameter
14
+ end
15
+
16
+ def self.sanitize(parameter)
17
+ ParameterSanitizer.new parameter
18
+ end
19
+
20
+ def as(type)
21
+ return @parameter if @parameter.is_a?(type)
22
+ raise "Unable to sanitize parameter of type #{type}. Missing 'parse' method." unless type.respond_to? :parse
23
+ type.parse(@parameter)
24
+ end
25
+ end
26
+ end
@@ -4,9 +4,9 @@ module Runby
4
4
  # An assortment of mathematical functions related to running.
5
5
  class RunMath
6
6
  def self.predict_race_time(race1_distance, race1_time, target_distance)
7
- race1_distance = Distance.new(race1_distance)
8
- race1_time = RunbyTime.new(race1_time)
9
- target_distance = Distance.new(target_distance)
7
+ race1_distance = sanitize_arg(race1_distance).as(Distance)
8
+ race1_time = sanitize_arg(race1_time).as(RunbyTime)
9
+ target_distance = sanitize_arg(target_distance).as(Distance)
10
10
 
11
11
  race1_time * (target_distance / race1_distance)**1.06
12
12
  end
@@ -23,7 +23,7 @@ module Runby
23
23
  else
24
24
  fast_multiplier = format('%g', @fast.distance.multiplier.round(2))
25
25
  slow_multiplier = format('%g', @slow.distance.multiplier.round(2))
26
- @fast.to_s(format: format).sub("#{fast_multiplier}", "#{fast_multiplier}-#{slow_multiplier}")
26
+ @fast.to_s(format: format).sub(fast_multiplier.to_s, "#{fast_multiplier}-#{slow_multiplier}")
27
27
  end
28
28
  end
29
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Runby
4
- VERSION = "0.6.#{`git rev-list --count HEAD`}".freeze
4
+ VERSION = "0.6.#{`git rev-list --count HEAD`}"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runby_pace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.146
4
+ version: 0.6.147
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ty Walls
@@ -79,6 +79,7 @@ files:
79
79
  - lib/runby_pace/pace.rb
80
80
  - lib/runby_pace/pace_calculator.rb
81
81
  - lib/runby_pace/pace_range.rb
82
+ - lib/runby_pace/parameter_sanitizer.rb
82
83
  - lib/runby_pace/run_math.rb
83
84
  - lib/runby_pace/run_type.rb
84
85
  - lib/runby_pace/run_types/all_run_types.g.rb
@@ -105,7 +106,7 @@ homepage: https://github.com/tygerbytes/runby-pace
105
106
  licenses:
106
107
  - MIT
107
108
  metadata:
108
- commit-hash: 65fe0e2369734efe51f94117b434af9cc57849d5
109
+ commit-hash: d8811ae095d9919ea5393bf0e6c93de27ae324f3
109
110
  post_install_message:
110
111
  rdoc_options: []
111
112
  require_paths: