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/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/lib/vehicle_collector.rb b/lib/vehicle_collector.rb new file mode 100644 index 0000000..8f8511e --- /dev/null +++ b/lib/vehicle_collector.rb @@ -0,0 +1,20 @@ +module VehicleCollector + + @@vehicles = [] + + def self.show_vehicle_names + @@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 new file mode 100644 index 0000000..c2607d7 --- /dev/null +++ b/models/automobile.rb @@ -0,0 +1,13 @@ +require_relative 'vehicle' +class Automobile < Vehicle + + attr_reader :color, :make, :model, :year, :type + + def initialize(options = {}) + @color = options[:color] + @make = options[:make] + @model = options[:model] + @year = options[:year] + @type = Vehicle.new(self) + end +end diff --git a/models/motorcycle.rb b/models/motorcycle.rb new file mode 100644 index 0000000..55cd2d5 --- /dev/null +++ b/models/motorcycle.rb @@ -0,0 +1,14 @@ +require_relative 'vehicle' +class Motorcycle < Vehicle + + attr_reader :name, :type + + def initialize(options = {}) + @name = options[:name] + @type = Vehicle.new(self) + end + + def self.wheels + 2 + end +end diff --git a/models/vehicle.rb b/models/vehicle.rb new file mode 100644 index 0000000..24b1f6d --- /dev/null +++ b/models/vehicle.rb @@ -0,0 +1,21 @@ +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 } + automobiles.keep_if do |automobile| + automobile.color == 'blue' && + automobile.make == 'Honda' && + automobile.model == 'Accord' + end + end + + def self.wheels + 4 + end +end diff --git a/spec/lib/vehicle_collector_spec.rb b/spec/lib/vehicle_collector_spec.rb new file mode 100644 index 0000000..69e26a7 --- /dev/null +++ b/spec/lib/vehicle_collector_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +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_vehicle_names).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 new file mode 100644 index 0000000..2d3fdca --- /dev/null +++ b/spec/models/automobile_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +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.type).to be_instance_of Vehicle + 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 new file mode 100644 index 0000000..9f83783 --- /dev/null +++ b/spec/models/motorcycle_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Motorcycle, '.initialize' do + it 'has a name and type when instantiated' do + @@vehicles = [] + motorcycle = Motorcycle.new(name: 'Harley Davidson') + + expect(motorcycle.type).to be_instance_of Vehicle + expect(motorcycle.name).to eq 'Harley Davidson' + end +end + +describe Motorcycle, '.wheels' do + it 'overrides Vehicle and returns 2 wheels' do + expect(Motorcycle.wheels).to eq 2 + end +end diff --git a/spec/models/vehicle_spec.rb b/spec/models/vehicle_spec.rb new file mode 100644 index 0000000..46beafa --- /dev/null +++ b/spec/models/vehicle_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..f927c39 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,6 @@ +require 'rspec' +require 'bundler/setup' +require_relative '../models/automobile' +require_relative '../models/vehicle' +require_relative '../models/motorcycle' +require_relative '../lib/vehicle_collector'