lib/mongo_mapper/plugins/modifiers.rb in mongo_mapper-0.7.3 vs lib/mongo_mapper/plugins/modifiers.rb in mongo_mapper-0.7.4
- old
+ new
@@ -15,23 +15,35 @@
def set(*args)
modifier_update('$set', args)
end
+ 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 = to_criteria(criteria)
+ modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
+ collection.update(criteria, {'$unset' => modifiers}, :multi => true)
+ end
+
def push(*args)
modifier_update('$push', args)
end
def push_all(*args)
modifier_update('$pushAll', args)
end
- def push_uniq(*args)
- criteria, keys = criteria_and_keys_from_args(args)
- keys.each { |key, value | criteria[key] = {'$ne' => value} }
- collection.update(criteria, {'$push' => keys}, :multi => true)
+ def add_to_set(*args)
+ modifier_update('$addToSet', args)
end
+ alias push_uniq add_to_set
def pull(*args)
modifier_update('$pull', args)
end
@@ -44,47 +56,51 @@
end
private
def modifier_update(modifier, args)
criteria, keys = criteria_and_keys_from_args(args)
- modifiers = {modifier => keys}
- collection.update(criteria, modifiers, :multi => true)
+ collection.update(criteria, {modifier => keys}, :multi => true)
end
def criteria_and_keys_from_args(args)
keys = args.pop
criteria = args[0].is_a?(Hash) ? args[0] : {:id => args}
[to_criteria(criteria), keys]
end
end
module InstanceMethods
+ def unset(*keys)
+ self.class.unset(id, *keys)
+ end
+
def increment(hash)
- self.class.increment({:_id => id}, hash)
+ self.class.increment(id, hash)
end
def decrement(hash)
- self.class.decrement({:_id => id}, hash)
+ self.class.decrement(id, hash)
end
def set(hash)
- self.class.set({:_id => id}, hash)
+ self.class.set(id, hash)
end
def push(hash)
- self.class.push({:_id => id}, hash)
+ self.class.push(id, hash)
end
def pull(hash)
- self.class.pull({:_id => id}, hash)
+ self.class.pull(id, hash)
end
- def push_uniq(hash)
- self.class.push_uniq({:_id => id}, hash)
+ def add_to_set(hash)
+ self.class.push_uniq(id, hash)
end
+ alias push_uniq add_to_set
def pop(hash)
- self.class.pop({:_id => id}, hash)
+ self.class.pop(id, hash)
end
end
end
end
end
\ No newline at end of file