Module: MongoMapper::Plugins::Modifiers::ClassMethods
- Defined in:
- lib/mongo_mapper/plugins/modifiers.rb
Instance Method Summary collapse
- #add_to_set(*args) ⇒ Object (also: #push_uniq)
- #decrement(*args) ⇒ Object
- #increment(*args) ⇒ Object
- #pop(*args) ⇒ Object
- #pull(*args) ⇒ Object
- #pull_all(*args) ⇒ Object
- #push(*args) ⇒ Object
- #push_all(*args) ⇒ Object
- #set(*args) ⇒ Object
- #unset(*args) ⇒ Object
Instance Method Details
#add_to_set(*args) ⇒ Object Also known as: push_uniq
50 51 52 |
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 50 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, = 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
63 64 65 |
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 63 def pop(*args) modifier_update('$pop', args) end |
#pull(*args) ⇒ Object
55 56 57 |
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 55 def pull(*args) modifier_update('$pull', args) end |
#pull_all(*args) ⇒ Object
59 60 61 |
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 59 def pull_all(*args) modifier_update('$pullAll', args) end |
#push(*args) ⇒ Object
42 43 44 |
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 42 def push(*args) modifier_update('$push', args) end |
#push_all(*args) ⇒ Object
46 47 48 |
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 46 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, = criteria_and_keys_from_args(args) updates.each do |key, value| updates[key] = keys[key.to_s].set(value) if key?(key) end modifier_update('$set', [criteria, updates, ]) end |
#unset(*args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mongo_mapper/plugins/modifiers.rb', line 27 def unset(*args) if args[0].is_a?(Hash) criteria, keys = args.shift, args = keys.last.is_a?(Hash) ? keys.pop : {} else keys, ids = args.partition { |arg| arg.is_a?(Symbol) } = ids.last.is_a?(Hash) ? ids.pop : {} criteria = {:id => ids} end criteria = criteria_hash(criteria).to_hash updates = keys.inject({}) { |hash, key| hash[key] = 1; hash } modifier_update('$unset', [criteria, updates, ]) end |