Skip to content

Instantly share code, notes, and snippets.

@matoni109
Created October 29, 2020 07:57
Show Gist options
  • Save matoni109/d18be00727d03d2db4183563d3b1254c to your computer and use it in GitHub Desktop.
Save matoni109/d18be00727d03d2db4183563d3b1254c to your computer and use it in GitHub Desktop.
require 'csv'
require_relative 'recipe'
class Cookbook # or DATABASE
attr_reader :recipes
def initialize(csv_filepath)
@recipes = [] # array of Task Instances
@csv_filepath = csv_filepath
load_csv(@csv_filepath)
end
def load_csv(csv_filepath)
CSV.foreach(csv_filepath) do |row|
recipe = Recipe.new(row[0], row[1])
# Convert the each array into a hash
@recipes << recipe
end
end
# # Behavior # CRUD
def add_recipe(recipe)# task parameter is a task instance
@recipes << recipe
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' }
CSV.open(@csv_filepath, 'wb', csv_options) do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description]
end
end
end
def all
@recipes
end
# def find(index)
# @tasks[index]
# end
def remove_recipe(index)
@recipes.delete_at(index)
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' }
CSV.open(@csv_filepath, 'w', csv_options) do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description]
end
end
end
end
# my_repo = Cookbook.new('recipes.csv')
# my_repo.add_recipe(Recipe.new('carrots', 'carrots description'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment