From 33f68e07ef9a106d0de58630c907ec0478ca4bd3 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Fri, 21 Dec 2012 23:29:17 +1100 Subject: [PATCH 1/7] Panda Level: Completed using TDD and applied Thoughtbot's style guide for writing Unit Tests --- Gemfile.lock | 38 +++++++++++++++++----------------- models/automobile.rb | 15 ++++++++++++++ spec/models/automobile_spec.rb | 17 +++++++++++++++ spec/spec_helper.rb | 3 +++ 4 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 models/automobile.rb create mode 100644 spec/models/automobile_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/Gemfile.lock b/Gemfile.lock index 0facad0..809e913 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,33 +1,33 @@ GEM remote: http://rubygems.org/ specs: - activemodel (3.2.3) - activesupport (= 3.2.3) + activemodel (3.2.9) + activesupport (= 3.2.9) builder (~> 3.0.0) - activerecord (3.2.3) - activemodel (= 3.2.3) - activesupport (= 3.2.3) + activerecord (3.2.9) + activemodel (= 3.2.9) + activesupport (= 3.2.9) arel (~> 3.0.2) tzinfo (~> 0.3.29) - activesupport (3.2.3) + activesupport (3.2.9) i18n (~> 0.6) multi_json (~> 1.0) arel (3.0.2) - builder (3.0.0) + builder (3.0.4) diff-lcs (1.1.3) - i18n (0.6.0) - multi_json (1.3.4) - pg (0.13.2) - rake (0.9.2.2) - rspec (2.10.0) - rspec-core (~> 2.10.0) - rspec-expectations (~> 2.10.0) - rspec-mocks (~> 2.10.0) - rspec-core (2.10.0) - rspec-expectations (2.10.0) + i18n (0.6.1) + multi_json (1.5.0) + pg (0.14.1) + rake (10.0.3) + rspec (2.12.0) + rspec-core (~> 2.12.0) + rspec-expectations (~> 2.12.0) + rspec-mocks (~> 2.12.0) + rspec-core (2.12.2) + rspec-expectations (2.12.1) diff-lcs (~> 1.1.3) - rspec-mocks (2.10.1) - tzinfo (0.3.33) + rspec-mocks (2.12.0) + tzinfo (0.3.35) PLATFORMS ruby diff --git a/models/automobile.rb b/models/automobile.rb new file mode 100644 index 0000000..38ee0f1 --- /dev/null +++ b/models/automobile.rb @@ -0,0 +1,15 @@ +class Automobile + + attr_reader :color, :make, :model, :year + + def initialize(options = {}) + @color = options[:color] + @make = options[:make] + @model = options[:model] + @year = options[:year] + end + + def self.wheels + 4 # Hardcoded value since there were no further intructions regarding what an automobile is. + end +end diff --git a/spec/models/automobile_spec.rb b/spec/models/automobile_spec.rb new file mode 100644 index 0000000..828107f --- /dev/null +++ b/spec/models/automobile_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Automobile, '.wheels' do + it 'returns the number of wheels' do + Automobile.wheels.should eq 4 + end +end + +describe Automobile, '.initialize' do + it 'can receive a hash containing color, make, model and year to update its variables' do + car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) + expect(car.color).to eq 'blue' + expect(car.make).to eq 'BMW' + expect(car.model).to eq '325i' + expect(car.year).to eq 1995 + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..fc5a17f --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,3 @@ +require 'rspec' +require 'bundler/setup' +require_relative '../models/automobile' From 0077961db34ace321afb2ca3a6417f1b236ead16 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Fri, 21 Dec 2012 23:45:49 +1100 Subject: [PATCH 2/7] Tiger Level: Completed --- block_review.rb | 4 ++-- models/automobile.rb | 7 ++----- models/motorcycle.rb | 7 +++++++ models/vehicle.rb | 6 ++++++ spec/models/motorcycle_spec.rb | 4 ++++ spec/models/vehicle_spec.rb | 4 ++++ spec/spec_helper.rb | 2 ++ 7 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 models/motorcycle.rb create mode 100644 models/vehicle.rb create mode 100644 spec/models/motorcycle_spec.rb create mode 100644 spec/models/vehicle_spec.rb diff --git a/block_review.rb b/block_review.rb index fcdefc7..1a5a820 100644 --- a/block_review.rb +++ b/block_review.rb @@ -1,7 +1,7 @@ require 'rubygems' require 'bundler/setup' -require_relative 'db/setup' -Dir.glob("./**/*.rb").each {|f| require f} +#require_relative 'db/setup' +Dir.glob("./models/*.rb").each {|f| require f} puts "Serenity now!" diff --git a/models/automobile.rb b/models/automobile.rb index 38ee0f1..a1174fb 100644 --- a/models/automobile.rb +++ b/models/automobile.rb @@ -1,4 +1,5 @@ -class Automobile +require_relative 'vehicle' +class Automobile < Vehicle attr_reader :color, :make, :model, :year @@ -8,8 +9,4 @@ def initialize(options = {}) @model = options[:model] @year = options[:year] end - - def self.wheels - 4 # Hardcoded value since there were no further intructions regarding what an automobile is. - end end diff --git a/models/motorcycle.rb b/models/motorcycle.rb new file mode 100644 index 0000000..e0dfadf --- /dev/null +++ b/models/motorcycle.rb @@ -0,0 +1,7 @@ +require_relative 'vehicle' +class Motorcycle < Vehicle + + def self.wheels + 2 + end +end diff --git a/models/vehicle.rb b/models/vehicle.rb new file mode 100644 index 0000000..e8fd1ec --- /dev/null +++ b/models/vehicle.rb @@ -0,0 +1,6 @@ +class Vehicle + + def self.wheels + 4 # Hardcoded value since there were no further intructions regarding what an automobile is. + end +end diff --git a/spec/models/motorcycle_spec.rb b/spec/models/motorcycle_spec.rb new file mode 100644 index 0000000..93ab1fc --- /dev/null +++ b/spec/models/motorcycle_spec.rb @@ -0,0 +1,4 @@ +require 'spec_helper' + +describe Motorcycle do +end diff --git a/spec/models/vehicle_spec.rb b/spec/models/vehicle_spec.rb new file mode 100644 index 0000000..8de58be --- /dev/null +++ b/spec/models/vehicle_spec.rb @@ -0,0 +1,4 @@ +require 'spec_helper' + +describe Vehicle do +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fc5a17f..15ecb93 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ require 'rspec' require 'bundler/setup' require_relative '../models/automobile' +require_relative '../models/vehicle' +require_relative '../models/motorcycle' From 0370eff99e4b90a87e50951ba275159aea663fd6 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sat, 22 Dec 2012 00:28:30 +1100 Subject: [PATCH 3/7] Eagle Level: Completed --- models/automobile.rb | 5 +++++ models/motorcycle.rb | 7 +++++++ models/vehicle.rb | 12 ++++++++++++ spec/models/automobile_spec.rb | 10 ++++++++++ spec/models/motorcycle_spec.rb | 14 +++++++++++++- spec/models/vehicle_spec.rb | 20 +++++++++++++++++++- 6 files changed, 66 insertions(+), 2 deletions(-) diff --git a/models/automobile.rb b/models/automobile.rb index a1174fb..318142f 100644 --- a/models/automobile.rb +++ b/models/automobile.rb @@ -8,5 +8,10 @@ def initialize(options = {}) @make = options[:make] @model = options[:model] @year = options[:year] + add_to_vehicles + end + + def add_to_vehicles + @@vehicles << self end end diff --git a/models/motorcycle.rb b/models/motorcycle.rb index e0dfadf..89f1f4c 100644 --- a/models/motorcycle.rb +++ b/models/motorcycle.rb @@ -1,7 +1,14 @@ require_relative 'vehicle' class Motorcycle < Vehicle + def initialize + add_to_vehicles + end def self.wheels 2 end + + def add_to_vehicles + @@vehicles << self + end end diff --git a/models/vehicle.rb b/models/vehicle.rb index e8fd1ec..b24df0e 100644 --- a/models/vehicle.rb +++ b/models/vehicle.rb @@ -1,6 +1,18 @@ class Vehicle + @@vehicles = [] + def self.wheels 4 # Hardcoded value since there were no further intructions regarding what an automobile is. end + + def self.blue_honda_accords + automobiles = @@vehicles.select { |vehicle| vehicle.class == Automobile } + automobiles.keep_if do |automobile| + automobile.color == 'blue' && + automobile.make == 'Honda' && + automobile.model == 'Accord' + end + end + end diff --git a/spec/models/automobile_spec.rb b/spec/models/automobile_spec.rb index 828107f..1f954cc 100644 --- a/spec/models/automobile_spec.rb +++ b/spec/models/automobile_spec.rb @@ -9,9 +9,19 @@ describe Automobile, '.initialize' do it 'can receive a hash containing color, make, model and year to update its variables' do car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) + expect(car.color).to eq 'blue' expect(car.make).to eq 'BMW' expect(car.model).to eq '325i' expect(car.year).to eq 1995 end end + +describe Automobile, '#add_to_vehicles' do + it 'adds itself to @@vehicles array' do + @@vehicles = [] + car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) + + expect(@@vehicles.count).to eq 1 + end +end diff --git a/spec/models/motorcycle_spec.rb b/spec/models/motorcycle_spec.rb index 93ab1fc..a1a96b8 100644 --- a/spec/models/motorcycle_spec.rb +++ b/spec/models/motorcycle_spec.rb @@ -1,4 +1,16 @@ require 'spec_helper' -describe Motorcycle do +describe Motorcycle, '.wheels' do + it 'overrides Vehicle and returns 2 wheels' do + expect(Motorcycle.wheels).to eq 2 + end +end + +describe Motorcycle, '#add_to_vehicles' do + it 'adds itself to @@vehicles array' do + @@vehicles = [] + motorcycle = Motorcycle.new + + expect(@@vehicles.count).to eq 1 + end end diff --git a/spec/models/vehicle_spec.rb b/spec/models/vehicle_spec.rb index 8de58be..8e1ca88 100644 --- a/spec/models/vehicle_spec.rb +++ b/spec/models/vehicle_spec.rb @@ -1,4 +1,22 @@ require 'spec_helper' -describe Vehicle do +describe Vehicle, '@@vehicles' do + it 'stores all vehicles in a global variable' do + @@vehicles = [] + Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) + Motorcycle.new + expect(@@vehicles.count).to eq 2 + end +end + +describe Vehicle, '#blue_honda_accords' do + it 'returns only blue honda accords' do + car = Automobile.new(color: 'blue', make: 'Honda', model: 'Accord', year: 1995) + another_car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) + motorcycle = Motorcycle.new + + blue_hondas = Vehicle.blue_honda_accords + + expect(blue_hondas).to eq [car] + end end From cb8ac295d6ffec7c7aba3396f1c944ad8a1a40cd Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 23 Dec 2012 14:14:05 +1100 Subject: [PATCH 4/7] refactoring to move vehicle counter to Vehicle class using inheritance to make code DRYer --- models/automobile.rb | 6 +----- models/motorcycle.rb | 6 +----- models/vehicle.rb | 3 +++ spec/models/automobile_spec.rb | 9 --------- spec/models/motorcycle_spec.rb | 9 --------- spec/models/vehicle_spec.rb | 10 ++++++++++ 6 files changed, 15 insertions(+), 28 deletions(-) diff --git a/models/automobile.rb b/models/automobile.rb index 318142f..d8a7210 100644 --- a/models/automobile.rb +++ b/models/automobile.rb @@ -8,10 +8,6 @@ def initialize(options = {}) @make = options[:make] @model = options[:model] @year = options[:year] - add_to_vehicles - end - - def add_to_vehicles - @@vehicles << self + add_vehicles(self) end end diff --git a/models/motorcycle.rb b/models/motorcycle.rb index 89f1f4c..4289981 100644 --- a/models/motorcycle.rb +++ b/models/motorcycle.rb @@ -1,14 +1,10 @@ require_relative 'vehicle' class Motorcycle < Vehicle def initialize - add_to_vehicles + add_vehicles(self) end def self.wheels 2 end - - def add_to_vehicles - @@vehicles << self - end end diff --git a/models/vehicle.rb b/models/vehicle.rb index b24df0e..af6baf7 100644 --- a/models/vehicle.rb +++ b/models/vehicle.rb @@ -15,4 +15,7 @@ def self.blue_honda_accords end end + def add_vehicles(vehicle) + @@vehicles << vehicle + end end diff --git a/spec/models/automobile_spec.rb b/spec/models/automobile_spec.rb index 1f954cc..b160842 100644 --- a/spec/models/automobile_spec.rb +++ b/spec/models/automobile_spec.rb @@ -16,12 +16,3 @@ expect(car.year).to eq 1995 end end - -describe Automobile, '#add_to_vehicles' do - it 'adds itself to @@vehicles array' do - @@vehicles = [] - car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) - - expect(@@vehicles.count).to eq 1 - end -end diff --git a/spec/models/motorcycle_spec.rb b/spec/models/motorcycle_spec.rb index a1a96b8..eccc092 100644 --- a/spec/models/motorcycle_spec.rb +++ b/spec/models/motorcycle_spec.rb @@ -5,12 +5,3 @@ expect(Motorcycle.wheels).to eq 2 end end - -describe Motorcycle, '#add_to_vehicles' do - it 'adds itself to @@vehicles array' do - @@vehicles = [] - motorcycle = Motorcycle.new - - expect(@@vehicles.count).to eq 1 - end -end diff --git a/spec/models/vehicle_spec.rb b/spec/models/vehicle_spec.rb index 8e1ca88..ce72afa 100644 --- a/spec/models/vehicle_spec.rb +++ b/spec/models/vehicle_spec.rb @@ -20,3 +20,13 @@ expect(blue_hondas).to eq [car] end end + +describe Vehicle, '#add_vehicles' do + it 'adds new instances of subclasses to @@vehicles when created' do + @@vehicles = [] + car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) + motorcycle = Motorcycle.new + + expect(@@vehicles.count).to eq 2 + end +end From 57a9a5e7f1e20afe00c32bbcea4a0ca790b80835 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 23 Dec 2012 17:37:00 +1100 Subject: [PATCH 5/7] Added a VehicleCollector class to deal with collecting vehicles and showing them --- models/automobile.rb | 5 +++-- models/motorcycle.rb | 8 +++++-- models/vehicle.rb | 10 ++------- models/vehicle_collector.rb | 19 +++++++++++++++++ spec/models/automobile_spec.rb | 23 ++++++++++++++------ spec/models/motorcycle_spec.rb | 11 ++++++++++ spec/models/vehicle_collector_spec.rb | 30 +++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + 8 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 models/vehicle_collector.rb create mode 100644 spec/models/vehicle_collector_spec.rb diff --git a/models/automobile.rb b/models/automobile.rb index d8a7210..fc8cafb 100644 --- a/models/automobile.rb +++ b/models/automobile.rb @@ -1,13 +1,14 @@ require_relative 'vehicle' +require_relative 'vehicle_collector' class Automobile < Vehicle - attr_reader :color, :make, :model, :year + attr_reader :color, :make, :model, :year, :collector def initialize(options = {}) @color = options[:color] @make = options[:make] @model = options[:model] @year = options[:year] - add_vehicles(self) + @collector = VehicleCollector.new(self) end end diff --git a/models/motorcycle.rb b/models/motorcycle.rb index 4289981..d571445 100644 --- a/models/motorcycle.rb +++ b/models/motorcycle.rb @@ -1,7 +1,11 @@ require_relative 'vehicle' class Motorcycle < Vehicle - def initialize - add_vehicles(self) + + attr_reader :name, :collector + + def initialize(options = {}) + @name = options[:name] + @collector = VehicleCollector.new(self) end def self.wheels diff --git a/models/vehicle.rb b/models/vehicle.rb index af6baf7..3193d96 100644 --- a/models/vehicle.rb +++ b/models/vehicle.rb @@ -1,11 +1,5 @@ class Vehicle - @@vehicles = [] - - def self.wheels - 4 # Hardcoded value since there were no further intructions regarding what an automobile is. - end - def self.blue_honda_accords automobiles = @@vehicles.select { |vehicle| vehicle.class == Automobile } automobiles.keep_if do |automobile| @@ -15,7 +9,7 @@ def self.blue_honda_accords end end - def add_vehicles(vehicle) - @@vehicles << vehicle + def self.wheels + 4 end end diff --git a/models/vehicle_collector.rb b/models/vehicle_collector.rb new file mode 100644 index 0000000..3542cc4 --- /dev/null +++ b/models/vehicle_collector.rb @@ -0,0 +1,19 @@ +class VehicleCollector + + @@vehicles = [] + + attr_reader :vehicle + + def self.show_vehicles + @@vehicles.map { |vehicle| vehicle.name } + end + + def initialize(vehicle) + @vehicle = vehicle + add_vehicles + end + + def add_vehicles + @@vehicles << vehicle + end +end diff --git a/spec/models/automobile_spec.rb b/spec/models/automobile_spec.rb index b160842..3629c52 100644 --- a/spec/models/automobile_spec.rb +++ b/spec/models/automobile_spec.rb @@ -1,18 +1,29 @@ require 'spec_helper' -describe Automobile, '.wheels' do - it 'returns the number of wheels' do - Automobile.wheels.should eq 4 - end -end - describe Automobile, '.initialize' do it 'can receive a hash containing color, make, model and year to update its variables' do + @@vehicles = [] car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) expect(car.color).to eq 'blue' expect(car.make).to eq 'BMW' expect(car.model).to eq '325i' expect(car.year).to eq 1995 + expect(car.collector).to be_instance_of VehicleCollector + end +end + +describe Automobile, '.initialize' do + it 'has a vehicle collector when instantiated' do + @@vehicles = [] + car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) + + expect(@@vehicles).to eq [car] + end +end + +describe Automobile, '.wheels' do + it 'returns the number of wheels' do + Automobile.wheels.should eq 4 end end diff --git a/spec/models/motorcycle_spec.rb b/spec/models/motorcycle_spec.rb index eccc092..d143a3f 100644 --- a/spec/models/motorcycle_spec.rb +++ b/spec/models/motorcycle_spec.rb @@ -1,5 +1,16 @@ require 'spec_helper' +describe Motorcycle, '.initialize' do + it 'has a name and vehicle collector when instantiated' do + @@vehicles = [] + motorcycle = Motorcycle.new(name: 'Harley Davidson') + + expect(motorcycle.collector).to be_instance_of VehicleCollector + expect(motorcycle.name).to eq 'Harley Davidson' + expect(@@vehicles).to eq [motorcycle] + end +end + describe Motorcycle, '.wheels' do it 'overrides Vehicle and returns 2 wheels' do expect(Motorcycle.wheels).to eq 2 diff --git a/spec/models/vehicle_collector_spec.rb b/spec/models/vehicle_collector_spec.rb new file mode 100644 index 0000000..8e395bb --- /dev/null +++ b/spec/models/vehicle_collector_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe VehicleCollector, '.initialize' do + it 'receives a subclass of vehicle as an attribute when instantiated' do + @@vehicles = [] + motorcycle = Motorcycle.new + collector = VehicleCollector.new(motorcycle) + + expect(collector.vehicle).to eq motorcycle + end +end + +describe VehicleCollector, '.show_vehicles' do + it 'outputs all collected vehicles' do + @@vehicles = [] + motorcycle = Motorcycle.new(name: 'Harley Davidson') + another_motorcycle = Motorcycle.new(name: 'Honda') + + expect(VehicleCollector.show_vehicles).to eq ['Harley Davidson', 'Honda'] + end +end + +describe VehicleCollector, '#add_vehicles' do + it 'adds vehicles to global variable' do + @@vehicles = [] + car = Automobile.new + + expect(@@vehicles.length).to eq 1 + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 15ecb93..e16128d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,3 +3,4 @@ require_relative '../models/automobile' require_relative '../models/vehicle' require_relative '../models/motorcycle' +require_relative '../models/vehicle_collector' From c64dde18a63801fa8b8575fc8f67d362b9f88ce7 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 23 Dec 2012 19:57:57 +1100 Subject: [PATCH 6/7] Played some more: refactored to make VehicleCollector a module instead and included it in Vehicle class --- lib/vehicle_collector.rb | 20 ++++++++++++++++ models/automobile.rb | 5 ++-- models/motorcycle.rb | 4 ++-- models/vehicle.rb | 6 +++++ spec/lib/vehicle_collector_spec.rb | 38 ++++++++++++++++++++++++++++++ spec/models/automobile_spec.rb | 11 +-------- spec/models/motorcycle_spec.rb | 5 ++-- spec/models/vehicle_spec.rb | 21 +---------------- spec/spec_helper.rb | 2 +- 9 files changed, 73 insertions(+), 39 deletions(-) create mode 100644 lib/vehicle_collector.rb create mode 100644 spec/lib/vehicle_collector_spec.rb diff --git a/lib/vehicle_collector.rb b/lib/vehicle_collector.rb new file mode 100644 index 0000000..6b601eb --- /dev/null +++ b/lib/vehicle_collector.rb @@ -0,0 +1,20 @@ +module VehicleCollector + + @@vehicles = [] + + def self.show_vehicles + @@vehicles.map { |vehicle| vehicle.name } + end + + def self.add_vehicles(vehicle) + @@vehicles << vehicle + end + + def self.get + @@vehicles + end + + def self.reset + @@vehicles.clear + end +end diff --git a/models/automobile.rb b/models/automobile.rb index fc8cafb..c2607d7 100644 --- a/models/automobile.rb +++ b/models/automobile.rb @@ -1,14 +1,13 @@ require_relative 'vehicle' -require_relative 'vehicle_collector' class Automobile < Vehicle - attr_reader :color, :make, :model, :year, :collector + attr_reader :color, :make, :model, :year, :type def initialize(options = {}) @color = options[:color] @make = options[:make] @model = options[:model] @year = options[:year] - @collector = VehicleCollector.new(self) + @type = Vehicle.new(self) end end diff --git a/models/motorcycle.rb b/models/motorcycle.rb index d571445..55cd2d5 100644 --- a/models/motorcycle.rb +++ b/models/motorcycle.rb @@ -1,11 +1,11 @@ require_relative 'vehicle' class Motorcycle < Vehicle - attr_reader :name, :collector + attr_reader :name, :type def initialize(options = {}) @name = options[:name] - @collector = VehicleCollector.new(self) + @type = Vehicle.new(self) end def self.wheels diff --git a/models/vehicle.rb b/models/vehicle.rb index 3193d96..24b1f6d 100644 --- a/models/vehicle.rb +++ b/models/vehicle.rb @@ -1,4 +1,10 @@ +require './lib/vehicle_collector' class Vehicle + include VehicleCollector + + def initialize(vehicle) + VehicleCollector.add_vehicles(vehicle) + end def self.blue_honda_accords automobiles = @@vehicles.select { |vehicle| vehicle.class == Automobile } diff --git a/spec/lib/vehicle_collector_spec.rb b/spec/lib/vehicle_collector_spec.rb new file mode 100644 index 0000000..ce58194 --- /dev/null +++ b/spec/lib/vehicle_collector_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe VehicleCollector, '.show_vehicles' do + it 'outputs all collected vehicles' do + VehicleCollector.reset + motorcycle = Motorcycle.new(name: 'Harley Davidson') + another_motorcycle = Motorcycle.new(name: 'Honda') + + expect(VehicleCollector.show_vehicles).to eq ['Harley Davidson', 'Honda'] + end +end + +describe VehicleCollector, '.add_vehicles' do + it 'adds vehicles to class variable' do + motorcycle = Motorcycle.new(name: 'Harley Davidson') + # Reset here since add_vehicles is called in initialize + VehicleCollector.reset + expect(VehicleCollector.add_vehicles(motorcycle).length).to eq 1 + end +end + +describe VehicleCollector, '.get' do + it 'returns @@vehicles' do + VehicleCollector.reset + car = Automobile.new + + expect(VehicleCollector.get.first).to eq car + end +end + +describe VehicleCollector, '.reset' do + it 'clears out @@vehicles' do + motorcycle = Motorcycle.new(name: 'Harley Davidson') + VehicleCollector.reset + + expect(VehicleCollector.get).to be_empty + end +end diff --git a/spec/models/automobile_spec.rb b/spec/models/automobile_spec.rb index 3629c52..2d3fdca 100644 --- a/spec/models/automobile_spec.rb +++ b/spec/models/automobile_spec.rb @@ -9,16 +9,7 @@ expect(car.make).to eq 'BMW' expect(car.model).to eq '325i' expect(car.year).to eq 1995 - expect(car.collector).to be_instance_of VehicleCollector - end -end - -describe Automobile, '.initialize' do - it 'has a vehicle collector when instantiated' do - @@vehicles = [] - car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) - - expect(@@vehicles).to eq [car] + expect(car.type).to be_instance_of Vehicle end end diff --git a/spec/models/motorcycle_spec.rb b/spec/models/motorcycle_spec.rb index d143a3f..9f83783 100644 --- a/spec/models/motorcycle_spec.rb +++ b/spec/models/motorcycle_spec.rb @@ -1,13 +1,12 @@ require 'spec_helper' describe Motorcycle, '.initialize' do - it 'has a name and vehicle collector when instantiated' do + it 'has a name and type when instantiated' do @@vehicles = [] motorcycle = Motorcycle.new(name: 'Harley Davidson') - expect(motorcycle.collector).to be_instance_of VehicleCollector + expect(motorcycle.type).to be_instance_of Vehicle expect(motorcycle.name).to eq 'Harley Davidson' - expect(@@vehicles).to eq [motorcycle] end end diff --git a/spec/models/vehicle_spec.rb b/spec/models/vehicle_spec.rb index ce72afa..46beafa 100644 --- a/spec/models/vehicle_spec.rb +++ b/spec/models/vehicle_spec.rb @@ -1,15 +1,6 @@ require 'spec_helper' -describe Vehicle, '@@vehicles' do - it 'stores all vehicles in a global variable' do - @@vehicles = [] - Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) - Motorcycle.new - expect(@@vehicles.count).to eq 2 - end -end - -describe Vehicle, '#blue_honda_accords' do +describe Vehicle, '.blue_honda_accords' do it 'returns only blue honda accords' do car = Automobile.new(color: 'blue', make: 'Honda', model: 'Accord', year: 1995) another_car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) @@ -20,13 +11,3 @@ expect(blue_hondas).to eq [car] end end - -describe Vehicle, '#add_vehicles' do - it 'adds new instances of subclasses to @@vehicles when created' do - @@vehicles = [] - car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) - motorcycle = Motorcycle.new - - expect(@@vehicles.count).to eq 2 - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e16128d..f927c39 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,4 +3,4 @@ require_relative '../models/automobile' require_relative '../models/vehicle' require_relative '../models/motorcycle' -require_relative '../models/vehicle_collector' +require_relative '../lib/vehicle_collector' From f0ba60499e3f81f4205b7cfa6c494ab612ba8262 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 23 Dec 2012 20:02:20 +1100 Subject: [PATCH 7/7] Renamed method and removed old files --- lib/vehicle_collector.rb | 2 +- models/vehicle_collector.rb | 19 ----------------- spec/lib/vehicle_collector_spec.rb | 4 ++-- spec/models/vehicle_collector_spec.rb | 30 --------------------------- 4 files changed, 3 insertions(+), 52 deletions(-) delete mode 100644 models/vehicle_collector.rb delete mode 100644 spec/models/vehicle_collector_spec.rb diff --git a/lib/vehicle_collector.rb b/lib/vehicle_collector.rb index 6b601eb..8f8511e 100644 --- a/lib/vehicle_collector.rb +++ b/lib/vehicle_collector.rb @@ -2,7 +2,7 @@ module VehicleCollector @@vehicles = [] - def self.show_vehicles + def self.show_vehicle_names @@vehicles.map { |vehicle| vehicle.name } end diff --git a/models/vehicle_collector.rb b/models/vehicle_collector.rb deleted file mode 100644 index 3542cc4..0000000 --- a/models/vehicle_collector.rb +++ /dev/null @@ -1,19 +0,0 @@ -class VehicleCollector - - @@vehicles = [] - - attr_reader :vehicle - - def self.show_vehicles - @@vehicles.map { |vehicle| vehicle.name } - end - - def initialize(vehicle) - @vehicle = vehicle - add_vehicles - end - - def add_vehicles - @@vehicles << vehicle - end -end diff --git a/spec/lib/vehicle_collector_spec.rb b/spec/lib/vehicle_collector_spec.rb index ce58194..69e26a7 100644 --- a/spec/lib/vehicle_collector_spec.rb +++ b/spec/lib/vehicle_collector_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' -describe VehicleCollector, '.show_vehicles' do +describe VehicleCollector, '.show_vehicle_names' do it 'outputs all collected vehicles' do VehicleCollector.reset motorcycle = Motorcycle.new(name: 'Harley Davidson') another_motorcycle = Motorcycle.new(name: 'Honda') - expect(VehicleCollector.show_vehicles).to eq ['Harley Davidson', 'Honda'] + expect(VehicleCollector.show_vehicle_names).to eq ['Harley Davidson', 'Honda'] end end diff --git a/spec/models/vehicle_collector_spec.rb b/spec/models/vehicle_collector_spec.rb deleted file mode 100644 index 8e395bb..0000000 --- a/spec/models/vehicle_collector_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'spec_helper' - -describe VehicleCollector, '.initialize' do - it 'receives a subclass of vehicle as an attribute when instantiated' do - @@vehicles = [] - motorcycle = Motorcycle.new - collector = VehicleCollector.new(motorcycle) - - expect(collector.vehicle).to eq motorcycle - end -end - -describe VehicleCollector, '.show_vehicles' do - it 'outputs all collected vehicles' do - @@vehicles = [] - motorcycle = Motorcycle.new(name: 'Harley Davidson') - another_motorcycle = Motorcycle.new(name: 'Honda') - - expect(VehicleCollector.show_vehicles).to eq ['Harley Davidson', 'Honda'] - end -end - -describe VehicleCollector, '#add_vehicles' do - it 'adds vehicles to global variable' do - @@vehicles = [] - car = Automobile.new - - expect(@@vehicles.length).to eq 1 - end -end