Module: Mongoid::Atomic

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_atomic.rb

Instance Method Summary collapse

Instance Method Details

#atomic_add_to_set(field, value) ⇒ Object



34
35
36
# File 'lib/mongoid_atomic.rb', line 34

def atomic_add_to_set(field, value)
  atomic_update :addToSet => { field => value }
end

#atomic_inc(field, by = 1) ⇒ Object Also known as: atomic_increment



13
14
15
# File 'lib/mongoid_atomic.rb', line 13

def atomic_inc(field, by=1)
  atomic_update :inc => { field => by }
end

#atomic_pop(field, index = 1) ⇒ Object



38
39
40
# File 'lib/mongoid_atomic.rb', line 38

def atomic_pop(field, index=1)
  atomic_update :pop => { field => index.to_i }
end

#atomic_pull(field, value) ⇒ Object



42
43
44
# File 'lib/mongoid_atomic.rb', line 42

def atomic_pull(field, value)
  atomic_update :pull => { field => value }
end

#atomic_pull_all(field, array) ⇒ Object



46
47
48
# File 'lib/mongoid_atomic.rb', line 46

def atomic_pull_all(field, array)
  atomic_update :pullAll => { field => array.to_a }
end

#atomic_push(field, value) ⇒ Object



26
27
28
# File 'lib/mongoid_atomic.rb', line 26

def atomic_push(field, value)
  atomic_update :push => { field => value }
end

#atomic_push_all(field, array) ⇒ Object



30
31
32
# File 'lib/mongoid_atomic.rb', line 30

def atomic_push_all(field, array)
  atomic_update :pushAll => { field => array.to_a }
end

#atomic_set(field, value) ⇒ Object



18
19
20
# File 'lib/mongoid_atomic.rb', line 18

def atomic_set(field, value)
  atomic_update :set => { field => value }
end

#atomic_unset(field) ⇒ Object



22
23
24
# File 'lib/mongoid_atomic.rb', line 22

def atomic_unset(field)
  atomic_update :unset => { field => 1 }
end

#atomic_update(options) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/mongoid_atomic.rb', line 4

def atomic_update(options)
  doc = {}
  options.each do |k,v|
    next unless %w[inc set unset push pushAll addToSet pop pull pullAll].include? k.to_s
    doc["$#{k}"] = v
  end
  collection.update({'_id' => self._id}, doc)
end