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

Panda, Tiger & Eagle levels complete #9

Open
wants to merge 7 commits 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
38 changes: 19 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.3)
activesupport (= 3.2.3)
activemodel (3.2.9)
activesupport (= 3.2.9)
builder (~> 3.0.0)
activerecord (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
activerecord (3.2.9)
activemodel (= 3.2.9)
activesupport (= 3.2.9)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.3)
activesupport (3.2.9)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
builder (3.0.4)
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)
i18n (0.6.1)
multi_json (1.5.0)
pg (0.14.1)
rake (10.0.3)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
tzinfo (0.3.33)
rspec-mocks (2.12.0)
tzinfo (0.3.35)

PLATFORMS
ruby
Expand Down
4 changes: 2 additions & 2 deletions block_review.rb
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!"
20 changes: 20 additions & 0 deletions lib/vehicle_collector.rb
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
13 changes: 13 additions & 0 deletions models/automobile.rb
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
14 changes: 14 additions & 0 deletions models/motorcycle.rb
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
21 changes: 21 additions & 0 deletions models/vehicle.rb
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)
Copy link
Member

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:

def initialize
  self.class.add_vehicles(vehicle)
end

Copy link
Author

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!

Copy link
Member

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:

module Collection
  @@items = []
  def self.add_to_collection(item)
    @@items << item
  end

  def self.all_items
    @@items
  end
end

Collection.add_to_collection :the_item
Collection.all_items
=> [:the_item]

Copy link
Member

Choose a reason for hiding this comment

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

"What do you mean I'm reaching out to the main VehicleCollector module?"

You're doing the equivalent of "Collection.add_to_collection :the_item" in the above example.

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
Copy link
Member

Choose a reason for hiding this comment

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

We talked about this code in office hours... just an alternative way:

class Automobile
  def ==(other)
    color == other.color && make == other.make && model == other.model
  end
end

####
blue_honda_accord = Automobille.new( make: "Honda", model: "Accord", color: "blue" )
@@vehicles.select { |vehicle| vehicle.class == Automobile }.select {|a| a == blue_honda_accord }


def self.wheels
4
end
end
38 changes: 38 additions & 0 deletions spec/lib/vehicle_collector_spec.rb
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
20 changes: 20 additions & 0 deletions spec/models/automobile_spec.rb
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
17 changes: 17 additions & 0 deletions spec/models/motorcycle_spec.rb
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
13 changes: 13 additions & 0 deletions spec/models/vehicle_spec.rb
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
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
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'