Sha256: 42ad94dd234f75dffeca052ad0beac816a82cd3eb525beb3a216c725af0c7c26

Contents?: true

Size: 1.03 KB

Versions: 188

Compression:

Stored size: 1.03 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Persistence #:nodoc:
    module Atomic #:nodoc:

      # This operation is for performing $bit atomic operations against the
      # database.
      class Bit
        include Operation

        # Execute the bitwise operation. This correlates to a $bit in MongoDB.
        #
        # @example Execute the op.
        #   bit.persist
        #
        # @return [ Integer ] The new value.
        #
        # @since 2.1.0
        def persist
          prepare do
            current = document[field]
            return nil unless current
            document[field] = value.inject(current) do |result, (bit, val)|
              result = result & val if bit.to_s == "and"
              result = result | val if bit.to_s == "or"
              result
            end
            document[field].tap do
              collection.update(document.atomic_selector, operation("$bit"), options)
              document.remove_change(field)
            end
          end
        end
      end
    end
  end
end

Version data entries

188 entries across 96 versions & 4 rubygems

Version Path
mongoid-2.1.7 lib/mongoid/persistence/atomic/bit.rb
mongoid-2.1.6 lib/mongoid/persistence/atomic/bit.rb
mongoid-2.1.5 lib/mongoid/persistence/atomic/bit.rb
mongoid-2.1.4 lib/mongoid/persistence/atomic/bit.rb
mongoid-2.1.3 lib/mongoid/persistence/atomic/bit.rb
mongoid-2.1.2 lib/mongoid/persistence/atomic/bit.rb
mongoid-2.1.1 lib/mongoid/persistence/atomic/bit.rb
mongoid-2.1.0 lib/mongoid/persistence/atomic/bit.rb