-
Notifications
You must be signed in to change notification settings - Fork 1
/
shallow_mongoid.rb
45 lines (42 loc) · 1.49 KB
/
shallow_mongoid.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
module Delayed
module ShallowMongoid
module Errors
class DocumentNotFound < StandardError
end
end
def self.dump(arg)
return arg unless arg.is_a?(::Mongoid::Document) && arg.persisted?
return arg if arg._updates.any? && !Delayed::Worker.delay_jobs
if arg.embedded?
Delayed::ShallowMongoid::DocumentStub.new(arg._root.class, arg._root._id.to_s, selector_from(arg))
else
Delayed::ShallowMongoid::DocumentStub.new(arg.class, arg._id.to_s)
end
end
def self.load(arg)
return arg unless arg.is_a?(Delayed::ShallowMongoid::DocumentStub)
begin
result = arg.klass.find(arg.id)
fail Delayed::ShallowMongoid::Errors::DocumentNotFound unless result
rescue Mongoid::Errors::DocumentNotFound
raise Delayed::ShallowMongoid::Errors::DocumentNotFound
end
(arg.selector || []).each do |message|
result = result.send(*message)
end
fail Delayed::ShallowMongoid::Errors::DocumentNotFound unless result
result
end
# The chain of relations allowing us to locate an embedded document.
# E.g., ['images', ['find', '4eef..678'], 'width']
def self.selector_from(doc)
[].tap do |selector|
while doc._parent
selector.unshift ['find', doc._id.to_s] if Delayed::ShallowMongoid.metadata(doc).macro == :embeds_many
selector.unshift Delayed::ShallowMongoid.metadata(doc).key
doc = doc._parent
end
end
end
end
end