Skip to content
Closed
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
7 changes: 3 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
source 'http://rubygems.org'

gem 'rake'
gem 'rspec'
gem 'activesupport'
gem 'activerecord'
gem 'pg'
gem 'minitest'
gem 'mynyml-redgreen'
gem 'fivemat'
38 changes: 8 additions & 30 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.3)
activesupport (= 3.2.3)
builder (~> 3.0.0)
activerecord (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
diff-lcs (1.1.3)
i18n (0.6.0)
multi_json (1.3.4)
pg (0.13.2)
fivemat (1.0.0)
minitest (3.0.0)
mynyml-redgreen (0.7.1)
term-ansicolor (>= 1.0.4)
rake (0.9.2.2)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
rspec-core (2.10.0)
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
tzinfo (0.3.33)
term-ansicolor (1.0.7)

PLATFORMS
ruby

DEPENDENCIES
activerecord
activesupport
pg
fivemat
minitest
mynyml-redgreen
rake
rspec
6 changes: 0 additions & 6 deletions config/database.yml.sample

This file was deleted.

1 change: 0 additions & 1 deletion db/seed.rb

This file was deleted.

15 changes: 0 additions & 15 deletions db/setup.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/ep6.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative 'ep6/vehicle'
require_relative 'ep6/automobile'
require_relative 'ep6/motorcycle'
32 changes: 32 additions & 0 deletions lib/ep6/automobile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Automobile < Vehicle
attr_accessor :color
attr_reader :make, :model, :year

def initialize(features = {})
defaults = {
:color => 'black',
:make => 'ford',
:model => 'taurus',
:year => '2012'
}
features = defaults.merge(features)

@color = features[:color]
@make = features[:make]
@model = features[:model]
@year = features[:year]

# Now add the new instance to the @all_vehicles array via Vehicle's
# initialize method.
super()
end

def ==(other)
if @color == other.color && @make == other.make &&
@model == other.model && @year == other.year
return true
else
return false
end
end
end
11 changes: 11 additions & 0 deletions lib/ep6/motorcycle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Motorcycle < Vehicle
def self.wheels
2
end

def initialize
# Add the new instance to the @all_vehicles array via Vehicle's
# initialize method.
super()
end
end
24 changes: 24 additions & 0 deletions lib/ep6/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Vehicle
extend Enumerable

@all_vehicles = []

# create read/write methods for @all_vehicles
class << self
attr_accessor :all_vehicles
end

def initialize
Vehicle.all_vehicles.push self
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this

end

def self.wheels
4
end

def self.each
@all_vehicles.each do |vehicle|
yield vehicle
end
end
end
Empty file removed models/.gitkeep
Empty file.
16 changes: 7 additions & 9 deletions rakefile.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require "rubygems"
require "bundler/setup"
require 'bundler/setup'

require 'rspec/core/rake_task'
require 'rake/testtask'

desc 'Default: run specs.'
task :default => :spec
desc 'Default: run tests.'
task :default => :test

desc "Run specs"
RSpec::Core::RakeTask.new do |t|
t.pattern = "**/*_spec.rb" # don't need this, it's default.
# Put spec opts in a file named .rspec in root
desc "Run tests"
Rake::TestTask.new do |t|
t.pattern = 'test/test_*.rb'
end
Empty file removed spec/.gitkeep
Empty file.
75 changes: 75 additions & 0 deletions test/test_automobile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require_relative 'test_helper'

describe 'Automobile class' do
it 'should exist' do
Automobile.must_be_kind_of Class
end

it 'should inherit from Vehicle' do
Automobile.superclass.must_equal Vehicle
end

it 'should have a class method that returns the number of wheels' do
Automobile.must_respond_to :wheels
Automobile.wheels.must_equal 4
end

it 'should have default features when initialized with an empty hash' do
car = Automobile.new({})

car.color.must_equal 'black'
car.make.must_equal 'ford'
car.model.must_equal 'taurus'
car.year.must_equal '2012'
end

it 'should override defaults when given various values' do
white_car = Automobile.new({:color => 'white'})
mercedes = Automobile.new({:make => 'mercedes'})
charger = Automobile.new({:model => 'charger'})
classic = Automobile.new({:year => '1968'})

white_car.color.must_equal 'white'
mercedes.make.must_equal 'mercedes'
charger.model.must_equal 'charger'
classic.year.must_equal '1968'
end

it 'should accept multiple values in any order' do
old_charger = Automobile.new({:year => '1968', :model => 'charger'})
new_mercedes = Automobile.new({
:model => 'SE',
:make => 'mercedes',
:color => 'red',
:year => '2010'
})

old_charger.model.must_equal 'charger'
old_charger.year.must_equal '1968'

new_mercedes.color.must_equal 'red'
new_mercedes.year.must_equal '2010'
end

it 'should be able to change its color' do
car = Automobile.new
car.color.must_equal 'black'

car.color = 'white'
car.color.must_equal 'white'
end

it 'should be able to compare two cars' do
chevy = Automobile.new({
:color => 'red',
:make => 'chevy',
:model => 'impala',
:year => 1972
})
ford = Automobile.new
chevy_copy = chevy.dup

chevy.wont_equal ford
chevy.must_equal chevy_copy
end
end
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gem 'minitest'
require 'minitest/spec'
require 'fivemat/minitest/autorun'
require 'redgreen'
require_relative '../lib/ep6'
16 changes: 16 additions & 0 deletions test/test_motorcycle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require_relative 'test_helper'

describe 'Motorcycle class' do
it 'should exist' do
Motorcycle.must_be_kind_of Class
end

it 'should inherit from Vehicle' do
Motorcycle.superclass.must_equal Vehicle
end

it 'should have a class method that returns the number of wheels' do
Motorcycle.must_respond_to :wheels
Motorcycle.wheels.must_equal 2
end
end
9 changes: 9 additions & 0 deletions test/test_sanity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe 'sanity' do
it 'is minimally capable' do
true.must_be :==, true
end

it 'must know true from false' do
true.wont_be :==, false
end
end
34 changes: 34 additions & 0 deletions test/test_vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'test_helper'

describe 'Vehicle class' do
it 'should exist' do
Vehicle.must_be_kind_of Class
end

it 'should have a class method that returns the number of wheels' do
Vehicle.must_respond_to :wheels
Vehicle.wheels.must_equal 4
end

describe 'class instance variable and enumerability' do
before do
# I don't think this should be necessary, but it is. Why?
Vehicle.all_vehicles = []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's necessary because whenever we declare an attr_accessor, it defaults to nil

Here's an example that shows a way to solve this... Probably a way using meta-programming to handle inside the class definition though: http://rubyfiddle.com/riddles/b2334

end

it 'should store all the vehicles as they are made' do
car = Automobile.new
motorcycle = Motorcycle.new
vehicle = Vehicle.new

Vehicle.all_vehicles.count.must_equal 3
end

it 'should include enumerable methods' do
Vehicle.must_respond_to :each
Vehicle.must_respond_to :select
Vehicle.must_respond_to :map
Vehicle.must_respond_to :partition
end
end
end