Module: Mongoid::Persistable::Logical

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Persistable
Defined in:
lib/mongoid/persistable/logical.rb

Overview

Defines behaviour for logical bitwise operations.

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#bit(operations) ⇒ Document

Performs an atomic $bit operation on the field with the provided hash of bitwise ops to execute in order.

Examples:

Execute the bitwise operations.

person.bit(age: { :and => 12 }, val: { and: 10, or: 12 })

Since:

  • 4.0.0


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/persistable/logical.rb', line 22

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
      attributes[field] = value
      ops[atomic_attribute_name(field)] = values
    end
    { "$bit" => ops }
  end
end