module Delayed::ShallowMongoid

Constants

DocumentStub
VERSION

Public Class Methods

dump(arg) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/delayed/shallow_mongoid/mongoid.rb, line 7
def self.metadata(instance)
  if Delayed::ShallowMongoid.mongoid3?
    instance.metadata
  else
    instance.relation_metadata
  end
end
mongoid3?() click to toggle source
# 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) click to toggle source

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

# 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.metadata(doc).macro == :embeds_many
      selector.unshift Delayed::ShallowMongoid.metadata(doc).key
      doc = doc._parent
    end
  end
end