-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
70 lines (60 loc) · 1.43 KB
/
models.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
require 'securerandom'
module Models
class Quote
@@instances = []
attr_accessor :author, :content, :language
attr_reader :id
def initialize(author, content, language = 'en')
@id = SecureRandom.uuid
@author = author
@content = content
@language = language
@@instances << self
end
def as_text
"
#{id}.
#{content}
--#{author}
"
end
def self.create(hash_or_array)
if hash_or_array.is_a?(Hash)
hsh = hash_or_array
Quote.new(hsh[:author], hsh[:content], hsh[:language])
elsif hash_or_array.is_a?(Array) && hash_or_array.all?{|e| e.is_a?(Hash)}
hash_or_array.map{|h| Quote.create(h) }
end
end
def self.all
@@instances
end
def self.find(id)
@@instances.find do |instance|
instance.id == id
end
end
end
end
Models::Quote.create([
{
author: "Ralph Waldo Emerson",
content: "Every sweet has its sour; every evil its good.",
language: "en"
},
{
author: "Winston Churchill",
content: "We make a living by what we get, we make a life by what we give",
language: "en"
},
{
author: "Siddhartha Gautama",
content: "El dolor es inevitable pero el sufrimiento es opcional.",
language: "es"
},
{
author: "Walt Whitman",
content: "Be curious, not judgmental",
language: "en"
}
])