Class: StoreTasks
Instance Method Summary
collapse
Methods inherited from Volt::Task
inherited, known_handlers, method_missing, #store
Constructor Details
#initialize(channel = nil, dispatcher = nil) ⇒ StoreTasks
Returns a new instance of StoreTasks.
5
6
7
8
|
# File 'app/volt/tasks/store_tasks.rb', line 5
def initialize(channel = nil, dispatcher = nil)
@channel = channel
@dispatcher = dispatcher
end
|
Instance Method Details
10
11
12
|
# File 'app/volt/tasks/store_tasks.rb', line 10
def db
@@db ||= Volt::DataStore.fetch
end
|
#delete(collection, id) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'app/volt/tasks/store_tasks.rb', line 69
def delete(collection, id)
query = nil
Volt.skip_permissions do
query = store.send(:"_#{collection}").where(_id: id)
end
query.fetch_first do |model|
if model
if model.can_delete?
db.delete('_id' => id)
else
raise "Permissions did not allow #{collection} #{id} to be deleted."
end
QueryTasks.live_query_pool.updated_collection(collection, @channel)
else
raise "Could not find #{id} in #{collection}"
end
end
end
|
#load_model(collection, path, data) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'app/volt/tasks/store_tasks.rb', line 14
def load_model(collection, path, data)
model_name = collection.singularize.camelize
collection = store.send(:"_#{path[-2]}")
collection.find(_id: data[:_id]).fetch_first do |model|
model ||= collection
buffer = model.buffer
buffer.assign_attributes(data, false, true)
buffer
end
end
|
#save(collection, path, data) ⇒ Object
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
|
# File 'app/volt/tasks/store_tasks.rb', line 35
def save(collection, path, data)
data = data.symbolize_keys
promise = nil
Volt.skip_permissions do
Volt::Model.no_validate do
promise = load_model(collection, path, data)
end
end
return promise.then do |model|
Thread.current['in_channel'] = @channel
save_promise = model.save!.then do |result|
next nil
end.fail do |err|
Promise.new.reject(err.to_h)
end
Thread.current['in_channel'] = nil
next save_promise
end
end
|