From a111bb33e3e99a4c9c701736dc67594eef2d1d11 Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 11:02:36 -0500 Subject: [PATCH 01/12] create initial file system --- lib/.gitkeep => spec/bike_club_spec.rb | 0 spec/{.gitkeep => biker_spec.rb} | 0 spec/ride_spec.rb | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename lib/.gitkeep => spec/bike_club_spec.rb (100%) rename spec/{.gitkeep => biker_spec.rb} (100%) create mode 100644 spec/ride_spec.rb diff --git a/lib/.gitkeep b/spec/bike_club_spec.rb similarity index 100% rename from lib/.gitkeep rename to spec/bike_club_spec.rb diff --git a/spec/.gitkeep b/spec/biker_spec.rb similarity index 100% rename from spec/.gitkeep rename to spec/biker_spec.rb diff --git a/spec/ride_spec.rb b/spec/ride_spec.rb new file mode 100644 index 0000000..e69de29 From b50b0bee194c90670b87b97dd3aa95cf8dd6b767 Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 11:05:55 -0500 Subject: [PATCH 02/12] create spec helper --- spec/spec_helper.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spec/spec_helper.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..addb0ec --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,6 @@ +# require 'simplecov';SimpleCov.start +require 'pry' +require 'rspec' +require './lib/ride' +require './lib/biker' +require './lib/bike_club' \ No newline at end of file From 1b99d268fa95a87ac38967a5376c5df6ae13a98d Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 11:18:43 -0500 Subject: [PATCH 03/12] write Ride class tests --- spec/ride_spec.rb | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/spec/ride_spec.rb b/spec/ride_spec.rb index e69de29..0b59496 100644 --- a/spec/ride_spec.rb +++ b/spec/ride_spec.rb @@ -0,0 +1,54 @@ +require_relative 'spec_helper' + +RSpec.describe Ride do + describe '#initialize' do + it 'exists' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + expect(ride1).to be_an_instance_of(Ride) + end + + it 'has attributes' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + + expect(ride1.name).to eq("Walnut Creek Trail") + expect(ride1.distance).to eq(10.7) + expect(ride1.terrain).to eq(:hills) + end + + it 'can have different attributes' do + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + expect(ride2.name).to eq("Town Lake") + expect(ride2.distance).to eq(14.9) + expect(ride2.terrain).to eq(:gravel) + end + end + + describe '#loop?' do + it 'returns the value of the Rides loop attribute' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + expect(ride1.loop?).to be false + expect(ride2.loop?).to be true + end + end + + describe '#total_distance' do + it 'returns the base distance attribute if loop is true' do + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + expect(ride2.distance).to eq(14.9) + expect(ride2.loop?).to be true + expect(ride2.total_distance).to eq(14.9) + end + + it 'returns distance times two if loop is false' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + + expect(ride1.distance).to eq(10.7) + expect(ride1.loop?).to be false + expect(ride1.total_distance).to eq(21.4) + end + end +end \ No newline at end of file From a659e761d89c8e1a0b705f6839c55f32c187d6f7 Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 11:21:35 -0500 Subject: [PATCH 04/12] write Ride class --- lib/ride.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/ride.rb b/lib/ride.rb index 1be20f0..4c689dc 100644 --- a/lib/ride.rb +++ b/lib/ride.rb @@ -1,3 +1,22 @@ class Ride + attr_reader :name, :distance, :terrain + def initialize(ride_info) + @name = ride_info[:name] + @distance = ride_info[:distance] + @loop = ride_info[:loop] + @terrain = ride_info[:terrain] + end + + def loop? + @loop + end + + def total_distance + if @loop + @distance + else + @distance * 2.0 + end + end end \ No newline at end of file From 31d73e81deeec2615d77d86121e232619d3d54c0 Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 12:02:41 -0500 Subject: [PATCH 05/12] write tests for Biker class --- spec/biker_spec.rb | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/spec/biker_spec.rb b/spec/biker_spec.rb index e69de29..d78631e 100644 --- a/spec/biker_spec.rb +++ b/spec/biker_spec.rb @@ -0,0 +1,106 @@ +require_relative 'spec_helper' + +RSpec.describe Biker do + describe '#initialize' do + it 'exists' do + biker = Biker.new("Kenny", 30) + expect(biker).to be_an_instance_of(Biker) + end + + it 'has attributes' do + biker = Biker.new("Kenny", 30) + + expect(biker.name).to eq("Kenny") + expect(biker.max_distance).to eq(30) + expect(biker.rides).to eq({}) + expect(biker.acceptable_terrain).to eq([]) + end + + it 'can have some different attributes' do + biker2 = Biker.new("Athena", 15) + + expect(biker2.name).to eq("Athena") + expect(biker2.max_distance).to eq(15) + expect(biker2.rides).to eq({}) + expect(biker2.acceptable_terrain).to eq([]) + end + end + + describe '#learn_terrain!' do + it 'adds a new terrain to the bikers acceptable terrains' do + biker = Biker.new("Kenny", 30) + + expect(biker.acceptable_terrain).to eq([]) + biker.learn_terrain!(:gravel) + expect(biker.acceptable_terrain).to eq([:gravel]) + biker.learn_terrain!(:hills) + expect(biker.acceptable_terrain).to eq([:gravel, :hills]) + end + end + + describe '#log_ride' do + context 'each unique ride is a key, with all the times for each ride being the values' do + it 'saves a bikers ride and time in the rides attribute' do + biker = Biker.new("Kenny", 30) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker.log_ride(ride1, 92.5) + biker.log_ride(ride1, 91.1) + biker.log_ride(ride2, 60.9) + biker.log_ride(ride2, 61.6) + + expect(biker.rides.keys).to contain_exactly(ride1, ride2) + expect(biker.rides[ride1]).to contain_exactly(92.5, 91.1) + expect(biker.rides[ride2]).to contain_exactly(60.9, 61.6) + end + + it 'will not log if rides terrain is not accepted by biker' do + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker2.log_ride(ride1, 97.0) + biker2.log_ride(ride2, 67.0) + + expect(biker2.rides).to eq({}) + end + + it 'will not log if rides total distance is higher than bikers max_distance' do + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker2.learn_terrain!(:gravel) + biker2.learn_terrain!(:hills) + + biker2.log_ride(ride1, 95.0) + biker2.log_ride(ride2, 65.0) + + expect(biker2.rides).to eq({ride2: [65.0]}) + end + end + end + + describe '#personal_record' do + context 'returns false if biker has no saved time for a ride' do + it 'returns a bikers best ride time for a specific ride' do + biker = Biker.new("Kenny", 30) + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker.log_ride(ride1, 92.5) + biker.log_ride(ride1, 91.1) + biker.log_ride(ride2, 60.9) + biker.log_ride(ride2, 61.6) + biker2.log_ride(ride2, 65.0) + + expect(biker.personal_record(ride1)).to eq(91.1) + expect(biker.personal_record(ride2)).to eq(60.9) + expect(biker2.personal_record(ride1)).to be false + expect(biker.personal_record(ride2)).to eq(65.0) + end + end + end +end \ No newline at end of file From f821d32528673dd174eb89c0685773868b12187f Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 12:24:10 -0500 Subject: [PATCH 06/12] fix test errors --- spec/biker_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/biker_spec.rb b/spec/biker_spec.rb index d78631e..26aed8f 100644 --- a/spec/biker_spec.rb +++ b/spec/biker_spec.rb @@ -45,6 +45,9 @@ ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + biker.learn_terrain!(:gravel) + biker.learn_terrain!(:hills) + biker.log_ride(ride1, 92.5) biker.log_ride(ride1, 91.1) biker.log_ride(ride2, 60.9) @@ -77,7 +80,7 @@ biker2.log_ride(ride1, 95.0) biker2.log_ride(ride2, 65.0) - expect(biker2.rides).to eq({ride2: [65.0]}) + expect(biker2.rides).to eq({ride2 => [65.0]}) end end end From 0b7157f542e0096a5c0e79cb90c146b8a9f2629c Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 12:32:03 -0500 Subject: [PATCH 07/12] fix test error --- spec/biker_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/biker_spec.rb b/spec/biker_spec.rb index 26aed8f..240e451 100644 --- a/spec/biker_spec.rb +++ b/spec/biker_spec.rb @@ -92,6 +92,11 @@ biker2 = Biker.new("Athena", 15) ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker.learn_terrain!(:gravel) + biker.learn_terrain!(:hills) + biker2.learn_terrain!(:gravel) + biker2.learn_terrain!(:hills) biker.log_ride(ride1, 92.5) biker.log_ride(ride1, 91.1) @@ -102,7 +107,7 @@ expect(biker.personal_record(ride1)).to eq(91.1) expect(biker.personal_record(ride2)).to eq(60.9) expect(biker2.personal_record(ride1)).to be false - expect(biker.personal_record(ride2)).to eq(65.0) + expect(biker2.personal_record(ride2)).to eq(65.0) end end end From c058be424601d990b309bb88ff9aa70b58e145fc Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 12:32:12 -0500 Subject: [PATCH 08/12] write Biker class --- lib/biker.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/biker.rb b/lib/biker.rb index db234de..90565e9 100644 --- a/lib/biker.rb +++ b/lib/biker.rb @@ -1,3 +1,40 @@ +require './spec/spec_helper' + class Biker + attr_reader :name, :max_distance, :acceptable_terrain, :rides + + def initialize(name, max_distance) + @name = name + @max_distance = max_distance + @acceptable_terrain = [] + @rides = Hash.new { |hash, key| hash[key] = [] } + end + + def learn_terrain!(terrain) + @acceptable_terrain << terrain + end + + def log_ride(ride, time) + if @max_distance >= ride.total_distance && @acceptable_terrain.include?(ride.terrain) + @rides[ride] << time + end + end + + def personal_record(ride) + best_time = false + + if @rides[ride].empty? + return best_time + else + @rides[ride].each do |time| + if best_time == false + best_time = time + else + best_time = time if time < best_time + end + end + end + best_time + end end \ No newline at end of file From 06cdc7997b7c312acdf52b5da7919d62bf91737d Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 12:52:18 -0500 Subject: [PATCH 09/12] write tests for BikeClub class --- spec/bike_club_spec.rb | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/spec/bike_club_spec.rb b/spec/bike_club_spec.rb index e69de29..56a1541 100644 --- a/spec/bike_club_spec.rb +++ b/spec/bike_club_spec.rb @@ -0,0 +1,61 @@ +require_relative 'spec_helper' + +RSpec.describe BikeClub do + describe '#initialize' do + it 'exists' do + bike_club = BikeClub.new("Hell's Angels") + expect(bike_club).to be_an_instance_of(BikeClub) + end + + it 'has attributes' do + bike_club = BikeClub.new("Hell's Angels") + expect(bike_club.name).to eq("Hell's Angels") + end + + it 'can have different attributes' do + bike_club2 = BikeClub.new("Rolling Thunder") + expect(bike_club.name).to eq("Rolling Thunder") + end + + it 'has a default attribute' do + bike_club = BikeClub.new("Hell's Angels") + bike_club2 = BikeClub.new("Rolling Thunder") + + expect(bike_club.bikers).to eq([]) + expect(bike_club2.bikers).to eq([]) + end + end + + describe '#add_biker' do + it 'can add a biker to its bikers list' do + bike_club = BikeClub.new("Hell's Angels") + biker = Biker.new("Kenny", 30) + biker2 = Biker.new("Athena", 15) + + expect(bike_club.bikers).to eq([]) + bike_club.add_biker(biker) + bike_club.add_biker(biker2) + expect(bike_club.bikers).to eq([biker, biker2]) + end + end + + describe '#most_rides' do + it 'returns the biker with the most rides' do + bike_club = BikeClub.new("Hell's Angels") + biker = Biker.new("Kenny", 30) + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker.learn_terrain!(:hills) + biker.learn_terrain!(:gravel) + biker2.learn_terrain!(:gravel) + + biker.log_ride(ride1, 91.2) + biker.log_ride(ride2, 60.6) + biker2.log_ride(ride2, 59.7) + + expect(bike_club.most_rides).to eq(biker) + end + end +end \ No newline at end of file From c01c0334de1255fb6cd32826872c5960a3aed5c0 Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 12:54:25 -0500 Subject: [PATCH 10/12] fix syntax error --- spec/bike_club_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/bike_club_spec.rb b/spec/bike_club_spec.rb index 56a1541..0fb498a 100644 --- a/spec/bike_club_spec.rb +++ b/spec/bike_club_spec.rb @@ -14,7 +14,7 @@ it 'can have different attributes' do bike_club2 = BikeClub.new("Rolling Thunder") - expect(bike_club.name).to eq("Rolling Thunder") + expect(bike_club2.name).to eq("Rolling Thunder") end it 'has a default attribute' do From 5f8c3bc744deecd840ee601b7b3f3a68ef9791e5 Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 13:08:25 -0500 Subject: [PATCH 11/12] fix error --- spec/bike_club_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/bike_club_spec.rb b/spec/bike_club_spec.rb index 0fb498a..ff1fd14 100644 --- a/spec/bike_club_spec.rb +++ b/spec/bike_club_spec.rb @@ -47,6 +47,9 @@ ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + bike_club.add_biker(biker) + bike_club.add_biker(biker2) + biker.learn_terrain!(:hills) biker.learn_terrain!(:gravel) biker2.learn_terrain!(:gravel) From 603a666ae1337ea22b4821698ed53d1e9ab5a21a Mon Sep 17 00:00:00 2001 From: Dustin Peukert Date: Mon, 4 Nov 2024 13:08:43 -0500 Subject: [PATCH 12/12] write BikeClub class --- lib/bike_club.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/bike_club.rb b/lib/bike_club.rb index b83c6db..dec8db8 100644 --- a/lib/bike_club.rb +++ b/lib/bike_club.rb @@ -1,3 +1,25 @@ class BikeClub + attr_reader :name, :bikers + def initialize(name) + @name = name + @bikers = [] + end + + def add_biker(biker) + @bikers << biker + end + + def most_rides + most = @bikers[0] + + @bikers.each do |biker| + current_most_rides = most.rides.values.flatten.count + num_of_rides = biker.rides.values.flatten.count + + most = biker if num_of_rides > current_most_rides + end + + most + end end \ No newline at end of file