Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/jnunemaker/mongomapper
Browse files Browse the repository at this point in the history
  • Loading branch information
hookercookerman committed Mar 4, 2010
2 parents 3b051f0 + cefbe43 commit 0d78cc0
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 226 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -12,7 +12,7 @@ Jeweler::Tasks.new do |gem|
gem.authors = ["John Nunemaker"]

gem.add_dependency('activesupport', '>= 2.3')
gem.add_dependency('mongo', '0.18.3')
gem.add_dependency('mongo', '0.19.1')
gem.add_dependency('jnunemaker-validatable', '1.8.3')

gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper.rb
Expand Up @@ -7,7 +7,7 @@
# if there is a better way to do this, please enlighten me!
if self.class.const_defined?(:Gem)
gem 'activesupport', '>= 2.3'
gem 'mongo', '0.18.3'
gem 'mongo', '0.19.1'
gem 'jnunemaker-validatable', '1.8.3'
end

Expand Down
99 changes: 3 additions & 96 deletions lib/mongo_mapper/document.rb
Expand Up @@ -17,10 +17,13 @@ def self.included(model)
plugin Plugins::Keys
plugin Plugins::Dirty # for now dirty needs to be after keys
plugin Plugins::Logger
plugin Plugins::Modifiers
plugin Plugins::Pagination
plugin Plugins::Protected
plugin Plugins::Rails
plugin Plugins::Serialization
plugin Plugins::Timestamps
plugin Plugins::Userstamps
plugin Plugins::Validations
plugin Plugins::Callbacks # for now callbacks needs to be after validations

Expand Down Expand Up @@ -143,47 +146,6 @@ def destroy_all(options={})
find_each(options) { |document| document.destroy }
end

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

def embeddable?
false
end
Expand Down Expand Up @@ -225,19 +187,6 @@ def collection
database.collection(collection_name)
end

def timestamps!
key :created_at, Time
key :updated_at, Time
class_eval { before_save :update_timestamps }
end

def userstamps!
key :creator_id, ObjectId
key :updater_id, ObjectId
belongs_to :creator, :class_name => 'User'
belongs_to :updater, :class_name => 'User'
end

def single_collection_inherited?
keys.key?(:_type) && single_collection_inherited_superclass?
end
Expand All @@ -258,18 +207,6 @@ def initialize_each(*docs)
instances.size == 1 ? instances[0] : instances
end

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

def assert_no_first_last_or_all(args)
if args[0] == :first || args[0] == :last || args[0] == :all
raise ArgumentError, "#{self}.find(:#{args}) is no longer supported, use #{self}.#{args} instead."
Expand Down Expand Up @@ -397,30 +334,6 @@ def reload
def _root_document
self
end

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

private
def create_or_update(options={})
Expand All @@ -441,12 +354,6 @@ def save_to_collection(options={})
@new = false
collection.save(to_mongo, :safe => safe)
end

def update_timestamps
now = Time.now.utc
self[:created_at] = now if new? && !created_at?
self[:updated_at] = now
end
end
end # Document
end # MongoMapper
3 changes: 3 additions & 0 deletions lib/mongo_mapper/plugins.rb
Expand Up @@ -20,9 +20,12 @@ def plugin(mod)
autoload :Inspect, 'mongo_mapper/plugins/inspect'
autoload :Keys, 'mongo_mapper/plugins/keys'
autoload :Logger, 'mongo_mapper/plugins/logger'
autoload :Modifiers, 'mongo_mapper/plugins/modifiers'
autoload :Protected, 'mongo_mapper/plugins/protected'
autoload :Rails, 'mongo_mapper/plugins/rails'
autoload :Serialization, 'mongo_mapper/plugins/serialization'
autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
autoload :Validations, 'mongo_mapper/plugins/validations'
end
end
Expand Down
87 changes: 87 additions & 0 deletions lib/mongo_mapper/plugins/modifiers.rb
@@ -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
21 changes: 21 additions & 0 deletions lib/mongo_mapper/plugins/timestamps.rb
@@ -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
14 changes: 14 additions & 0 deletions lib/mongo_mapper/plugins/userstamps.rb
@@ -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

0 comments on commit 0d78cc0

Please sign in to comment.