Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GEM
i18n (0.6.0)
multi_json (1.3.4)
pg (0.13.2)
pg (0.13.2-x86-mingw32)
rake (0.9.2.2)
rspec (2.10.0)
rspec-core (~> 2.10.0)
Expand All @@ -31,6 +32,7 @@ GEM

PLATFORMS
ruby
x86-mingw32

DEPENDENCIES
activerecord
Expand Down
16 changes: 15 additions & 1 deletion block_review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
require 'bundler/setup'

require_relative 'db/setup'
Dir.glob("./**/*.rb").each {|f| require f}
Dir.glob("./models/*.rb").each {|f| require f}

auto1 = Automobile.new(color: "white", make: "Ford", model: "Explorer", year: "2000")
auto2 = Automobile.new(color: "blue", make: "Honda", model: "Accord", year: "2009")
auto3 = Automobile.new(color: "blue", make: "Honda", model: "Accord", year: "2011")

puts "An automobile has #{Automobile.number_of_wheels} tires."
puts "A motorcycle has #{Motorcycle.number_of_wheels} tires."
puts
puts "Auto 1: #{auto1.to_s}"
puts "Auto 2: #{auto2.to_s}"
puts "Auto 3: #{auto3.to_s}"
puts
puts "All vehicles: #{Vehicle.vehicles.inspect}"
puts "All blue Honda Accords: #{Vehicle.get_favorite_combo}"
puts
puts "Serenity now!"
6 changes: 0 additions & 6 deletions config/database.yml.sample

This file was deleted.

9 changes: 9 additions & 0 deletions models/automobile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative "Vehicle"

class Automobile < Vehicle

def self.number_of_wheels
@wheels = 4
end

end
9 changes: 9 additions & 0 deletions models/motorcycle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative "Vehicle"

class Motorcycle < Vehicle

def self.number_of_wheels
@wheels = 2
end

end
42 changes: 42 additions & 0 deletions models/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Vehicle

@@vehicles = []

def self.get_favorite_combo
veh = @@vehicles.select { |i| i.color == "blue" &&
i.make == "Honda" && i.model == "Accord" }
end

def self.reset
@@vehicles = []
end

def self.number_of_wheels
@wheels = nil
end

def self.vehicles
@@vehicles
end

attr_reader :color, :make, :model, :year
def initialize (hash={})
@@vehicles << self
@color = hash.fetch(:color)
@make = hash.fetch(:make)
@model = hash.fetch(:model)
@year = hash.fetch(:year)
end

def update (hash={})
@color = hash.fetch(:color)
@make = hash.fetch(:make)
@model = hash.fetch(:model)
@year = hash.fetch(:year)
end

def to_s
"#{year} #{color} #{make} #{model}"
end

end
10 changes: 10 additions & 0 deletions spec/automobile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "rspec"
require_relative "../models/automobile"

describe Automobile do

it "should have four wheels" do
Automobile.number_of_wheels.should eq(4)
end

end
10 changes: 10 additions & 0 deletions spec/motorcycle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "rspec"
require_relative "../models/motorcycle"

describe Motorcycle do

it "should have two wheels" do
Motorcycle.number_of_wheels.should eq(2)
end

end
52 changes: 52 additions & 0 deletions spec/vehicle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "rspec"
require_relative "../models/vehicle"

describe Vehicle do

before(:each) do
Vehicle.reset
end

it "should have four wheels" do
Vehicle.number_of_wheels.should eq(nil)
end

it "class should keep track of the vehicles made" do
Vehicle.vehicles.should_not be(nil)
Vehicle.vehicles.count.should eq(0)
veh = Vehicle.new(color: "white", make: "Honda", model: "Accord", year: "2002")
Vehicle.vehicles.count.should eq(1)
end


it "should take input to describe the automobile" do
auto = Vehicle.new(color: "black", make: "Honda", model: "Accord", year: "2002")
auto.color.should eq("black")
auto.make.should eq("Honda")
auto.model.should eq("Accord")
auto.year.should eq("2002")
end

it "should allow an update to the automobile" do
auto = Vehicle.new(color: "silver", make: "Toyota", model: "Camry", year: "2012")
auto.update(color: "red", make: "Honda", model: "Accord", year: "2002")
auto.color.should eq("red")
auto.make.should eq("Honda")
auto.model.should eq("Accord")
auto.year.should eq("2002")
end

it "should filter the vehicles list to only show blue Honda Accords" do
auto1 = Vehicle.new(color: "blue", make: "Honda", model: "Accord", year: "2002")
auto2 = Vehicle.new(color: "blue", make: "Honda", model: "Accord", year: "2005")
auto3 = Vehicle.new(color: "silver", make: "Toyota", model: "Camry", year: "2012")
results = Vehicle.get_favorite_combo
results.count.should eq(2)
end

it "should allow the class to be reset" do
auto = Vehicle.new(color: "silver", make: "Toyota", model: "Camry", year: "2012")
Vehicle.reset
Vehicle.vehicles.count.should eq(0)
end
end