Skip to content

Instantly share code, notes, and snippets.

@rskelley9
Last active December 22, 2015 09:49
Show Gist options
  • Save rskelley9/6454281 to your computer and use it in GitHub Desktop.
Save rskelley9/6454281 to your computer and use it in GitHub Desktop.
Working Bernies Bistro that I fixed. Ryan.fail!
require 'csv'
class Recipe
attr_reader :id, :name, :description, :ingredients, :directions
def initialize(id, name, description, ingredients, directions)
@id = id
@name = name
@description = description
@ingredients = ingredients
@directions = directions
end
def to_a
[self.id, self.name, self.description, self.ingredients, self.directions]
end
def to_a_and_sort!
lower_recipes = [self.id, self.name, self.description, self.ingredients, self.directions].downcase
lower_recipes.sort
end
end
def to_s
"#{self.id}, #{self.name}, #{self.description}, #{self.ingredients}, #{self.directions}"
end
end
class Bistro
attr_accessor :file, :recipes
def initialize(file)
@file = file
@recipes = []
end
def load_recipes
return @recipes if @recipes.any?
CSV.foreach(file, { :headers => true }) do |row|
@recipes << Recipe.new(row)
end
end
def add_recipe(new_recipe)
@recipes << new_recipe
end
def save_and_write_on_recipes_file
CSV.open(file, 'w+') do |csv|
csv << ['id','name','description','ingredients','directions']
@recipes.each do |recipe|
csv << recipe.to_a
end
end
end
def sort!
@recipes.sort! {|a,b| a.name.downcase <=> b.name.downcase}
end
def print_recipes
@recipes.each do |recipe|
p recipe
end
end
def find_by_id(id)
@recipes.each do |recipe|
if id.to_s == recipe.id
p recipe
end
end
end
end
file = 'recipes.csv'
bernies_bistro = Bistro.new('recipes.csv')
bernies = bernies_bistro.load_recipes
# bernies_bistro.find_by_id(2)
# bernies_bistro.print_recipes
bernies_bistro.sort!
id name description ingredients directions
1 Kale Burger Combining the taste of cow with the nutrition of kale! Kale, Cow Preheat oven to 500 degrees, put in a cow. Wait 10 minutes. Put in some kale. Take out cow and kale. Put on bun. Serve HOT!
2 Poodle Cake (For your puppy!) Poople cakes! Perfect for pet poodle! Kibble, Hot dogs Mix kibble with hot dog. Bake at 500 degrees for 25 minutes. DO NOT ADD CHOCOLATE
3 Peanut Butter Coffee Brownie Just looking has caused people to gain 10 pounds! Chocolate, Peanut Butter, Espresso, Cream Cheese Chew the espresso beans to a pulp, spit them into the bowl. Now mix in the chocolate, cream cheese, and peanut butter. Bake at 450 for 45 minutes
require_relative('bistro')
bernies_bistro = Bistro.new(file)
bernies_bistro.load_recipes
# bernies_bistro.print_recipes
# bernies_bistro.sort!
# bernies_bistro.print_recipes
if ARGV.any?
puts "Hello, sir/madam, welcome to Bernie's Bistro. In terms of recipes, what would you like to do?"
if ARGV[0].downcase.to_s == "print"
bernies_bistro.print_recipes
elsif ARGV[0].downcase.to_s == "find"
bernies_bistro.find_recipe_by_id(ARGV[1])
elsif ARGV[0].downcase.to_s == "add"
bernies_bistro.add_new_recipe(ARGV[1])
elsif ARGV[0].downcase.to_s == "sort!"
else
puts "You entered #{ARGV[0]} which just doesn' make a whole lot of sense to me right now."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment