diff --git a/Gemfile.lock b/Gemfile.lock index 0facad0..e885584 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/models/automobile.rb b/models/automobile.rb new file mode 100644 index 0000000..52a0e65 --- /dev/null +++ b/models/automobile.rb @@ -0,0 +1,5 @@ +require_relative 'vehicle' + +class Automobile < Vehicle + +end \ No newline at end of file diff --git a/models/motorcycle.rb b/models/motorcycle.rb new file mode 100644 index 0000000..82329fe --- /dev/null +++ b/models/motorcycle.rb @@ -0,0 +1,7 @@ +require_relative 'vehicle' + +class Motorcycle < Vehicle + def self.num_wheels + num_wheels = 2 + end +end \ No newline at end of file diff --git a/models/vehicle.rb b/models/vehicle.rb new file mode 100644 index 0000000..c66205d --- /dev/null +++ b/models/vehicle.rb @@ -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] + @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) + index = @@all_vehicles.index {|vehicle| vehicle[:name] == name} + @@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) + end +end \ No newline at end of file diff --git a/spec/automobiles_spec.rb b/spec/automobiles_spec.rb new file mode 100644 index 0000000..347de07 --- /dev/null +++ b/spec/automobiles_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/motorcycle_spec.rb b/spec/motorcycle_spec.rb new file mode 100644 index 0000000..600b337 --- /dev/null +++ b/spec/motorcycle_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..35d8f48 --- /dev/null +++ b/spec/spec_helper.rb @@ -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 + diff --git a/spec/vehicles_spec.rb b/spec/vehicles_spec.rb new file mode 100644 index 0000000..cebfb27 --- /dev/null +++ b/spec/vehicles_spec.rb @@ -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