-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookbook.rb
49 lines (40 loc) · 873 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
46
47
48
49
require 'csv'
require_relative 'recipe'
class Cookbook
attr_reader :recipes
def initialize(csv_file_path)
@file_path = csv_file_path
@recipes = []
load_csv
end
def all
@recipes
end
def add_recipe(recipe)
@recipes << recipe
update_csv
end
def remove_recipe(recipe_index)
@recipes.delete_at(recipe_index)
update_csv
end
def recipe_index_exists?(recipe_index)
@recipes[recipe_index].nil? ? false : true
end
def mark_as_done(recipe_index)
@recipes[recipe_index].done = true
end
private
def load_csv
CSV.foreach(@file_path) do |row|
@recipes << Recipe.new(row[0], row[1])
end
end
def update_csv
CSV.open(@file_path, 'wb') do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description, recipe.prep_time, recipe.difficulty]
end
end
end
end