Module: MongoMapper::Plugins::Modifiers::ClassMethods

Defined in:
lib/mongo_mapper/plugins/modifiers.rb

Instance Method Summary collapse

Instance Method Details

#add_to_set(*args) ⇒ Object Also known as: push_uniq



48
49
50
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 48

def add_to_set(*args)
  modifier_update('$addToSet', args)
end

#decrement(*args) ⇒ Object



12
13
14
15
16
17
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 12

def decrement(*args)
  criteria, keys, options = criteria_and_keys_from_args(args)
  values, to_decrement = keys.values, {}
  keys.keys.each_with_index { |k, i| to_decrement[k] = -values[i].abs }
  collection.update(criteria, {'$inc' => to_decrement}, :multi => true)
end

#increment(*args) ⇒ Object



8
9
10
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 8

def increment(*args)
  modifier_update('$inc', args)
end

#pop(*args) ⇒ Object



61
62
63
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 61

def pop(*args)
  modifier_update('$pop', args)
end

#pull(*args) ⇒ Object



53
54
55
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 53

def pull(*args)
  modifier_update('$pull', args)
end

#pull_all(*args) ⇒ Object



57
58
59
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 57

def pull_all(*args)
  modifier_update('$pullAll', args)
end

#push(*args) ⇒ Object



40
41
42
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 40

def push(*args)
  modifier_update('$push', args)
end

#push_all(*args) ⇒ Object



44
45
46
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 44

def push_all(*args)
  modifier_update('$pushAll', args)
end

#set(*args) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 19

def set(*args)
  criteria, updates, options = criteria_and_keys_from_args(args)
  updates.each do |key, value|
    updates[key] = keys[key.to_s].set(value) if key?(key)
  end
  collection.update(criteria, {'$set' => updates}, :multi => true)
end

#unset(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 27

def unset(*args)
  if args[0].is_a?(Hash)
    criteria, keys = args.shift, args
  else
    keys, ids = args.partition { |arg| arg.is_a?(Symbol) }
    criteria = {:id => ids}
  end

  criteria  = criteria_hash(criteria).to_hash
  modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
  collection.update(criteria, {'$unset' => modifiers}, :multi => true)
end