-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentence.rb
66 lines (50 loc) · 1.41 KB
/
sentence.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
#encoding: utf-8
require 'datamapper'
require 'dm-aggregates'
#database stuff
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/my.db")
configure :test do
DataMapper.setup(:default, "sqlite::memory:")
end
class Sentence
include DataMapper::Resource
property :id, Serial
property :contents, Text, :required => true
property :speaker, String, :required => true
property :meaning, String
property :created_at, DateTime
def self.random
Sentence.first(:limit => 1, :offset =>rand(Sentence.count))
end
def self.solve_names
Sentence.all.each do |sentence|
if sentence.speaker == "Vic" or sentence.speaker == "Victor"
sentence.speaker = "Víctor"
sentence.save
elsif sentence.speaker =="Johnny"
sentence.speaker = "John"
sentence.save
elsif sentence.speaker =="Alvaro"
sentence.speaker = "Álvaro"
sentence.save
end
end
end
def self.all_speakers
names = Sentence.all.map {|s| s.speaker}
names.uniq!
names
end
def self.ranking
names = Sentence.all_speakers
ranking ={}
names.each do |n|
count = Sentence.count( :speaker => n )
ranking[n] = count
end
ranking = ranking.sort{|a,b| a[1] <=> b[1]}.reverse!.first(5)
ranking
end
end
# migrate deletes all the data , dangerous
Sentence.auto_migrate! unless Sentence.storage_exists?