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
62 changes: 35 additions & 27 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
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)
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)
activemodel (4.0.2)
activesupport (= 4.0.2)
builder (~> 3.1.0)
activerecord (4.0.2)
activemodel (= 4.0.2)
activerecord-deprecated_finders (~> 1.0.2)
activesupport (= 4.0.2)
arel (~> 4.0.0)
activerecord-deprecated_finders (1.0.3)
activesupport (4.0.2)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
arel (4.0.1)
atomic (1.1.14)
builder (3.1.4)
diff-lcs (1.2.5)
i18n (0.6.9)
minitest (4.7.5)
multi_json (1.8.4)
pg (0.17.1)
rake (10.1.1)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
thread_safe (0.1.3)
atomic
tzinfo (0.3.38)

PLATFORMS
ruby
Expand Down
5 changes: 5 additions & 0 deletions models/automobile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative 'vehicle'

class Automobile < Vehicle

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

class Motorcycle < Vehicle
def self.num_wheels
num_wheels = 2
Copy link
Member

Choose a reason for hiding this comment

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

this is the same as

def self.num_wheels
  2
end

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

attr_reader :color, :make, :model, :year, :name

@@all_vehicles = []

def initialize(args={})
@color = args.fetch(:color) if args[:color]
Copy link
Member

Choose a reason for hiding this comment

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

if you're going to use if args[:color], that means it's ok if @color is nil.

If that's the case, this syntax is preferred:

@color = args[:color]

If you want a default value, you can do this:

@color = args.fetch(:color, "red")

@make = args.fetch(:make) if args[:make]
@model = args.fetch(:model) if args[:model]
@year = args.fetch(:year).to_s if args[:year]
@name = args.fetch(:name) if args[:name]
args[:year] = @year.to_s
@@all_vehicles << args
end

def self.all_vehicles
@@all_vehicles
end

def self.num_wheels
num_wheels = 4
end

def self.filter(args ={})
search_str = args.to_a.map { |group| "vehicle[:#{group[0]}] == '#{group[1]}' and "}.reduce(:+)[0..-5]
@@all_vehicles.find_all { |vehicle| eval(search_str) }
end

def update_all_vehicles(name)
Copy link
Member

Choose a reason for hiding this comment

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

Something that can update all instances of a class should be on the class itself. That is, the method should be "scoped" to the object -- it should have the same powers as the object itself.

tl;dr

If it can update more than 1 thing:

def self.update_all_vehicles(name)

index = @@all_vehicles.index {|vehicle| vehicle[:name] == name}
Copy link
Member

Choose a reason for hiding this comment

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

so, instead of using "index", in ruby you have the "find" method, and then update it in place.

  Vehicle.all_vehicles.find{|v| v.name == name}.update(args)

Example usage: http://rubyfiddle.com/riddles/37f83

@@all_vehicles[index] = { name: @name, color: @color, make: @make, model: @model, year: @year}
end

def all_vehicles
@@all_vehicles
end

def update(name, 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]
update_all_vehicles(name)
Copy link
Member

Choose a reason for hiding this comment

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

This went a bit beyond the spec --- it makes sense to do this, but it wasn't required of you.

Mostly, if I have a vehicle, I want to be able to update it.

class Vehicle
  def update(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]
    self
  end
end

end
end
23 changes: 23 additions & 0 deletions spec/automobiles_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative 'spec_helper'

describe Automobile do

it "should return the number of wheels" do
expect(Automobile.num_wheels).to eq(4)
end

it "should have and set attributes: color, make, model, and year" do
car = Automobile.new(name: 'bill', color: "blue", make: "Toyota", model: "Prius", year: 2008 )
expect(car.color).to eq('blue')
expect(car.make).to eq('Toyota')
expect(car.model).to eq('Prius')
expect(car.year).to eq('2008')
end

it "should allow you to change attributes" do
car = Automobile.new(name: 'jerry', color: "blue", make: "Toyota", model: "Prius", year: 2008 )
expect(car.color).to eq("blue")
car.update('jerry', color: "yellow")
expect(car.color).to eq("yellow")
end
end
7 changes: 7 additions & 0 deletions spec/motorcycle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative 'spec_helper'

describe Motorcycle do
it "should return the number of wheels" do
expect(Motorcycle.num_wheels).to eq(2)
end
end
19 changes: 19 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rspec'
require 'bundler/setup'
require_relative '../db/setup'

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



RSpec.configure do |config|
# Use color in STDOUT
config.color_enabled = true

# Use color not only in STDOUT but also in pagers and files
config.tty = true

# Use the specified formatter
config.formatter = :documentation # :progress, :html, :textmate
end

40 changes: 40 additions & 0 deletions spec/vehicles_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require_relative 'spec_helper'

describe Vehicle do

it "should add to @all_vehicles" do
Automobile.new(name: 'new', color: 'purple', make: 'Toyota', model: 'Prius', year: 2008)
index = Automobile.all_vehicles.index {|vehicle| vehicle[:name] == 'new' }
expect(Automobile.all_vehicles[index]).to eq({name: 'new', :color => 'purple', :make => 'Toyota', :model => 'Prius', :year => "2008"})
end

describe "#filter" do
it "should filter on blue honda accords" do
Automobile.new(name: 'new3', color: 'blue', make: 'Honda', model: 'Accord', year: 2008)
Automobile.new(name: 'new4', color: 'white', make: 'Honda', model: 'Accord', year: 2008)
Automobile.new(name: 'new5', color: 'blue', make: 'Honda', model: 'Accord', year: 2008)
Automobile.new(name: 'new6', color: 'purple', make: 'Toyota', model: 'Prius', year: 2008)
expect(Automobile.filter(color: 'blue', make: 'Honda', model: 'Accord')).to eq([{name: 'new3', color: 'blue', make: 'Honda', model: 'Accord', year: "2008"}, {name: 'new5', color: 'blue', make: 'Honda', model: 'Accord', year: "2008"}])
end
end


describe "#update" do
it "should update @@all_vehicles" do
car = Automobile.new(name: 'new2', color: 'purple', make: 'Toyota', model: 'Prius', year: 2008)
car.update('new2', year: 2009)
index = car.all_vehicles.index {|vehicle| vehicle[:name] == 'new2' }
expect(car.all_vehicles[index][:year]).to eq('2009')
end
end

it "should return the number of wheels" do
expect(Vehicle.num_wheels).to eq(4)
end

it "should keep track of the vehicles created" do
Automobile.new(name: 'john', color: 'yellow', make: 'Toyota', model: 'Prius', year: 2008)
Motorcycle.new(name: 'ben', color: 'red', make: 'honda', model: 'fast', year: 2014)
expect(Automobile.all_vehicles.length).to eq(2)
end
end