diff --git a/models/eagle.rb b/models/eagle.rb new file mode 100644 index 0000000..cc74b06 --- /dev/null +++ b/models/eagle.rb @@ -0,0 +1,70 @@ +class Vehicle + + @@vehicles = [] + def initialize + + end + + def self.count + @@vehicles.count + end + + def self.search(args) + + results = @@vehicles + args.each do |key, value| + results.select!{|vehicle| vehicle.send(key) == value.to_sym} + end + results + end + + def self.all_vehicles + @@vehicles + end + + def self.wheels + 2 + end +end + + +class Automobile < Vehicle + @@auto = [] + attr_accessor :color, :make, :model, :year + def self.wheels + 4 + end + + def initialize(args) + @color = args[:color] + @make = args[:make] + @model = args[:model] + @year = args[:year] + super() + end + + def self.build(args) + auto = Automobile.new(args) + @@vehicles << auto + @@auto << auto + auto + end + + def update(new_args) + @color = new_args[:color] if new_args[:color] + @make = new_args[:make] if new_args[:make] + @model = new_args[:model] if new_args[:model] + @year = new_args[:year] if new_args[:year] + end + + def self.all_autos + @@auto + end + + def self.count + @@auto.count + end +end + +class Motorcycle < Vehicle +end diff --git a/models/panda.rb b/models/panda.rb new file mode 100644 index 0000000..bcc9a8a --- /dev/null +++ b/models/panda.rb @@ -0,0 +1,15 @@ +class Automobile + attr_accessor :color, :make, :model, :year + def self.wheels + 4 + end + + def initialize(args) + @color = args[:color] + @make = args[:make] + @model = args[:model] + @year = args[:year] + end +end + +puts Automobile.wheels diff --git a/models/tiger.rb b/models/tiger.rb new file mode 100644 index 0000000..6f63eb9 --- /dev/null +++ b/models/tiger.rb @@ -0,0 +1,40 @@ +class Vehicle + def self.wheels + 2 + end +end + +class Automobile < Vehicle + + attr_accessor :color, :make, :model, :year + def self.wheels + 4 + end + + def initialize(args) + @color = args[:color] + @make = args[:make] + @model = args[:model] + @year = args[:year] + end + + def update(new_args) + @color = new_args[:color] if new_args[:color] + @make = new_args[:make] if new_args[:make] + @model = new_args[:model] if new_args[:model] + @year = new_args[:year] if new_args[:year] + end +end + +class Motorcycle < Vehicle +end + +puts Motorcycle.wheels + +auto = Automobile.new(color: 'Red', make: 'Ford', model: 'Mustang', year: 2007) +p auto +auto.update(color: 'Blue', year: 2012) +puts auto.color +puts auto.year +puts auto.model + diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb new file mode 100644 index 0000000..9b286b5 --- /dev/null +++ b/spec/automobile_spec.rb @@ -0,0 +1,115 @@ +require 'rspec' +require '../models/eagle' + +describe Automobile do + describe '.wheels' do + it 'should return four' do + Automobile.wheels.should eq(4) + end + end + + let (:auto) { Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007)} + + describe "#new" do + it "takes four parameters and returns a Automobile object" do + auto.should be_an_instance_of Automobile + end + + it 'should inherit from vehicle' do + auto.is_a?(Vehicle).should be_true + end + end + + describe "#model" do + it 'should have model attribute' do + auto.should respond_to(:model) + end + + it "returns the correct model" do + auto.model.should eql 'Mustang' + end + end + + describe "#color" do + it 'should have color attribute' do + auto.should respond_to(:color) + end + + it "returns the correct color" do + auto.color.should eql 'Red' + end + end + + describe "#make" do + it 'should have make attribute' do + auto.should respond_to(:make) + end + + it "returns the correct make" do + auto.make.should eql 'Ford' + end + end + + describe "#year" do + it 'should have year attribute' do + auto.should respond_to(:year) + end + + it "returns the correct year" do + auto.year.should eql 2007 + end + end + + describe "#update" do + context "with no parameters" do + it "should keep the Automobile unchanged if no parameters are given " do + auto.update({}) + auto.model.should eql 'Mustang' + auto.color.should eql 'Red' + auto.make.should eql 'Ford' + auto.year.should eql 2007 + end + end + + it 'should update the model attribute' do + auto.update(model: 'Calibre') + auto.model.should eql 'Calibre' + end + it 'should update the color attribute' do + auto.update(color: 'Blue') + auto.color.should eql 'Blue' + end + it 'should update the make attribute' do + auto.update(make: 'Dodge') + auto.make.should eql 'Dodge' + end + it 'should update the year attribute' do + auto.update(year: 2009) + auto.year.should eql 2009 + end + + context "with all parameters" do + it 'should takes four parameters and changes the entire Automobile object' do + auto.update(model: 'Explorer', color: 'Silver', make: 'Ford', year: 1998) + auto.model.should eql 'Explorer' + auto.color.should eql 'Silver' + auto.make.should eql 'Ford' + auto.year.should eql 1998 + end + end + end + + describe "#build" do + it "records the auto when I build it" do + auto = Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) + expect(Automobile.all_autos).to include (auto) + expect(Vehicle.all_vehicles).to include (auto) + end + + it "should increment the Vehicle count" do + expect{Automobile.build model: :honda, color: :blue, make: 'Civic', year: 2013}.to change{Vehicle.count}.from(1).to(2) + end + end +end + + diff --git a/spec/motorcycle_spec.rb b/spec/motorcycle_spec.rb new file mode 100644 index 0000000..c12aec5 --- /dev/null +++ b/spec/motorcycle_spec.rb @@ -0,0 +1,22 @@ +require 'rspec' +require '../models/eagle' + +describe Motorcycle do + let (:bike) {Motorcycle.new} + + describe '.wheels' do + it 'should return two' do + Motorcycle.wheels.should eq(2) + end + end + + describe "#new" do + it "takes no parameters and returns a Motorcycle object" do + bike.should be_an_instance_of Motorcycle + end + + it 'should inherit from vehicle' do + bike.is_a?(Vehicle).should be_true + end + end +end diff --git a/spec/scenarios.txt b/spec/scenarios.txt new file mode 100644 index 0000000..aabe25b --- /dev/null +++ b/spec/scenarios.txt @@ -0,0 +1,34 @@ +#Vehicle scenarios +Vehicle.wheels +8.times { Vehicle.new } and Vehicle.count == 8 +Vehicle.search(color: "blue", model: "honda").inspect + + +#Automobilce scenarios +Automobile.is_a?(Automobile) +Automobile.wheels == 4 +auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) +auto.color == 'Red' +auto.update(color: 'Blue', year: 2012) +auto.color == 'Blue' +auto.make = 'Ford' +auto.year == 2012 + +Automobile.new model: :mazda, color: :red, make: 'Civic', year: 2012 +Automobile.new model: :toyota, color: :red, make: 'Corolla', year: 2010 +Automobile.new model: :honda, color: :blue, make: 'Civic', year: 2013 +Automobile.new model: :mazda, color: :blue, make: 'Accord', year: 2003 +Vehicle.count == 5 #=> after all the above tests have run + +Automobile.count #=> 0 +Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) +Automobile.count #=> 1 + +#Motorcycle +Motorcycle.wheels +4.times { Motorcycle.new } and Vehicle.count == 4 +Motorcycle.is_a?(Motorcycle) + + + + diff --git a/spec/vehicle_spec.rb b/spec/vehicle_spec.rb new file mode 100644 index 0000000..e8e140d --- /dev/null +++ b/spec/vehicle_spec.rb @@ -0,0 +1,35 @@ +require 'rspec' +require '../models/eagle' + +describe Vehicle do + + let(:vehicle) { Vehicle.new } + + describe '.wheels' do + it 'should return two' do + Vehicle.wheels.should eq(2) + end + end + + describe "#new" do + it "takes no parameters and returns a Vehicle object" do + vehicle.should be_an_instance_of Vehicle + end + end + + describe "#search" do + it "should find an Automobile when it is searched for" do + Automobile.build model: :mazda, color: :red, make: 'Civic', year: 2012 + Automobile.build model: :toyota, color: :red, make: 'Corolla', year: 2010 + auto = Automobile.build model: :honda, color: :blue, make: 'Civic', year: 2013 + Automobile.build model: :mazda, color: :blue, make: 'Accord', year: 2003 + Vehicle.search(color: "blue", model: "honda").first.should eq(auto) + end + end + + describe "#count" do + it "should not increment the Vehicle count no matter how many vehicles are created but not built" do + expect{8.times { Vehicle.new }}.to_not change{Vehicle.count}.from(0).to(0) + end + end +end