Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Episode 6 Half-Eagle #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions db/migrate/001_create_vehicles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateVehicles < ActiveRecord::Migration
def change

create_table :vehicles do |t|
t.text :color
t.text :make
t.text :model
t.integer :year
end
end
end
32 changes: 32 additions & 0 deletions models/automobile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Vehicle < ActiveRecord::Base

def self.count
self.all.length
end

def self.count_blue_hondas
blue_hondas = self.all.count {|vehicle| vehicle.make == "Honda"; vehicle.color == "blue"; vehicle.model == "Accord"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is what you meant...

{|vehicle| vehicle.make == "Honda"; vehicle.color == "blue"; vehicle.model == "Accord"}

This will resolve to (assuming it is a blue Honda Accord)

[true; true; true]

But if it was a blue Toyota Accord, it would be:

[false; true; true]

I'll add some code to the main description on better way to do this

end

end


class Automobile < Vehicle

@number_of_wheels = 4

class << self
attr_reader :number_of_wheels
attr_reader :vehicles_made
end

end


class Motorcycle < Automobile
@number_of_wheels = 2

class << self
attr_reader :number_of_wheels
end
end
63 changes: 63 additions & 0 deletions spec/automobile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rspec'
require 'bundler/setup'
require_relative '../db/setup'
require_relative "../models/automobile"

describe Automobile do

let!(:car) {Automobile.new(color: "blue", make: "Honda", model: "S2000", year: 2001)}

it "should have color" do
car.color.should eq("blue")
end

it "should have a make" do
car.make.should eq("Honda")
end

it "should have a model" do
car.model.should eq("S2000")
end

it "should have a year" do
car.year.should eq(2001)
end

it "should be able to change attributes" do
car.color = "green"
car.color.should eq("green")
end

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

end


describe Motorcycle do

it "should override the number of wheels" do
Motorcycle.number_of_wheels.should eq(2)
end

end


describe Vehicle do
Vehicle.create(color: "gray", make: "USN", model: "Ballistic Sub", year: 2008)
Vehicle.create(color: "while", make: "NASA", model: "SATURN V", year: 1969)
Vehicle.create(color: "blue", make: "Honda", model: "CR-V", year: 1999)
Vehicle.create(color: "blue", make: "Honda", model: "Accord", year: 2009)

it "should return a count of vehicles" do
Vehicle.count.should eq(4)
end

it "should return a count of blue honda accords" do
Vehicle.count_blue_hondas.should eq(1)
end
end