Sha256: 68c111c12449f803c5c62847b06423a4c13e693d1dbdec92fbfcbb3a6b6cf297

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module Mongoid
  module Locker
    # Normalizes queries between various Mongoid versions.
    module Wrapper
      # Update the document for the provided Class matching the provided query with the provided setter.
      #
      # @param [Class] The model class
      # @param [Hash] The Mongoid query
      # @param [Hash] The Mongoid setter
      # @return [Boolean] true if the document was successfully updated, false otherwise
      def self.update(klass, query, setter)
        rc = klass.with(write: { w: 1 }) do
          klass.collection.find(query).update_one(setter)
        end
        rc.ok? && rc.documents.first['n'] == 1
      end

      # Determine whether the provided document is locked in the database or not.
      #
      # @param [Class] The model instance
      # @return [Time] The timestamp of when the document is locked until, nil if not locked.
      def self.locked_until(doc)
        existing_query = { _id: doc.id, doc.locked_until_field => { '$exists' => true } }
        existing = doc.class.where(existing_query).limit(1).only(doc.locked_until_field).first
        existing ? existing[doc.locked_until_field] : nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-locker-1.0.1 lib/mongoid/locker/wrapper6.rb
mongoid-locker-1.0.0 lib/mongoid/locker/wrapper6.rb