-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookbook.rb
52 lines (43 loc) · 1.06 KB
/
cookbook.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'csv'
class Cookbook
def initialize(csv_file_path = nil)
@csv_file_path = csv_file_path
@recipes = []
if csv_file_path
CSV.foreach(@csv_file_path) do |row|
done = row[4] == "true"
recipe_hash = { name: row[0], description: row[1], preptime: row[2], rating: row[3], done: done }
@recipes << Recipe.new(recipe_hash)
end
end
end
def all
# returns all the recipes
return @recipes
end
def csv
@csv_file_path ? true : false
end
def add_recipe(new_recipe)
# add a new recipe
@recipes << new_recipe
save_to_csv
end
def remove_recipe(recipe_index)
# remove a recipe at a particular index (line number)
@recipes.delete_at(recipe_index)
save_to_csv if csv
end
def mark_as_done!(recipe)
recipe.mark_as_done!
save_to_csv if csv
end
private
def save_to_csv
CSV.open(@csv_file_path, "w") do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description, recipe.preptime, recipe.rating, recipe.done]
end
end
end
end