From a70ca8d1b6c2e8a41489937368d2db8cc45d5004 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Saraiva Date: Thu, 9 Jul 2026 20:43:38 -0300 Subject: [PATCH 1/2] refactor: extract dig_key helper in TrackCalculator Collapse the duplicated symbol/string key-access pattern (point[key] || point[key.to_s]) into a single private dig_key helper. Coordinate, elevation, and time readers now share it, and the redundant fetch_coord wrapper is inlined. No behavior change. Bump to 1.9.10. --- CHANGELOG.md | 5 +++++ lib/calcpace/track_calculator.rb | 15 +++++++-------- lib/calcpace/version.rb | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ce1b7b..0e74300 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.9.10] - 2026-07-09 + +### Changed +- Refactor `TrackCalculator`: extract a single `dig_key` helper for symbol/string key access, removing duplicated fallback logic across coordinate, elevation, and time readers (no behavior change) + ## [1.9.9] - 2026-06-17 ### Changed diff --git a/lib/calcpace/track_calculator.rb b/lib/calcpace/track_calculator.rb index 6417db2..6883272 100644 --- a/lib/calcpace/track_calculator.rb +++ b/lib/calcpace/track_calculator.rb @@ -74,8 +74,8 @@ def track_distance(points) return 0.0 if points.nil? || points.size < 2 total = points.each_cons(2).sum do |a, b| - haversine_distance(fetch_coord(a, :lat), fetch_coord(a, :lon), - fetch_coord(b, :lat), fetch_coord(b, :lon)) + haversine_distance(dig_key(a, :lat), dig_key(a, :lon), + dig_key(b, :lat), dig_key(b, :lon)) end total.round(2) @@ -180,13 +180,12 @@ def accumulate_elevation(gain, loss, ele_a, ele_b) end end - def fetch_coord(point, key) + def dig_key(point, key) point[key] || point[key.to_s] end def fetch_ele(point) - val = point[:ele] || point['ele'] - val&.to_f + dig_key(point, :ele)&.to_f end def validate_points_have_time(points) @@ -198,7 +197,7 @@ def validate_points_have_time(points) end def point_time(point) - t = point[:time] || point['time'] + t = dig_key(point, :time) t.respond_to?(:to_f) ? t.to_f : t end @@ -228,8 +227,8 @@ def collect_splits(points, split_km) end def process_segment(point_a, point_b, split_km, state) - segment_km = haversine_distance(fetch_coord(point_a, :lat), fetch_coord(point_a, :lon), - fetch_coord(point_b, :lat), fetch_coord(point_b, :lon)) + segment_km = haversine_distance(dig_key(point_a, :lat), dig_key(point_a, :lon), + dig_key(point_b, :lat), dig_key(point_b, :lon)) state[:accumulated_km] += segment_km while state[:accumulated_km] >= split_km * state[:split_number] diff --git a/lib/calcpace/version.rb b/lib/calcpace/version.rb index 8492a63..6756319 100644 --- a/lib/calcpace/version.rb +++ b/lib/calcpace/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Calcpace - VERSION = '1.9.9' + VERSION = '1.9.10' end From 82d907de556624a1f6d5fceca451682a08f15de9 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Saraiva Date: Thu, 9 Jul 2026 20:51:44 -0300 Subject: [PATCH 2/2] refactor: use dig_key in validate_points_have_time Address review: the last remaining symbol/string fallback (pt[:time] || pt['time']) now delegates to dig_key, keeping key access centralized. --- lib/calcpace/track_calculator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/calcpace/track_calculator.rb b/lib/calcpace/track_calculator.rb index 6883272..ee9f694 100644 --- a/lib/calcpace/track_calculator.rb +++ b/lib/calcpace/track_calculator.rb @@ -190,7 +190,7 @@ def fetch_ele(point) def validate_points_have_time(points) points.each_with_index do |pt, i| - next if pt[:time] || pt['time'] + next if dig_key(pt, :time) raise ArgumentError, "Point at index #{i} is missing :time key required for splits" end