-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookbook.rb
56 lines (49 loc) · 1.2 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 'csv'
require_relative "recipe"
class Cookbook
def initialize(csv_file_path)
@csv_file_path = csv_file_path
@recipes = []
@recipes = loading_csv
end
def all
@recipes
end
def add_recipe(recipe)
@recipes << recipe
writing_csv
end
def remove_recipe(recipe_index)
@recipes.delete_at(recipe_index)
writing_csv
end
def mark_as_done(recipe_index)
@recipes[recipe_index].done = true
writing_csv
@recipes[recipe_index].name
end
def loading_csv
# no headers headers: :first_row
# csv_options = { col_sep: ',', quote_char: '"' }
CSV.foreach(@csv_file_path) do |row|
# recipe_done = row[3] == "true" ? true : false
@recipes << Recipe.new(
{ name: row[0],
description: row[1],
rating: row[2],
done: (row[3] == "true"),
prep_time: row[4] }
)
end
@recipes
end
def writing_csv
# no headers headers: :first_row
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' }
CSV.open(@csv_file_path, 'wb', csv_options) do |csv|
@recipes.each do |v|
csv << [v.name, v.description, v.rating, v.done, v.prep_time]
end
end
end
end