Skip to content

Commit

Permalink
Moving the $inc atomic modifier over with its brothers and sisters.
Browse files Browse the repository at this point in the history
- Refactored inc to truly be atomic like the others (remove dirty
  changes) and changed it's internal structure to match the other
  operations.
  • Loading branch information
durran committed Mar 4, 2011
1 parent 7e6f96f commit 7ef9a77
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 213 deletions.
1 change: 0 additions & 1 deletion lib/mongoid.rb
Expand Up @@ -69,7 +69,6 @@
require "mongoid/keys"
require "mongoid/logger"
require "mongoid/matchers"
require "mongoid/modifiers"
require "mongoid/multi_parameter_attributes"
require "mongoid/multi_database"
require "mongoid/named_scope"
Expand Down
1 change: 0 additions & 1 deletion lib/mongoid/components.rb
Expand Up @@ -29,7 +29,6 @@ module Components #:nodoc
include Mongoid::JSON
include Mongoid::Keys
include Mongoid::Matchers
include Mongoid::Modifiers
include Mongoid::NamedScope
include Mongoid::NestedAttributes
include Mongoid::Paths
Expand Down
25 changes: 0 additions & 25 deletions lib/mongoid/modifiers.rb

This file was deleted.

18 changes: 0 additions & 18 deletions lib/mongoid/modifiers/command.rb

This file was deleted.

24 changes: 0 additions & 24 deletions lib/mongoid/modifiers/inc.rb

This file was deleted.

21 changes: 20 additions & 1 deletion lib/mongoid/persistence/atomic.rb
@@ -1,6 +1,7 @@
# encoding: utf-8
require "mongoid/persistence/atomic/operation"
require "mongoid/persistence/atomic/add_to_set"
require "mongoid/persistence/atomic/inc"
require "mongoid/persistence/atomic/pull_all"
require "mongoid/persistence/atomic/push"

Expand Down Expand Up @@ -30,6 +31,24 @@ def add_to_set(field, value, options = {})
AddToSet.new(self, field, value, options).persist
end

# Performs an atomic $inc of the provided value on the supplied
# field. If the field does not exist it will be initialized as
# the provided value.
#
# @example Increment a field.
# person.inc(:score, 2)
#
# @param [ Symbol ] field The name of the field.
# @param [ Integer ] value The value to increment.
# @param [ Hash ] options The mongo persistence options.
#
# @return [ Array<Object> ] The new value of the field.
#
# @since 2.0.0
def inc(field, value, options = {})
Inc.new(self, field, value, options).persist
end

# Performs an atomic $pullAll of the provided value on the supplied
# field. If the field does not exist it will be initialized as an
# empty array.
Expand All @@ -38,7 +57,7 @@ def add_to_set(field, value, options = {})
# person.pull_all(:aliases, [ "Bond", "James" ])
#
# @param [ Symbol ] field The name of the field.
# @param [ Object ] value The value to push.
# @param [ Array<Object> ] value The values to pull.
# @param [ Hash ] options The mongo persistence options.
#
# @return [ Array<Object> ] The new value of the field.
Expand Down
42 changes: 42 additions & 0 deletions lib/mongoid/persistence/atomic/inc.rb
@@ -0,0 +1,42 @@
# encoding: utf-8
module Mongoid #:nodoc:
module Persistence #:nodoc:
module Atomic #:nodoc:

# This class provides atomic $inc behaviour.
class Inc < Operation

# Sends the atomic $inc operation to the database.
#
# @example Persist the new values.
# inc.persist
#
# @return [ Object ] The new integer value.
#
# @since 2.0.0
def persist
current = document[field] || 0
document[field] = current + value
document[field].tap do
document.collection.update(document._selector, operation, options)
document.changes.delete(field.to_s)
end
end

private

# Get the atomic operation to perform.
#
# @example Get the operation.
# inc.operation
#
# @return [ Hash ] The $push operation for the field and addition.
#
# @since 2.0.0
def operation
{ "$inc" => { field => value } }
end
end
end
end
end
36 changes: 0 additions & 36 deletions spec/functional/mongoid/modifiers/inc_spec.rb

This file was deleted.

88 changes: 88 additions & 0 deletions spec/functional/mongoid/persistence/atomic/inc_spec.rb
@@ -0,0 +1,88 @@
require "spec_helper"

describe Mongoid::Persistence::Atomic::Inc do

before do
Person.delete_all
end

describe "#inc" do

let(:person) do
Person.create(:ssn => "777-66-1010", :age => 100)
end

let(:reloaded) do
person.reload
end

context "when incrementing a field with a value" do

let!(:inced) do
person.inc(:age, 2)
end

it "increments by the provided value" do
person.age.should == 102
end

it "returns the new value" do
inced.should == 102
end

it "persists the changes" do
reloaded.age.should == 102
end

it "resets the dirty attributes" do
person.changes["age"].should be_nil
end
end

context "when incrementing a nil field" do

let!(:inced) do
person.inc(:score, 2)
end

it "sets the value to the provided number" do
person.score.should == 2
end

it "returns the new value" do
inced.should == 2
end

it "persists the changes" do
reloaded.score.should == 2
end

it "resets the dirty attributes" do
person.changes["score"].should be_nil
end
end

context "when incrementing a non existant field" do

let!(:inced) do
person.inc(:high_score, 5)
end

it "sets the value to the provided number" do
person.high_score.should == 5
end

it "returns the new value" do
inced.should == 5
end

it "persists the changes" do
reloaded.high_score.should == 5
end

it "resets the dirty attributes" do
person.changes["high_score"].should be_nil
end
end
end
end
77 changes: 0 additions & 77 deletions spec/unit/mongoid/modifiers/inc_spec.rb

This file was deleted.

0 comments on commit 7ef9a77

Please sign in to comment.