-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookbook.rb
45 lines (37 loc) · 816 Bytes
/
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
require 'csv'
require_relative 'recipe'
class Cookbook
def initialize(csv_file)
@recipes = [] # <--- <Recipe> instances
@csv_file = csv_file
load_csv
end
def add_recipe(recipe)
@recipes << recipe
save_to_csv
end
def remove_recipe(index)
@recipes.delete_at(index)
save_to_csv
end
def mark_completed(index)
@recipes[index].mark_completed
save_to_csv
end
def all
@recipes
end
private
def load_csv
CSV.foreach(@csv_file) do |row|
@recipes << Recipe.new(row[0], row[1], row[2], row[3], row[4] == 'true')
end
end
def save_to_csv
CSV.open(@csv_file, 'wb') do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description, recipe.prep_time, recipe.difficulty, recipe.completed]
end
end
end
end