-
Notifications
You must be signed in to change notification settings - Fork 1
/
review.rb
59 lines (48 loc) · 1.01 KB
/
review.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
require 'yaml'
class Review
attr_accessor :name, :id, :body, :image
def self.all
data = YAML.load_file('./reviews.yml')
data.map do | datum |
Review.new(datum)
end
end
def initialize(attributes)
@name = attributes['name']
@id = attributes['id']
@body = attributes['body']
@image = attributes['image']
end
def url
"/review/#{self.id}"
end
def image_url
"/assets/images/#{self.image}"
end
def self.find(id)
Review.all.find { |review| review.id == id }
end
def ==(other)
self.id == other.id
end
def to_hash
{
"id" => self.id,
"name" => self.name,
"body" => self.body,
"image" => self.image
}
end
def save
data = YAML.load_file('./reviews.yml')
index = data.find_index { |datum| datum["id"] == self.id }
if index
data[index] = self.to_hash
else
self.id = data.size + 1
data[data.size] = self.to_hash
end
File.write('./reviews.yml', data.to_yaml)
self
end
end