Sha256: dbc978fbeb2303c5c525b2f0612c3ed73c31a9c169929c28b2091fba15741c9d

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module Delayed
  module ShallowMongoid
    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?
        ShallowMongoid::DocumentStub.new(arg._root.class, arg._root._id.to_s, selector_from(arg))
      else
        ShallowMongoid::DocumentStub.new(arg.class, arg._id.to_s)
      end
    end
  
    def self.load(arg)
      return arg unless arg.is_a?(ShallowMongoid::DocumentStub)
      result = arg.klass.find(arg.id)
      (arg.selector || []).each do |message|
        result = result.send(*message)
      end
      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 do
          selector.unshift ['find', doc._id.to_s] if doc.metadata.macro == :embeds_many
          selector.unshift doc.metadata.key
          doc = doc._parent
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
delayed_job_shallow_mongoid-0.5.0 lib/delayed/shallow_mongoid.rb
delayed_job_shallow_mongoid-0.4.0 lib/delayed/shallow_mongoid.rb
delayed_job_shallow_mongoid-0.3.0 lib/delayed/shallow_mongoid.rb
delayed_job_shallow_mongoid-0.2.8 lib/delayed/shallow_mongoid.rb