-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookbook.rb
52 lines (46 loc) · 1.19 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"
require_relative "recipe"
class Cookbook
def initialize(csv_file_path)
@filepath = csv_file_path
@recipes = []
unless @filepath == ""
CSV.foreach(@filepath) do |row|
@recipes << Recipe.new(
{ name: row[0], description: row[1],
rating: row[2], read: row[3], duration: row[4], link: row[5] }
)
end
end
end
def all
@recipes
end
def add_recipe(recipe)
@recipes << recipe
CSV.open(@filepath, 'wb') do |csv|
@recipes.each do |item|
csv << [item.name, item.description, item.rating, item.read, item.duration, item.link]
end
end
end
def mark_as_read(recipe_index)
find(recipe_index).read = true
CSV.open(@filepath, 'wb') do |csv|
@recipes.each do |item|
csv << [item.name, item.description, item.rating, item.read, item.duration, item.link]
end
end
end
def find(index)
@recipes[index]
end
def remove_recipe(recipe_index)
@recipes.delete_at(recipe_index)
CSV.open(@filepath, 'wb') do |csv|
@recipes.each do |item|
csv << [item.name, item.description, item.rating, item.read, item.duration, item.link]
end
end
end
end