123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Persistable::Logical

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/persistable/logical.rb

Overview

Defines behavior for logical bitwise operations.

Instance Method Summary

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 })

Parameters:

  • operations (Hash)

    The bitwise operations.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/persistable/logical.rb', line 20

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