Skip to content

Commit

Permalink
Moved modifiers to plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Mar 2, 2010
1 parent 687311b commit 7784660
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 89 deletions.
78 changes: 1 addition & 77 deletions lib/mongo_mapper/document.rb
Expand Up @@ -17,6 +17,7 @@ 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
Expand Down Expand Up @@ -143,47 +144,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 @@ -258,18 +218,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 +345,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 Down
1 change: 1 addition & 0 deletions lib/mongo_mapper/plugins.rb
Expand Up @@ -20,6 +20,7 @@ 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'
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
24 changes: 12 additions & 12 deletions test/functional/test_modifiers.rb
Expand Up @@ -10,14 +10,14 @@ def setup
key :tags, Array
end
end

def assert_page_counts(page, day_count, week_count, month_count)
page.reload
page.day_count.should == day_count
page.week_count.should == week_count
page.month_count.should == month_count
end

context "using class methods" do
should "be able to increment with criteria and modifier hashes" do
page = @page_class.create(:title => 'Home')
Expand All @@ -30,7 +30,7 @@ def assert_page_counts(page, day_count, week_count, month_count)
assert_page_counts page, 1, 2, 3
assert_page_counts page2, 1, 2, 3
end

should "be able to increment with ids and modifier hash" do
page = @page_class.create(:title => 'Home')
page2 = @page_class.create(:title => 'Home')
Expand Down Expand Up @@ -251,32 +251,32 @@ def assert_page_counts(page, day_count, week_count, month_count)
page.tags.should == %w(bar)
end
end
context "using instance methods" do

context "using instance methods" do
should "be able to increment with modifier hashes" do
page = @page_class.create

page.increment({:day_count => 1, :week_count => 2, :month_count => 3})

assert_page_counts page, 1, 2, 3
end

should "be able to decrement with modifier hashes" do
page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)

page.decrement({:day_count => 1, :week_count => 2, :month_count => 3})

assert_page_counts page, 0, 0, 0
end

should "always decrement when decrement is called whether number is positive or negative" do
page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)

page.decrement({:day_count => -1, :week_count => 2, :month_count => -3})

assert_page_counts page, 0, 0, 0
end

should "be able to set with modifier hashes" do
page = @page_class.create(:title => 'Home')

Expand All @@ -285,7 +285,7 @@ def assert_page_counts(page, day_count, week_count, month_count)
page.reload
page.title.should == 'Home Revised'
end

should "be able to push with modifier hashes" do
page = @page_class.create

Expand All @@ -294,7 +294,7 @@ def assert_page_counts(page, day_count, week_count, month_count)
page.reload
page.tags.should == %w(foo)
end

should "be able to pull with criteria and modifier hashes" do
page = @page_class.create(:tags => %w(foo bar))

Expand All @@ -318,5 +318,5 @@ def assert_page_counts(page, day_count, week_count, month_count)
page.tags.should == %w(foo)
end
end

end

0 comments on commit 7784660

Please sign in to comment.