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.
Merge branch 'master' of git://github.com/jnunemaker/mongomapper
- Loading branch information
Showing
12 changed files
with
278 additions
and
226 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module MongoMapper | ||
module Plugins | ||
module Timestamps | ||
module ClassMethods | ||
def timestamps! | ||
key :created_at, Time | ||
key :updated_at, Time | ||
class_eval { before_save :update_timestamps } | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
def update_timestamps | ||
now = Time.now.utc | ||
self[:created_at] = now if new? && !created_at? | ||
self[:updated_at] = now | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module MongoMapper | ||
module Plugins | ||
module Userstamps | ||
module ClassMethods | ||
def userstamps! | ||
key :creator_id, ObjectId | ||
key :updater_id, ObjectId | ||
belongs_to :creator, :class_name => 'User' | ||
belongs_to :updater, :class_name => 'User' | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.