Skip to content

Commit

Permalink
now passing a proper separate options argument for modifier operation…
Browse files Browse the repository at this point in the history
…s instead of forcing it in the keys argument hash
  • Loading branch information
hamin committed Mar 4, 2012
1 parent f4ef5fb commit 62d23e9
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 125 deletions.
52 changes: 29 additions & 23 deletions lib/mongo_mapper/plugins/modifiers.rb
Expand Up @@ -65,17 +65,23 @@ def pop(*args)
private
def modifier_update(modifier, args)
criteria, updates, options = criteria_and_keys_from_args(args)
if options[:upsert].nil? && options[:safe].nil?
collection.update(criteria, {modifier => updates}, :multi => true)
else
if options
collection.update(criteria, {modifier => updates}, options.merge(:multi => true))
else
collection.update(criteria, {modifier => updates}, :multi => true)
end
end

def criteria_and_keys_from_args(args)
popped_args = args.pop
options = { :upsert => popped_args[:upsert], :safe => popped_args[:safe] }.reject{|k,v| v.nil?}
keys = popped_args.reject{|k,v| [:upsert, :safe, :multi].include?(k)}
if popped_args.nil? || (popped_args[:upsert].nil? && popped_args[:safe].nil?)
options = nil
keys = popped_args.nil? ? args.pop : popped_args
else
options = { :upsert => popped_args[:upsert], :safe => popped_args[:safe] }.reject{|k,v| v.nil?}
keys = args.pop
end

criteria = args[0].is_a?(Hash) ? args[0] : {:id => args}
[criteria_hash(criteria).to_hash, keys, options]
end
Expand All @@ -85,41 +91,41 @@ def unset(*keys)
self.class.unset(id, *keys)
end

def increment(hash)
self.class.increment(id, hash)
def increment(hash,options=nil)
self.class.increment(id, hash, options)
end

def decrement(hash)
self.class.decrement(id, hash)
def decrement(hash,options=nil)
self.class.decrement(id, hash, options)
end

def set(hash)
self.class.set(id, hash)
def set(hash,options=nil)
self.class.set(id, hash, options)
end

def push(hash)
self.class.push(id, hash)
def push(hash,options=nil)
self.class.push(id, hash, options)
end

def push_all(hash)
self.class.push_all(id, hash)
def push_all(hash,options=nil)
self.class.push_all(id, hash, options)
end

def pull(hash)
self.class.pull(id, hash)
def pull(hash,options=nil)
self.class.pull(id, hash, options)
end

def pull_all(hash)
self.class.pull_all(id, hash)
def pull_all(hash,options=nil)
self.class.pull_all(id, hash, options)
end

def add_to_set(hash)
self.class.push_uniq(id, hash)
def add_to_set(hash,options=nil)
self.class.push_uniq(id, hash, options)
end
alias push_uniq add_to_set

def pop(hash)
self.class.pop(id, hash)
def pop(hash,options=nil)
self.class.pop(id, hash, options)
end
end
end
Expand Down

0 comments on commit 62d23e9

Please sign in to comment.