-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
85 lines (67 loc) · 1.7 KB
/
app.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require "sinatra"
require "sinatra/reloader" if development?
require "pry-byebug"
require "better_errors"
require 'csv'
configure :development do
use BetterErrors::Middleware
BetterErrors.application_root = File.expand_path('..', __FILE__)
end
# get '/' do
# erb :index
# end
get '/about' do
erb :about
end
get '/new' do
erb :new
end
post '/' do
Cookbook.new('recipes.csv').add_recipe(Recipe.new(params[:recipe_name], params[:recipe_description]))
@recipes = Cookbook.new('recipes.csv')
erb :index
end
get '/' do
@recipes = Cookbook.new('recipes.csv')
erb :index
end
get '/delete/:number' do
Cookbook.new('recipes.csv').remove_recipe(params[:number].to_i)
@recipes = Cookbook.new('recipes.csv')
erb :index
end
class Recipe
attr_reader :name, :description, :time
def initialize(name, description, time = "No details")
@name = name
@description = description
@time = time
end
end
require 'csv'
class Cookbook
attr_reader :recipes
def initialize(csv_file_path)
@recipes = []
@csv_file_path = csv_file_path
CSV.foreach(@csv_file_path) do |recipe|
@recipes << Recipe.new(recipe[0], recipe[1], recipe[2])
end
return @recipes
end
def all
return @recipes
end
def add_recipe(recipe)
@recipes << recipe
CSV.open(@csv_file_path, 'wb') do |csv|
@recipes.each { |recipe_instance| csv << [recipe_instance.name, recipe_instance.description, recipe_instance.time] }
end
end
def remove_recipe(recipe_index)
@recipes.delete_at(recipe_index)
CSV.open(@csv_file_path, 'wb') do |csv|
@recipes.each { |recipe_instance| csv << [recipe_instance.name, recipe_instance.description, recipe_instance.time] }
end
end
end