Skip to content

Instantly share code, notes, and snippets.

@dammitBrandon
Created July 9, 2013 01:07
Show Gist options
  • Save dammitBrandon/5953843 to your computer and use it in GitHub Desktop.
Save dammitBrandon/5953843 to your computer and use it in GitHub Desktop.
require "csv"
class Recipe
# I need to finish this before I give Bernie the program...
attr_reader :id, :name, :description, :ingredients, :directions
def initialize(args)
@id = args[:id]
@name = args[:name]
@description = args[:description]
@ingredients = args[:ingredients]
@directions = args[:directions]
end
def to_a
[ self.id, self.name, self.description, self.ingredients, self.directions]
end
def to_s
<<-PRINT
id: #{self.id}
name: #{self.name}
description: #{self.description}
ingredients: #{self.ingredients}
directions: #{self.directions}
PRINT
end
end
class Bistro
attr_reader :recipes
def initialize
@recipes = []
#@filename = recipes.csv
end
def load_recipes
@recipes = Parser.load_recipes
end
def list_recipes
@recipes.each {|recipe| p recipe}
end
def add_recipe(args)
@recipes << Recipe.new(args)
end
def save_recipes
Parser.add_recipe_to_file(@recipes)
end
def find_recipe_by_id(recipe_id)
#recipes = []
puts "The id you are looking for is #{recipe_id}"
@recipes.each do |recipe|
if recipe.id == recipe_id.to_s
return recipe
end
#return "not found"
end
#raise "Can't find a recipe with an id of #{recipe_id.inspect}" unless recipe
#recipes
end
end
class Parser
def self.load_recipes(filename = "./recipes.csv")
@recipes = []
CSV.foreach(filename, headers: true, header_converters: :symbol) do |row|
@recipes << Recipe.new(row)
end
@recipes
end
def self.add_recipe_to_file(filename = "./test.csv", recipes)
CSV.open(filename, "a") do |csv|
recipes.each do |recipe|
# Turn the recipe object into an array and then shovel the array into the csv file
csv << recipe.to_a
end
end
end
end
class Viewer
def self.initialize
if ARGV.any?
run_task
else
prompt_user
end
end
def self.run_task
if ARGV[0].downcase == "list"
bistro.list_recipes
elsif ARGV[0].downcase == "display"
bistro.find_recipe_by_id(ARGV[1])
end
end
def self.prompt_user
puts "please enter a task: "
task = gets.chomp
puts "the task entered is #{task}"
end
end
Viewer.initialize
bistro = Bistro.new
#bistro = Bistro.new
#bistro.load_recipes
#bistro.list_recipes
#bistro.add_recipe(args = {:id => 4, :name => "cat", :description => "cat", :ingredients => "cat", :directions => "cook the cat"})
#bistro.save_recipes
# if ARGV.any?
# # I wonder if I could clean this up...
# bistro = Bistro.new
# bistro.load_recepes("recipes.csv")
# if ARGV[0] == "list"
# puts "TODO: implement listing of recipes"
# elsif ARGV[0] == "display"
# puts find_recipe_by_id(ARGV[1])
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment