From 5c3b2f475a28d77511aad562238e4873167e0a5f Mon Sep 17 00:00:00 2001 From: jason perez Date: Mon, 10 Feb 2014 13:58:28 -0800 Subject: [PATCH 01/11] update stubs to doubles --- spec/map_spec.rb | 8 ++++---- spec/place_spec.rb | 2 +- spec/sales_person_spec.rb | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/spec/map_spec.rb b/spec/map_spec.rb index 2e2f6f1..9688512 100644 --- a/spec/map_spec.rb +++ b/spec/map_spec.rb @@ -10,8 +10,8 @@ end it "should use the first item in the array" do - austin = stub("Austin") - dallas = stub("Dallas") + austin = double("Austin") + dallas = double("Dallas") Geocoder.stub(:search) {[austin, dallas]} Map.search("austin, tx").should eq(austin) end @@ -19,8 +19,8 @@ describe ":distance" do it "should calculate distance between two sets of coordinates" do - alpha = stub - beta = stub + alpha = double + beta = double Geocoder::Calculations.should_receive(:distance_between).with(alpha, beta) Map.distance_between(alpha, beta) end diff --git a/spec/place_spec.rb b/spec/place_spec.rb index 7d48250..396e0e8 100644 --- a/spec/place_spec.rb +++ b/spec/place_spec.rb @@ -14,7 +14,7 @@ describe ":build" do let(:name) { "El Paso, TX"} - let(:result) { stub("el paso", coordinates: [29, -95])} + let(:result) { double("el paso", coordinates: [29, -95])} it "should build from the map" do Map.should_receive(:search).with(name).and_return(result) diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 08a6ce9..84ad21b 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -3,13 +3,13 @@ describe SalesPerson do it "should have many cities" do - city = stub + city = double subject.schedule_city(city) subject.cities.should include(city) - end + end it "should keep the cities only scheduled once" do - city = stub + city = double expect{ subject.schedule_city(city) subject.schedule_city(city) @@ -17,13 +17,13 @@ end it "should calculate a route via the CalculatesRoute" do - cities = [stub, stub, stub] + cities = [double, double, double] subject.stub(:cities) { cities } CalculatesRoute.should_receive(:calculate).with(cities) subject.route end it "should returns the route from CalculatesRoute" do - route_stub = [stub, stub] + route_stub = [double, double] CalculatesRoute.stub(:calculate) { route_stub } subject.route.should eq(route_stub) end From 6f8b1336a5b8af0c9a247872fca529081ebcf580 Mon Sep 17 00:00:00 2001 From: jason perez Date: Mon, 10 Feb 2014 23:15:15 -0800 Subject: [PATCH 02/11] add a starting point --- lib/calculates_route.rb | 2 +- lib/sales_person.rb | 8 ++++++-- salesperson.rb | 8 ++++---- spec/sales_person_spec.rb | 16 +++++++++++----- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 4488393..436ca29 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -5,7 +5,7 @@ def self.calculate(points) remaining_points = points route = [] route << remaining_points.slice!(0) - until remaining_points == [] do + until remaining_points == [] do next_point = shortest_distance(route.last, remaining_points) route << remaining_points.slice!(remaining_points.index(next_point)) end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index d0c2890..6c0f0c1 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -1,11 +1,15 @@ class SalesPerson - attr_reader :cities + attr_reader :cities, :starting_city def initialize @cities = [] end - def schedule_city(city) + def schedule_city(city, options={}) + if options[:starting_city] == true + @starting_city ||= city + @cities.unshift(@starting_city) + end @cities << city unless @cities.include?(city) end diff --git a/salesperson.rb b/salesperson.rb index 9cafbd7..72e1ff4 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -2,9 +2,9 @@ phil = SalesPerson.new -phil.schedule_city(Place.build("Dallas, TX")) -phil.schedule_city(Place.build("El Paso, TX")) -phil.schedule_city(Place.build("Austin, TX")) -phil.schedule_city(Place.build("Lubbock, TX")) +phil.schedule_city(Place.build("Whittier, CA")) +phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true) +phil.schedule_city(Place.build("Brea, CA")) +phil.schedule_city(Place.build("Irvine, CA")) puts phil.route diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 84ad21b..ee47b85 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -6,25 +6,31 @@ city = double subject.schedule_city(city) subject.cities.should include(city) - end + end - it "should keep the cities only scheduled once" do + it "should keep the cities only scheduled once" do city = double expect{ subject.schedule_city(city) subject.schedule_city(city) - }.to change(subject.cities,:count).by(1) + }.to change(subject.cities,:count).by(1) end it "should calculate a route via the CalculatesRoute" do cities = [double, double, double] - subject.stub(:cities) { cities } + subject.stub(:cities) { cities } CalculatesRoute.should_receive(:calculate).with(cities) subject.route end - it "should returns the route from CalculatesRoute" do + it "should return the route from CalculatesRoute" do route_stub = [double, double] CalculatesRoute.stub(:calculate) { route_stub } subject.route.should eq(route_stub) end + + it "should allow for the starting point to be specified" do + city = double + subject.schedule_city(city, starting_city: true) + subject.starting_city.should eq(city) + end end From fb29c0c20c8464119a67f279e66ac859b73e921a Mon Sep 17 00:00:00 2001 From: jason perez Date: Tue, 11 Feb 2014 14:33:11 -0800 Subject: [PATCH 03/11] add benchmarking --- benchmark_helper.rb | 220 +++++++++++++++++++++++++++++++++++++++ benchmark_salesperson.rb | 36 +++++++ 2 files changed, 256 insertions(+) create mode 100644 benchmark_helper.rb create mode 100644 benchmark_salesperson.rb diff --git a/benchmark_helper.rb b/benchmark_helper.rb new file mode 100644 index 0000000..05f9205 --- /dev/null +++ b/benchmark_helper.rb @@ -0,0 +1,220 @@ +CITIES = ["Acton, CA", + "Adelanto, CA", + "Agoura Hills, CA", + "Agua Dulce, CA", + "Alameda, CA", + "Alamo, CA", + "Albany, CA", + "Alhambra, CA", + "Aliso Viejo, CA", + "Alpine, CA", + "Alta Loma, CA", + "Altadena, CA", + "Alturas, CA", + "Amador City, CA", + "Anaheim, CA", + "Anaheim Hills, CA", + "Anderson, CA", + "Angels Camp, CA", + "Angelus Oaks, CA", + "Antelope, CA", + "Antioch, CA", + "Anza, CA", + "Apple Valley, CA", + "Applegate, CA", + "Aptos, CA", + "Arcadia, CA", + "Arcata, CA", + "Arleta, CA", + "Armona, CA", + "Arnold, CA", + "Aroura Hills, CA", + "Arroyo Grande, CA", + "Atascadero, CA", + "Atwater, CA", + "Atwood, CA", + "Auberry, CA", + "Auburn, CA", + "Avalon, CA", + "Avery, CA", + "Avila Beach, CA", + "Azusa, CA", + "Bakersfield, CA", + "Baldwin Park, CA", + "Bangor, CA", + "Banning, CA", + "Barstow, CA", + "Bay Point, CA", + "Bayside, CA", + "Bel Air, CA", + "Bell, CA", + "Bellflower, CA", + "Belmont, CA", + "Belmont Shores, CA", + "Belvedere, CA", + "Ben Lomond, CA", + "Benicia, CA", + "Berkeley, CA", + "Berry Creek, CA", + "Beverly Hills, CA", + "Big Bear City, CA", + "Big Bear Lake, CA", + "Big Sur, CA", + "Bishop, CA", + "Blue Jay, CA", + "Blue Lake, CA", + "Blythe, CA", + "Bolinas, CA", + "Bonita, CA", + "Borrego Springs, CA", + "Bothell, CA", + "Boulder Creek, CA", + "Brea, CA", + "Breenbrae, CA", + "Brentwood, CA", + "Brisbane, CA", + "Buellton, CA", + "Buena Park, CA", + "Burbank, CA", + "Burlingame, CA", + "Burlington, CA", + "Calabasas, CA", + "California City, CA", + "Calimesa, CA", + "Calistoga, CA", + "Camarillo, CA", + "Cambria, CA", + "Cameron Park, CA", + "Campbell, CA", + "Campo, CA", + "Canoga Park, CA", + "Canyon Country, CA", + "Canyon Lake, CA", + "Capistrano Beach, CA", + "Capitola, CA", + "Cardiff, CA", + "Cardiff-by-the-Sea, CA", + "Carlsbad, CA", + "Carmel, CA", + "Carmel Valley, CA", + "Carmichael, CA", + "Carpinteria, CA", + "Carson, CA", + "Castaic, CA", + "Castro Valley, CA", + "Catalina Island, CA", + "Cathedral City, CA", + "Cayucos, CA", + "Cedar Glen, CA", + "Cedar Ridge, CA", + "Ceres, CA", + "Cerritos, CA", + "Chatsworth, CA", + "Cherry Valley, CA", + "Chester, CA", + "Chico, CA", + "China Lake, CA", + "Chino, CA", + "Chino Hills, CA", + "Chula Vista, CA", + "Citrus Heights, CA", + "City of Industry, CA", + "Claremont, CA", + "Clayton, CA", + "Clearlake Oaks, CA", + "Clearlake, CA", + "Cloverdale, CA", + "Clovis, CA", + "Coarsegold, CA", + "Colfax, CA", + "Coloma, CA", + "Colton, CA", + "Columbia, CA", + "Colusa, CA", + "Commerce, CA", + "Compton, CA", + "Concord, CA", + "Cordelia, CA", + "Corning, CA", + "Corona, CA", + "Corona del Mar, CA", + "Coronado, CA", + "Corralitos, CA", + "Corte Madera, CA", + "Costa Mesa, CA", + "Cotati, CA", + "Coto de Caza, CA", + "Covina, CA", + "CresceCutten, CA", + "Cypress, CA", + "Daly City, CA", + "Dana Point, CA", + "Danville, CA", + "Davis, CA", + "Del Mar, CA", + "Desert Hot Springs, CA", + "Diamond Bar, CA", + "Diamond Springs, CA", + "Dobbins, CA", + "Dominguez Hills, CA", + "Dove Canyon, CA", + "Downey, CA", + "Duarte, CA", + "Dublin, CA", + "Hemet, CA", + "Hercules, CA", + "Hermosa Beach, CA", + "Hesperia, CA", + "Highland, CA", + "Hinkley, CA", + "Hollister, CA", + "Hollywood, CA", + "Homeland, CA", + "Honcut, CA", + "Humboldt, CA", + "Huntington Beach, CA", + "Huntington Park, CA", + "Idyllwild, CA", + "Imperial, CA", + "Imperial Beach, CA", + "Independence, CA", + "Indian Wells, CA", + "Indio, CA", + "Inglewood, CA", + "Inverness, CA", + "Ione, CA", + "Irvine, CA", + "Irwindale, CA", + "Isla Vista, CA", + "Isleton, CA", + "Jackson, CA", + "Jacumba, CA", + "Jamestown, CA", + "Jamul, CA", + "Jenner, CA", + "Joshua Tree, CA", + "Julian, CA", + "Kelseyville, CA", + "Kensington, CA", + "Kentfield, CA", + "Kenwood, CA", + "King City, CA", + "Klamath, CA", + "Klamath River, CA", + "La Canada, CA", + "La Canada Flintridge, CA", + "La Crescenta, CA", + "La Habra, CA", + "La Honda, CA", + "Lake Elsinore, CA", + "Lake Forest, CA", + "Lake Isabella, CA", + "Lakeport, CA", + "Lakeside, CA", + "Vineburg, CA", + "Visalia, CA", + "Vista, CA", + "Walnut, CA", + "Walnut Creek, CA", + "Warm Springs, CA", + "Watsoncca Valley, CA"] \ No newline at end of file diff --git a/benchmark_salesperson.rb b/benchmark_salesperson.rb new file mode 100644 index 0000000..d3949b3 --- /dev/null +++ b/benchmark_salesperson.rb @@ -0,0 +1,36 @@ +Dir["./lib/*.rb"].each {|file| require file } + +require 'benchmark' +require_relative "benchmark_helper" + +phil = SalesPerson.new + +Benchmark.bm do |x| + x.report("2 cities") do + phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true) + 1.upto(2) do |city| + phil.schedule_city(Place.build(CITIES[city])) + end + end + + x.report("10 cities") do + phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true) + 1.upto(10) do |city| + phil.schedule_city(Place.build(CITIES[city])) + end + end + + x.report("50 cities") do + phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true) + 1.upto(50) do |city| + phil.schedule_city(Place.build(CITIES[city])) + end + end + + x.report("200 cities") do + phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true) + 1.upto(200) do |city| + phil.schedule_city(Place.build(CITIES[city])) + end + end +end From a5db5fc37b4b8dac796e97b4d334a3636292fb66 Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 12 Feb 2014 09:43:19 -0800 Subject: [PATCH 04/11] add initial version of miles traveled --- lib/sales_person.rb | 6 +++++- salesperson.rb | 1 + spec/sales_person_spec.rb | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 6c0f0c1..6659b06 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -14,6 +14,10 @@ def schedule_city(city, options={}) end def route - CalculatesRoute.calculate(cities) + @routed_cities = CalculatesRoute.calculate(cities) + end + + def miles_traveled + @routed_cities end end diff --git a/salesperson.rb b/salesperson.rb index 72e1ff4..43c5133 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -8,3 +8,4 @@ phil.schedule_city(Place.build("Irvine, CA")) puts phil.route +puts phil.miles_traveled \ No newline at end of file diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index ee47b85..864514d 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -33,4 +33,11 @@ subject.schedule_city(city, starting_city: true) subject.starting_city.should eq(city) end + + xit "should calculate total miles traveled" do + + subject.miles_traveled.should eq(400) + end + + it "should return total time traveled at 60 mph" end From ba1cebcbb684361c78939fae64dcacb61bf5dced Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 12 Feb 2014 14:05:43 -0800 Subject: [PATCH 05/11] update salesperson and route to calculate total miles traveled --- lib/calculates_route.rb | 23 +++++++++++++++++++---- lib/sales_person.rb | 5 +++-- spec/calculates_route_spec.rb | 18 +++++++++++++++--- spec/sales_person_spec.rb | 16 ++++++++-------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 436ca29..9acea91 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -1,7 +1,11 @@ -class CalculatesRoute +class Route + attr_reader :miles_traveled - def self.calculate(points) + def initialize + @miles_traveled = 0 + end + def calculate(points) remaining_points = points route = [] route << remaining_points.slice!(0) @@ -12,11 +16,22 @@ def self.calculate(points) route end - def self.shortest_distance(from, possible) + def shortest_distance(from, possible) distances = possible.map do |point| {point: point, distance: Map.distance_between(from, point)} end - distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)}.first.fetch(:point) + get_the_shortest_distance_from(distances) + end + + def get_the_shortest_distance_from(distances) + shortest_distance = distances.sort{|a,b| a.fetch(:distance) <=> b.fetch(:distance)} + add_to_miles_traveled(shortest_distance.first.fetch(:distance)) + shortest_distance.first.fetch(:point) + end + + def add_to_miles_traveled(miles) + @miles_traveled += miles end end + diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 6659b06..961a294 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -3,6 +3,7 @@ class SalesPerson attr_reader :cities, :starting_city def initialize @cities = [] + @route = Route.new end def schedule_city(city, options={}) @@ -14,10 +15,10 @@ def schedule_city(city, options={}) end def route - @routed_cities = CalculatesRoute.calculate(cities) + @route.calculate(cities) end def miles_traveled - @routed_cities + @route.miles_traveled end end diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index 47679d6..792f4d0 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -1,15 +1,27 @@ require_relative "../lib/calculates_route" require_relative "../lib/place" -describe CalculatesRoute do +describe Route do let(:dallas) {Place.build("Dallas, TX") } let(:austin ) { Place.build("Austin, TX")} let(:lubbock ) { Place.build("Lubbock, TX")} let(:el_paso ) { Place.build("El Paso, TX")} + let (:points) {[dallas, el_paso, austin, lubbock]} + let(:route) { Route.new } it "should calculate the route" do - points = [dallas, el_paso, austin, lubbock] expected = [dallas, austin, lubbock, el_paso] - CalculatesRoute.calculate(points).should eq(expected) + route.calculate(points).should eq(expected) + end + + it "should provide the shortest distance between a set of routes" do + remaining_points = [lubbock, dallas, el_paso] + route.shortest_distance(austin, remaining_points). should eq(dallas) + end + + it "should calculate total miles traveled" do + route.calculate(points) + route.miles_traveled.should be_within(1).of(812) end end + diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 864514d..50b19ec 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -16,15 +16,15 @@ }.to change(subject.cities,:count).by(1) end - it "should calculate a route via the CalculatesRoute" do + it "should calculate a route via the Route" do cities = [double, double, double] subject.stub(:cities) { cities } - CalculatesRoute.should_receive(:calculate).with(cities) + Route.any_instance.should_receive(:calculate).with(cities) subject.route end - it "should return the route from CalculatesRoute" do + it "should return the route from Route" do route_stub = [double, double] - CalculatesRoute.stub(:calculate) { route_stub } + Route.any_instance. stub(:calculate) { route_stub } subject.route.should eq(route_stub) end @@ -34,10 +34,10 @@ subject.starting_city.should eq(city) end - xit "should calculate total miles traveled" do - - subject.miles_traveled.should eq(400) + it "should calculate total miles traveled via Route" do + Route.any_instance.should_receive(:miles_traveled) + subject.miles_traveled end - it "should return total time traveled at 60 mph" + it "should return total time traveled at 60 mph via Route" end From 3050a742acfb1dbeb2ecf3bc1bfa2f4658742d88 Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 12 Feb 2014 22:34:20 -0800 Subject: [PATCH 06/11] add ability to calculate travel time --- lib/calculates_route.rb | 4 ++++ lib/sales_person.rb | 4 ++++ salesperson.rb | 3 ++- spec/calculates_route_spec.rb | 5 +++++ spec/sales_person_spec.rb | 5 ++++- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/calculates_route.rb b/lib/calculates_route.rb index 9acea91..c5a591d 100644 --- a/lib/calculates_route.rb +++ b/lib/calculates_route.rb @@ -32,6 +32,10 @@ def get_the_shortest_distance_from(distances) def add_to_miles_traveled(miles) @miles_traveled += miles end + + def time_traveled + @miles_traveled / 60 + end end diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 961a294..32b91ea 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -21,4 +21,8 @@ def route def miles_traveled @route.miles_traveled end + + def time_traveled + @route.time_traveled + end end diff --git a/salesperson.rb b/salesperson.rb index 43c5133..3f00e45 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -8,4 +8,5 @@ phil.schedule_city(Place.build("Irvine, CA")) puts phil.route -puts phil.miles_traveled \ No newline at end of file +puts phil.miles_traveled +puts phil.time_traveled \ No newline at end of file diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index 792f4d0..3b02710 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -23,5 +23,10 @@ route.calculate(points) route.miles_traveled.should be_within(1).of(812) end + + it "should calculate total travel time" do + route.calculate(points) + route.time_traveled.should be_within(1).of(13) + end end diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 50b19ec..65e9dea 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -39,5 +39,8 @@ subject.miles_traveled end - it "should return total time traveled at 60 mph via Route" + it "should return total time traveled at 60 mph via Route" do + Route.any_instance.should_receive(:time_traveled) + subject.time_traveled + end end From 1d5dbc05235ee959e54382dc1dc53a0c08b3b238 Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 12 Feb 2014 22:46:46 -0800 Subject: [PATCH 07/11] update salesperson --- salesperson.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/salesperson.rb b/salesperson.rb index 3f00e45..ce84ae8 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -1,12 +1,15 @@ Dir["./lib/*.rb"].each {|file| require file } -phil = SalesPerson.new -phil.schedule_city(Place.build("Whittier, CA")) -phil.schedule_city(Place.build("Chino Hills, CA"), starting_city: true) -phil.schedule_city(Place.build("Brea, CA")) -phil.schedule_city(Place.build("Irvine, CA")) +jason = SalesPerson.new +jason.schedule_city(Place.build("Whittier, CA")) +jason.schedule_city(Place.build("Chino Hills, CA"), starting_city: true) +jason.schedule_city(Place.build("Brea, CA")) +jason.schedule_city(Place.build("Irvine, CA")) -puts phil.route -puts phil.miles_traveled -puts phil.time_traveled \ No newline at end of file +puts "-----------------------------" +puts "Here's my route:" +puts jason.route +puts "-----------------------------" +puts "This route will cover #{jason.miles_traveled.round(1)} miles, as the crow flies." +puts "If that crow flew 60 mph it'd take #{jason.time_traveled.round(2)} hours." \ No newline at end of file From fd90517fdbd71485649d7b255a930bd38a55448a Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 12 Feb 2014 22:49:19 -0800 Subject: [PATCH 08/11] rename calculates_route to route --- lib/{calculates_route.rb => route.rb} | 0 spec/calculates_route_spec.rb | 2 +- spec/sales_person_spec.rb | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename lib/{calculates_route.rb => route.rb} (100%) diff --git a/lib/calculates_route.rb b/lib/route.rb similarity index 100% rename from lib/calculates_route.rb rename to lib/route.rb diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index 3b02710..a8e9717 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -1,4 +1,4 @@ -require_relative "../lib/calculates_route" +require_relative "../lib/route" require_relative "../lib/place" describe Route do diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 65e9dea..7a3c518 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -1,5 +1,5 @@ require_relative "../lib/sales_person" -require_relative "../lib/calculates_route" +require_relative "../lib/route" describe SalesPerson do it "should have many cities" do From b92f36c48943f5c5dcc50ba3dfdfe4a6ce31772a Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 12 Feb 2014 22:51:57 -0800 Subject: [PATCH 09/11] update time_traveled from 60 to 55 mph --- lib/route.rb | 2 +- spec/calculates_route_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/route.rb b/lib/route.rb index c5a591d..46d8771 100644 --- a/lib/route.rb +++ b/lib/route.rb @@ -34,7 +34,7 @@ def add_to_miles_traveled(miles) end def time_traveled - @miles_traveled / 60 + @miles_traveled / 55 end end diff --git a/spec/calculates_route_spec.rb b/spec/calculates_route_spec.rb index a8e9717..6e0a26f 100644 --- a/spec/calculates_route_spec.rb +++ b/spec/calculates_route_spec.rb @@ -26,7 +26,7 @@ it "should calculate total travel time" do route.calculate(points) - route.time_traveled.should be_within(1).of(13) + route.time_traveled.should be_within(0.1).of(14.76) end end From 4124db5276a69f66aae7d3812f9fba39af1cddbd Mon Sep 17 00:00:00 2001 From: jason perez Date: Thu, 13 Feb 2014 11:35:41 -0800 Subject: [PATCH 10/11] update sales_person class to inject the route collaborator in the constructor --- lib/sales_person.rb | 4 ++-- ...calculates_route_spec.rb => route_spec.rb} | 0 spec/sales_person_spec.rb | 24 ++++++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) rename spec/{calculates_route_spec.rb => route_spec.rb} (100%) diff --git a/lib/sales_person.rb b/lib/sales_person.rb index 32b91ea..2b13da4 100644 --- a/lib/sales_person.rb +++ b/lib/sales_person.rb @@ -1,9 +1,9 @@ class SalesPerson attr_reader :cities, :starting_city - def initialize + def initialize(route = Route.new) @cities = [] - @route = Route.new + @route = route end def schedule_city(city, options={}) diff --git a/spec/calculates_route_spec.rb b/spec/route_spec.rb similarity index 100% rename from spec/calculates_route_spec.rb rename to spec/route_spec.rb diff --git a/spec/sales_person_spec.rb b/spec/sales_person_spec.rb index 7a3c518..6b5946e 100644 --- a/spec/sales_person_spec.rb +++ b/spec/sales_person_spec.rb @@ -2,6 +2,12 @@ require_relative "../lib/route" describe SalesPerson do + + before do + @route_double = double + @sales_person = SalesPerson.new(@route_double) + end + it "should have many cities" do city = double subject.schedule_city(city) @@ -18,14 +24,14 @@ it "should calculate a route via the Route" do cities = [double, double, double] - subject.stub(:cities) { cities } - Route.any_instance.should_receive(:calculate).with(cities) - subject.route + @sales_person.stub(:cities) { cities } + @route_double.should_receive(:calculate).with(cities) + @sales_person.route end it "should return the route from Route" do route_stub = [double, double] - Route.any_instance. stub(:calculate) { route_stub } - subject.route.should eq(route_stub) + @route_double.stub(:calculate) { route_stub } + @sales_person.route.should eq(route_stub) end it "should allow for the starting point to be specified" do @@ -35,12 +41,12 @@ end it "should calculate total miles traveled via Route" do - Route.any_instance.should_receive(:miles_traveled) - subject.miles_traveled + @route_double.should_receive(:miles_traveled) + @sales_person.miles_traveled end it "should return total time traveled at 60 mph via Route" do - Route.any_instance.should_receive(:time_traveled) - subject.time_traveled + @route_double.should_receive(:time_traveled) + @sales_person.time_traveled end end From d5bcb35844f0b43d7e57cba34583f3cf5b9fbff1 Mon Sep 17 00:00:00 2001 From: jason perez Date: Thu, 13 Feb 2014 11:50:55 -0800 Subject: [PATCH 11/11] update text in salesperson to say 55 instead of 60, update route #calculate with a while do end loop --- lib/route.rb | 2 +- salesperson.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/route.rb b/lib/route.rb index 46d8771..3ea85a1 100644 --- a/lib/route.rb +++ b/lib/route.rb @@ -9,7 +9,7 @@ def calculate(points) remaining_points = points route = [] route << remaining_points.slice!(0) - until remaining_points == [] do + while remaining_points.any? do next_point = shortest_distance(route.last, remaining_points) route << remaining_points.slice!(remaining_points.index(next_point)) end diff --git a/salesperson.rb b/salesperson.rb index ce84ae8..e6d8eed 100644 --- a/salesperson.rb +++ b/salesperson.rb @@ -12,4 +12,4 @@ puts jason.route puts "-----------------------------" puts "This route will cover #{jason.miles_traveled.round(1)} miles, as the crow flies." -puts "If that crow flew 60 mph it'd take #{jason.time_traveled.round(2)} hours." \ No newline at end of file +puts "If that crow flew 55 mph it'd take #{jason.time_traveled.round(2)} hours." \ No newline at end of file