-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookbook.rb
61 lines (45 loc) · 1.02 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
53
54
55
56
require_relative 'recipe'
require_relative 'controller'
require 'csv'
require 'byebug'
class Cookbook
attr_accessor :recipes, :mark_recipe
def initialize(csv_file)
@recipes = []
@csv_file = csv_file
CSV.foreach(csv_file) do |row|
name = row[0]
description = row[1]
prep_time = row[2]
@recipes << Recipe.new(name: name, description: description, prep_time: prep_time)
end
end
def all
@recipes
end
def add_recipe(new_recipe)
@recipes.push(new_recipe)
update_csv
end
def remove_recipe(recipe_index)
@recipes.delete_at(recipe_index)
update_csv
end
private
def update_csv
CSV.open(@csv_file, "wb") do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description, recipe.prep_time]
end
end
end
def save_recipes
CSV.foreach(csv_file) do |row|
@recipes << Recipe.new(row[0], row[1], row[2])
end
end
def mark_recipe(mark_index)
@recipes[mark_index + 1][mark => "[x]"]
update_csv
end
end