forked from mongomapper/mongomapper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
687311b
commit 7784660
Showing
4 changed files
with
101 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module MongoMapper | ||
module Plugins | ||
module Modifiers | ||
module ClassMethods | ||
def increment(*args) | ||
modifier_update('$inc', args) | ||
end | ||
|
||
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 | ||
|
||
def set(*args) | ||
modifier_update('$set', args) | ||
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) | ||
end | ||
|
||
def pull(*args) | ||
modifier_update('$pull', args) | ||
end | ||
|
||
def pull_all(*args) | ||
modifier_update('$pullAll', args) | ||
end | ||
|
||
def pop(*args) | ||
modifier_update('$pop', args) | ||
end | ||
|
||
private | ||
def modifier_update(modifier, args) | ||
criteria, keys = criteria_and_keys_from_args(args) | ||
modifiers = {modifier => keys} | ||
collection.update(criteria, modifiers, :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 increment(hash) | ||
self.class.increment({:_id => id}, hash) | ||
end | ||
|
||
def decrement(hash) | ||
self.class.decrement({:_id => id}, hash) | ||
end | ||
|
||
def set(hash) | ||
self.class.set({:_id => id}, hash) | ||
end | ||
|
||
def push(hash) | ||
self.class.push({:_id => id}, hash) | ||
end | ||
|
||
def pull(hash) | ||
self.class.pull({:_id => id}, hash) | ||
end | ||
|
||
def push_uniq(hash) | ||
self.class.push_uniq({:_id => id}, hash) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters