Skip to content

Commit

Permalink
Add rudimentary support for calculating distance traveled at a given …
Browse files Browse the repository at this point in the history
…pace/duration
  • Loading branch information
tygerbytes committed Aug 19, 2016
1 parent e096915 commit 2f95dbd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/runby_pace/run_math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ def self.convert_speed_to_pace(units_per_hour)
raise 'units_per_hour must be numeric' unless units_per_hour.is_a? Numeric
RunbyPace::PaceTime.from_minutes(60.0 / units_per_hour)
end

def self.distance_traveled(pace, time)
# TODO: I sense the need for a refactor...
# Consider extracting a new Pace class, which consists of Time + Units
# Then move this method to the new class, and give it a better name.
pace = RunbyPace::PaceTime.new(pace)
time = RunbyPace::PaceTime.new(time)

divisor = pace.total_minutes / time.total_minutes
distance_units_traveled = 1 / divisor
return distance_units_traveled
end
end
end
9 changes: 9 additions & 0 deletions spec/runby_pace/run_math_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@
expect(pace).to eq '08:34'
end
end

describe 'distance calculations' do
it 'calculates distance traveled, given a pace and a duration in seconds' do
pace = '06:00'
duration = '60:00'
meters_traveled = RunbyPace::RunMath.distance_traveled(pace, duration)
expect(meters_traveled).to be_within(0.01).of(10)
end
end
end

0 comments on commit 2f95dbd

Please sign in to comment.