From afcc0fee4023974c4d38db48f319bc14b5a21638 Mon Sep 17 00:00:00 2001 From: PDaily Date: Sat, 5 Apr 2014 21:25:49 +0000 Subject: [PATCH 1/2] Added Vehicle, Automobile & Motorcycle classes with required options --- models/auto.rb | 38 ++++++++++++++++++++++++++++++++++++++ spec/auto_spec.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 models/auto.rb create mode 100644 spec/auto_spec.rb diff --git a/models/auto.rb b/models/auto.rb new file mode 100644 index 0000000..de9be66 --- /dev/null +++ b/models/auto.rb @@ -0,0 +1,38 @@ +class Vehicle + attr_accessor :color, :make, :model, :year, :wheel +## +# Initialize new vehicle; if no arguments are provided, default to "None" + def initialize(args) + @color = args[:color] ||= "None" + @make = args[:make] ||= "None" + @model = args[:model] ||= "None" + @year = args[:year] ||= "None" + end +## +# Such wheels, so round +# May have been overzealous +# +# Could have done: +# def number_of_wheels +# 4 +# end +# +# But wanted to mess with arguments +## + def self.number_of_wheels(wheels) + @wheel = wheels + return @wheel + end +end + + +class Automobile < Vehicle + Automobile.number_of_wheels(4) +end + + +## +# Doesn't have a redefined method, but should still give us correct wheels. +class Motorcycle < Vehicle + Motorcycle.number_of_wheels(2) +end \ No newline at end of file diff --git a/spec/auto_spec.rb b/spec/auto_spec.rb new file mode 100644 index 0000000..ee99448 --- /dev/null +++ b/spec/auto_spec.rb @@ -0,0 +1,44 @@ +require './models/auto' +require 'rspec' + +describe Automobile do + it "should have a color" do + auto = Automobile.new(color: "Red") + puts auto.inspect + end + it "should have a make" do + auto = Automobile.new(make: "Ford") + puts auto.inspect + end + it "should have a model" do + auto = Automobile.new(model: "Fiesta") + puts auto.inspect + end + it "should have a year made" do + auto = Automobile.new(year: 2014) + puts auto.inspect + end + it "should be able to take all arguments at once" do + auto = Automobile.new(color: "Red", make: "Ford", model: "Fiesta", year: 2014) + puts auto.inspect + end + it "should have 4 the wheels" do + Automobile.number_of_wheels(4).should eq(4) + puts "The automobile has 4 wheels!" + end + it "should not have 300 wheels" do + Automobile.number_of_wheels(300).should_not eq(4) + puts "The automobile doesn't have 300 wheels!" + end +end + +describe Motorcycle do + it "should have 2 wheels" do + Motorcycle.number_of_wheels(2).should eq(2) + puts "The motorcycle has 2 wheels!" + end + it "should not have 4 wheels" do + Motorcycle.number_of_wheels(4).should_not eq(2) + puts "The motorcycle doesn't have 4 wheels!" + end +end \ No newline at end of file From d85bb98dde284c3cfb6552ddff889109cbff6493 Mon Sep 17 00:00:00 2001 From: PDaily Date: Mon, 7 Apr 2014 03:47:04 +0000 Subject: [PATCH 2/2] updated rspec after some research, added a proper update method --- models/auto.rb | 8 ++++++++ spec/auto_spec.rb | 32 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/models/auto.rb b/models/auto.rb index de9be66..ab8bbfb 100644 --- a/models/auto.rb +++ b/models/auto.rb @@ -8,6 +8,14 @@ def initialize(args) @model = args[:model] ||= "None" @year = args[:year] ||= "None" end +## +# If I want to update the Vehicle, so shall it be + def updater(args) + @color = args.fetch(:color) if args[:color] + @make = args.fetch(:make) if args[:make] + @model = args.fetch(:model) if args[:model] + @year = args.fetch(:year).to_s if args[:year] + end ## # Such wheels, so round # May have been overzealous diff --git a/spec/auto_spec.rb b/spec/auto_spec.rb index ee99448..3990b0e 100644 --- a/spec/auto_spec.rb +++ b/spec/auto_spec.rb @@ -2,43 +2,43 @@ require 'rspec' describe Automobile do + + let(:auto) {Automobile.new(color: "Red",make: "Ford",model: "Fiesta",year: "2014")} + +## +# Passing tests it "should have a color" do - auto = Automobile.new(color: "Red") - puts auto.inspect + auto.color.should eq("Red") end it "should have a make" do - auto = Automobile.new(make: "Ford") - puts auto.inspect + auto.make.should eq("Ford") end it "should have a model" do - auto = Automobile.new(model: "Fiesta") - puts auto.inspect + auto.model.should eq("Fiesta") end it "should have a year made" do - auto = Automobile.new(year: 2014) - puts auto.inspect + auto.year.should eq("2014") end - it "should be able to take all arguments at once" do - auto = Automobile.new(color: "Red", make: "Ford", model: "Fiesta", year: 2014) - puts auto.inspect + it "should be able to update via hash" do + auto.updater(color: "Blue",make: "Chevy", model: "Nova SS", year: "1968") + auto.color.should eq("Blue") + auto.make.should eq("Chevy") + auto.model.should eq("Nova SS") + auto.year.should eq("1968") end it "should have 4 the wheels" do Automobile.number_of_wheels(4).should eq(4) - puts "The automobile has 4 wheels!" end it "should not have 300 wheels" do - Automobile.number_of_wheels(300).should_not eq(4) - puts "The automobile doesn't have 300 wheels!" + Automobile.number_of_wheels(300).should_not eq(4) end end describe Motorcycle do it "should have 2 wheels" do Motorcycle.number_of_wheels(2).should eq(2) - puts "The motorcycle has 2 wheels!" end it "should not have 4 wheels" do Motorcycle.number_of_wheels(4).should_not eq(2) - puts "The motorcycle doesn't have 4 wheels!" end end \ No newline at end of file