-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidea.rb
More file actions
58 lines (48 loc) · 1.03 KB
/
idea.rb
File metadata and controls
58 lines (48 loc) · 1.03 KB
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
class Idea
attr_reader :title, :description
def initialize(title, description)
@title = title
@description = description
end
def save
database.transaction do |db|
db['ideas'] ||= []
db['ideas'] << {title: title, description: description}
end
end
def database
Idea.database
end
def self.find(id)
raw_idea = find_raw_idea(id)
new(raw_idea[:title], raw_idea[:description])
end
def self.find_raw_idea(id)
database.transaction do
database['ideas'].at(id)
end
end
def self.all
raw_ideas.map do |data|
Idea.new(data[:title], data[:description])
end
end
def self.raw_ideas
database.transaction do
database['ideas']
end
end
def self.database
@database ||= YAML::Store.new('ideabox')
end
def self.delete(position)
database.transaction do
database['ideas'].delete_at(position)
end
end
def self.update(id, data)
database.transaction do
database['ideas'][id] = data
end
end
end