Sha256: d7ae9bbcf8ee65f6a822720bc12622b3bd5ea29221f25c79f9fbdc48965dd28e

Contents?: true

Size: 1.18 KB

Versions: 27

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

module Mongoid
  module Persistable

    # Defines behavior for logical bitwise operations.
    #
    # @since 4.0.0
    module Logical
      extend ActiveSupport::Concern

      # Performs an atomic $bit operation on the field with the provided hash
      # of bitwise ops to execute in order.
      #
      # @example Execute the bitwise operations.
      #   person.bit(age: { :and => 12 }, val: { and: 10, or: 12 })
      #
      # @param [ Hash ] operations The bitwise operations.
      #
      # @return [ Document ] The document.
      #
      # @since 4.0.0
      def bit(operations)
        prepare_atomic_operation do |ops|
          process_atomic_operations(operations) do |field, values|
            value = attributes[field]
            values.each do |op, val|
              value = value & val if op.to_s == "and"
              value = value | val if op.to_s == "or"
            end
            process_attribute field, value if executing_atomically?
            attributes[field] = value
            ops[atomic_attribute_name(field)] = values
          end
          { "$bit" => ops } unless ops.empty?
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 lib/mongoid/persistable/logical.rb
mongoid-7.3.4 lib/mongoid/persistable/logical.rb
mongoid-7.1.11 lib/mongoid/persistable/logical.rb
mongoid-7.2.6 lib/mongoid/persistable/logical.rb
mongoid-7.3.3 lib/mongoid/persistable/logical.rb
mongoid-7.3.2 lib/mongoid/persistable/logical.rb
mongoid-7.2.5 lib/mongoid/persistable/logical.rb
mongoid-7.1.10 lib/mongoid/persistable/logical.rb
mongoid-7.1.9 lib/mongoid/persistable/logical.rb
mongoid-7.2.4 lib/mongoid/persistable/logical.rb
mongoid-7.3.1 lib/mongoid/persistable/logical.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/lib/mongoid/persistable/logical.rb
mongoid-7.3.0 lib/mongoid/persistable/logical.rb
mongoid-7.2.3 lib/mongoid/persistable/logical.rb
mongoid-7.1.8 lib/mongoid/persistable/logical.rb
mongoid-7.2.2 lib/mongoid/persistable/logical.rb
mongoid-7.2.1 lib/mongoid/persistable/logical.rb
mongoid-7.1.7 lib/mongoid/persistable/logical.rb
mongoid-7.2.0 lib/mongoid/persistable/logical.rb
mongoid-7.1.6 lib/mongoid/persistable/logical.rb