Skip to content

Instantly share code, notes, and snippets.

@noelrappin
Created August 11, 2011 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noelrappin/1140408 to your computer and use it in GitHub Desktop.
Save noelrappin/1140408 to your computer and use it in GitHub Desktop.
class Restaurant
attr_accessor :ratings, :cuisine, :price
def initialize(cuisine = nil, price = 0)
@ratings = []
@cuisine = cuisine
@price = price
end
def cuisine_match?(requested_cuisine)
return true unless requested_cuisine
cuisine == requested_cuisine
end
def too_expensive?(price_range)
return false unless price_range
return false if price_range.include?(price)
return true if price > price_range.last
end
def too_cheap?(price_range)
return false unless price_range
return false if price_range.include?(price)
return true if price < price_range.first
end
def zero_score?(requested_cuisine, price_range)
ratings.empty? ||
!cuisine_match?(requested_cuisine) ||
too_expensive?(price_range)
end
def score(requested_cuisine = nil, price_range = nil)
return 0 if zero_score?(requested_cuisine, price_range)
result = ratings.inject(:+) / ratings.count
if too_cheap?(price_range)
result = result / 2.0
end
result
end
def add_rating(new_rating)
@ratings << new_rating
end
end
require "restaurant"
describe Restaurant do
it "gives a new restaurant a score of zero" do
restaurant = Restaurant.new
restaurant.score.should == 0
end
it "bases a score on a rating" do
restaurant = Restaurant.new
restaurant.add_rating(3)
restaurant.score.should == 3
end
it "bases a score on the average multiple ratings" do
restaurant = Restaurant.new
restaurant.add_rating(3)
restaurant.add_rating(5)
restaurant.score.should == 4
end
it "bases a score on requesed cuisine" do
restaurant = Restaurant.new(:mexican)
restaurant.add_rating(3)
restaurant.score(:mexican).should == 3
restaurant.score(:thai).should == 0
end
it "bases a score on price" do
restaurant = Restaurant.new(:mexican, 20)
restaurant.add_rating(3)
restaurant.score(
:mexican, (10 .. 30)).should == 3
restaurant.score(
:mexican, (0 .. 10)).should == 0
end
it "reduces score if cheaper than request" do
restaurant = Restaurant.new(:mexican, 20)
restaurant.add_rating(3)
restaurant.score(:mexican,
(30 .. 40)).should == 1.5
end
it "bases score on location" do
restaurant = Restaurant.new(:mexican, 20)
restaurant.zip = "60015"
restaurant.add_rating(3)
restaurant.score(:zip => "60015").should == 3
restaurant.score(:zip => "60091").should == 2
restaurant.score(:zip => "50023").should == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment