Class: Mongoid::Persistence::Atomic::Bit

Inherits:
Object
  • Object
show all
Includes:
Operation
Defined in:
lib/mongoid/persistence/atomic/bit.rb

Overview

This operation is for performing $bit atomic operations against the database.

Instance Attribute Summary

Attributes included from Operation

#document, #field, #options, #value

Instance Method Summary collapse

Methods included from Operation

#collection, #initialize, #operation, #path, #prepare

Instance Method Details

#persistInteger

Execute the bitwise operation. This correlates to a $bit in MongoDB.

Examples:

Execute the op.

bit.persist

Returns:

Since:

  • 2.1.0



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongoid/persistence/atomic/bit.rb', line 19

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