From 57fefe03c90717f152f9c7d2ba4c43a02cc4007d Mon Sep 17 00:00:00 2001 From: Ryan Moser Date: Wed, 19 Dec 2012 02:07:39 -0500 Subject: [PATCH] Eagle-ish Solutions --- db/migrate/001_create_vehicles.rb | 11 ++++++ models/automobile.rb | 32 ++++++++++++++++ spec/automobile_spec.rb | 63 +++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 db/migrate/001_create_vehicles.rb create mode 100644 models/automobile.rb create mode 100644 spec/automobile_spec.rb diff --git a/db/migrate/001_create_vehicles.rb b/db/migrate/001_create_vehicles.rb new file mode 100644 index 0000000..7104389 --- /dev/null +++ b/db/migrate/001_create_vehicles.rb @@ -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 \ No newline at end of file diff --git a/models/automobile.rb b/models/automobile.rb new file mode 100644 index 0000000..75909f4 --- /dev/null +++ b/models/automobile.rb @@ -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"} + 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 \ No newline at end of file diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb new file mode 100644 index 0000000..0cfad65 --- /dev/null +++ b/spec/automobile_spec.rb @@ -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 + + +