-
Notifications
You must be signed in to change notification settings - Fork 22
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
Panda, Tiger & Eagle levels complete #9
Open
ralphos
wants to merge
7
commits into
RubyoffRails:master
Choose a base branch
from
ralphos:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33f68e0
Panda Level: Completed using TDD and applied Thoughtbot's style guide…
ralphos 0077961
Tiger Level: Completed
ralphos 0370eff
Eagle Level: Completed
ralphos cb8ac29
refactoring to move vehicle counter to Vehicle class using inheritanc…
ralphos 57a9a5e
Added a VehicleCollector class to deal with collecting vehicles and s…
ralphos c64dde1
Played some more: refactored to make VehicleCollector a module instea…
ralphos f0ba604
Renamed method and removed old files
ralphos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
require 'rubygems' | ||
require 'bundler/setup' | ||
|
||
require_relative 'db/setup' | ||
Dir.glob("./**/*.rb").each {|f| require f} | ||
#require_relative 'db/setup' | ||
Dir.glob("./models/*.rb").each {|f| require f} | ||
|
||
puts "Serenity now!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module VehicleCollector | ||
|
||
@@vehicles = [] | ||
|
||
def self.show_vehicle_names | ||
@@vehicles.map { |vehicle| vehicle.name } | ||
end | ||
|
||
def self.add_vehicles(vehicle) | ||
@@vehicles << vehicle | ||
end | ||
|
||
def self.get | ||
@@vehicles | ||
end | ||
|
||
def self.reset | ||
@@vehicles.clear | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require_relative 'vehicle' | ||
class Automobile < Vehicle | ||
|
||
attr_reader :color, :make, :model, :year, :type | ||
|
||
def initialize(options = {}) | ||
@color = options[:color] | ||
@make = options[:make] | ||
@model = options[:model] | ||
@year = options[:year] | ||
@type = Vehicle.new(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require_relative 'vehicle' | ||
class Motorcycle < Vehicle | ||
|
||
attr_reader :name, :type | ||
|
||
def initialize(options = {}) | ||
@name = options[:name] | ||
@type = Vehicle.new(self) | ||
end | ||
|
||
def self.wheels | ||
2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require './lib/vehicle_collector' | ||
class Vehicle | ||
include VehicleCollector | ||
|
||
def initialize(vehicle) | ||
VehicleCollector.add_vehicles(vehicle) | ||
end | ||
|
||
def self.blue_honda_accords | ||
automobiles = @@vehicles.select { |vehicle| vehicle.class == Automobile } | ||
automobiles.keep_if do |automobile| | ||
automobile.color == 'blue' && | ||
automobile.make == 'Honda' && | ||
automobile.model == 'Accord' | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We talked about this code in office hours... just an alternative way:
|
||
|
||
def self.wheels | ||
4 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'spec_helper' | ||
|
||
describe VehicleCollector, '.show_vehicle_names' do | ||
it 'outputs all collected vehicles' do | ||
VehicleCollector.reset | ||
motorcycle = Motorcycle.new(name: 'Harley Davidson') | ||
another_motorcycle = Motorcycle.new(name: 'Honda') | ||
|
||
expect(VehicleCollector.show_vehicle_names).to eq ['Harley Davidson', 'Honda'] | ||
end | ||
end | ||
|
||
describe VehicleCollector, '.add_vehicles' do | ||
it 'adds vehicles to class variable' do | ||
motorcycle = Motorcycle.new(name: 'Harley Davidson') | ||
# Reset here since add_vehicles is called in initialize | ||
VehicleCollector.reset | ||
expect(VehicleCollector.add_vehicles(motorcycle).length).to eq 1 | ||
end | ||
end | ||
|
||
describe VehicleCollector, '.get' do | ||
it 'returns @@vehicles' do | ||
VehicleCollector.reset | ||
car = Automobile.new | ||
|
||
expect(VehicleCollector.get.first).to eq car | ||
end | ||
end | ||
|
||
describe VehicleCollector, '.reset' do | ||
it 'clears out @@vehicles' do | ||
motorcycle = Motorcycle.new(name: 'Harley Davidson') | ||
VehicleCollector.reset | ||
|
||
expect(VehicleCollector.get).to be_empty | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper' | ||
|
||
describe Automobile, '.initialize' do | ||
it 'can receive a hash containing color, make, model and year to update its variables' do | ||
@@vehicles = [] | ||
car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) | ||
|
||
expect(car.color).to eq 'blue' | ||
expect(car.make).to eq 'BMW' | ||
expect(car.model).to eq '325i' | ||
expect(car.year).to eq 1995 | ||
expect(car.type).to be_instance_of Vehicle | ||
end | ||
end | ||
|
||
describe Automobile, '.wheels' do | ||
it 'returns the number of wheels' do | ||
Automobile.wheels.should eq 4 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper' | ||
|
||
describe Motorcycle, '.initialize' do | ||
it 'has a name and type when instantiated' do | ||
@@vehicles = [] | ||
motorcycle = Motorcycle.new(name: 'Harley Davidson') | ||
|
||
expect(motorcycle.type).to be_instance_of Vehicle | ||
expect(motorcycle.name).to eq 'Harley Davidson' | ||
end | ||
end | ||
|
||
describe Motorcycle, '.wheels' do | ||
it 'overrides Vehicle and returns 2 wheels' do | ||
expect(Motorcycle.wheels).to eq 2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'spec_helper' | ||
|
||
describe Vehicle, '.blue_honda_accords' do | ||
it 'returns only blue honda accords' do | ||
car = Automobile.new(color: 'blue', make: 'Honda', model: 'Accord', year: 1995) | ||
another_car = Automobile.new(color: 'blue', make: 'BMW', model: '325i', year: 1995) | ||
motorcycle = Motorcycle.new | ||
|
||
blue_hondas = Vehicle.blue_honda_accords | ||
|
||
expect(blue_hondas).to eq [car] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'rspec' | ||
require 'bundler/setup' | ||
require_relative '../models/automobile' | ||
require_relative '../models/vehicle' | ||
require_relative '../models/motorcycle' | ||
require_relative '../lib/vehicle_collector' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you reach out to
VehicleCollector
-- you're actually not using the module you just included at all. Instead, you're reaching out to the main VehicleCollector module and using its methods.it's a bit confusing, but you'll likely want to either not include the VehicleCollector at all, and just use VehicleCollector as a "singleton" module, or you'll want to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So correct me if I'm wrong, but the intention of creating a "singleton" module would be to only create one instance of VehicleCollector which would be globally available? That way you know you're referencing the one and only VehicleCollector? How would create a "singleton" module? Would it be on the module itself or on the Vehicle class?
I tried implementing the code you provided above but I also couldn't get it to work. Specifically, I'm not sure what self is referencing in this context?
What do you mean I'm reaching out to the main VehicleCollector module?
Sorry, quite a few questions as I'm not sure what's going on!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, a singleton class would mean there is just one global collection. Example of a singleton type collector:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're doing the equivalent of "Collection.add_to_collection :the_item" in the above example.