forked from mrdoob/glsl-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.rb
213 lines (173 loc) · 4.59 KB
/
model.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
class Effects
def initialize(effects, extra={})
@effects=effects
@extra={
:page => 0,
:count => 0,
:effects_per_page => EFFECTS_PER_PAGE
}.merge(extra)
end
def bind
binding
end
def effects
@effects
end
def extra
@extra
end
def previous_page
if @extra[:page]>0
@extra[:page]-1
end
end
def next_page
if @extra[:count]>=((@extra[:page]+1)*@extra[:effects_per_page])
@extra[:page]+1
end
end
def image(effect)
if effect["image_url"]
effect["image_url"]
else
effect["image"]
end
end
end
class GlslDatabase
def initialize
connect_database
initialize_counter
end
def increment_code_counter
[email protected]_and_modify({
:query => {:_id => 'code'},
:update => {'$inc' => {:counter => 1}}
})
counter['counter']
end
def save_version(code_id, code)
time=Time.now
code_data=JSON.parse(code)
data={
:created_at => time,
:code => code_data['code']
}
res=Cloudinary::Uploader.upload(
code_data['image'],
:public_id => code_id.to_s)
image_url=res['url']
@code.find_and_modify({
:query => { :_id => code_id },
:update => {
'$set' => {
:modified_at => time,
:image_url => image_url
},
'$push' => { :versions => data }
}
})
code=$glsl.get_code(code_id)
version=code['versions'].length-1
"#{code_id}.#{version}"
end
def save_effect(code)
code_data=JSON.parse(code)
if code_data['code_id'] && !code_data['code_id'].empty?
m=code_data['code_id'].match(/^(\d+)(\.\d+)?/)
if m
save_version(m[1].to_i, code)
else
save_new_effect(code)
end
else
save_new_effect(code)
end
end
def save_new_effect(code)
counter=increment_code_counter
code_data=JSON.parse(code)
data={
:_id => counter,
:created_at => Time.now,
:modified_at => Time.now,
:versions => [],
:user => code_data['user']
}
if code_data['parent']
m=code_data['parent'].match(%r{^(\d+)(\.(\d+))?})
data[:parent] = m[1].to_i if m
data[:parent_version] = m[3].to_i if m && m[3]
end
@code.insert(data)
save_version(counter, code)
end
def get_page(page=0, effects_per_page=50)
[email protected]({}, {
:sort => [:modified_at, 'descending'],
:limit => effects_per_page,
:skip => page*effects_per_page
})
ef=Effects.new(effects,
:page => page,
:count => count
)
ef
end
def get_code(id)
@code.find_one({:_id => id})
end
def get_code_json(id, version=nil)
code=get_code(id)
if code
if version
item=code['versions'][version]
else
item=code['versions'].last
end
if code['user']
user=code['user']
else
user=false
end
parent=nil
if code['parent']
parent="/e##{code['parent']}"
parent+=".#{code['parent_version']}" if code['parent_version']
end
else
item=nil
end
if item
data={
:code => item['code'],
:user => user,
:parent => parent
}.to_json
else
{
:code => '// item not found',
:user => false
}.to_json
end
end
private
def connect_database
uri = URI.parse(ENV['MONGOHQ_URL'])
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
@db = conn.db(uri.path.gsub(/^\//, ''))
@[email protected]('versions')
@[email protected]('code')
@[email protected]('counters')
end
def initialize_counter
[email protected]_one({:_id => 'code'})
if !code
@counters.insert({
:_id => 'code',
:counter => 0
})
end
end
end