-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathin_memory.rb
163 lines (139 loc) · 3.26 KB
/
in_memory.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
module InMemory
class IndexedRepo
def initialize
@id_counter = 0
@records = []
end
def all
copy_and_return @records
end
def find(id)
find_one {|r| r.id == id }
end
def find_one(&block)
copy_and_return @records.find(&block)
end
def find_all(&block)
copy_and_return @records.select(&block)
end
##
# We make sure to always clone the object being returned
# so that it's an exact copy but changing it doesn't change
# the True Value stored in @records
##
def copy_and_return(result_or_array)
if result_or_array.nil?
nil
elsif result_or_array.is_a?(Array)
result_or_array.map {|r| copy_and_return(r) }
else
result_or_array.clone
end
end
##
# Save the record to the persistence store
# Will return true on success, false if there
# are any errors on the object.
##
def save(obj)
if obj.errors.empty?
set_or_replace_record obj
true
else
false
end
end
def set_or_replace_record(obj)
@records.delete_if {|record| record.id == obj.id }
obj.id ||= (@id_counter += 1)
# Dup to clean up any extra added pieces, like Errors
@records << obj.dup
end
end
class GuildRepo < IndexedRepo
def find_by_name(name)
find_one {|g| g.name == name }
end
def search_by_name(query)
find_all {|g|
g.name =~ /#{query}/i
}
end
end
class UserRepo < IndexedRepo
def find_by_login(login)
find_one {|u| u.login == login }
end
def find_by_login_token(type, token)
find_one {|u| u.login_token(type) == token }
end
end
class CharacterRepo < IndexedRepo
def find_by_user_and_id(user, id)
find_one {|c|
c.id == id &&
c.user.id == user.id
}
end
def find_all_for_user(user)
find_all {|c| c.user == user }
end
def find_main_character(user, guild)
find_one {|c|
c.user == user &&
c.guild == guild &&
c.main?
}
end
def find_all_in_guild(guild)
find_all {|c| c.guild == guild }
end
def find_all_for_user_in_guild(user, guild)
find_all { |char|
char.user == user && char.guild == guild
}
end
end
class RaidRepo < IndexedRepo
def find_raids_for_guild(guild)
find_all {|r| r.owner == guild }
end
def find_raids_for_guild_and_day(guild, day)
raids = find_raids_for_guild(guild)
if day
raids.select {|raid| raid.when == day }
else
raids
end
end
end
class SignupRepo < IndexedRepo
def find_all_for_raid(raid)
find_all {|s| s.raid == raid }
end
def find_all_for_user_and_raid(user, raid)
find_all {|s|
s.raid == raid && s.user == user
}
end
def find_by_raid_and_id(raid, id)
find_one {|s|
s.id == id && s.raid == raid
}
end
end
class PermissionRepo < IndexedRepo
def find_by_user_and_guild(user, guild)
find_one {|perm|
perm.user == user && perm.guild == guild
}
end
end
class CommentRepo < IndexedRepo
def find_all_by_signup(signup)
find_all {|c|
c.signup == signup
}
end
end
end