Module: Delayed::ShallowMongoid

Defined in:
lib/delayed/shallow_mongoid.rb,
lib/delayed/shallow_mongoid/mongoid.rb,
lib/delayed/shallow_mongoid/version.rb,
lib/delayed/shallow_mongoid/document_stub.rb

Defined Under Namespace

Modules: Errors Classes: DocumentStub

Constant Summary collapse

VERSION =
'1.2.0'

Class Method Summary collapse

Class Method Details

.dump(arg) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/delayed/shallow_mongoid.rb', line 8

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

.load(arg) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/delayed/shallow_mongoid.rb', line 18

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

.metadata(instance) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/delayed/shallow_mongoid/mongoid.rb', line 7

def self.(instance)
  if Delayed::ShallowMongoid.mongoid3?
    instance.
  else
    instance.
  end
end

.mongoid3?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/delayed/shallow_mongoid/mongoid.rb', line 3

def self.mongoid3?
  ::Mongoid.const_defined? :Observer # deprecated in Mongoid 4.x
end

.selector_from(doc) ⇒ Object

The chain of relations allowing us to locate an embedded document. E.g., [‘images’, [‘find’, ‘4eef..678’], ‘width’]



35
36
37
38
39
40
41
42
43
# File 'lib/delayed/shallow_mongoid.rb', line 35

def self.selector_from(doc)
  [].tap do |selector|
    while doc._parent
      selector.unshift ['find', doc._id.to_s] if Delayed::ShallowMongoid.(doc).macro == :embeds_many
      selector.unshift Delayed::ShallowMongoid.(doc).key
      doc = doc._parent
    end
  end
end