forked from mongodb/mongoid
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving the $inc atomic modifier over with its brothers and sisters.
- 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
Showing
13 changed files
with
260 additions
and
213 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 |
This file was deleted.
Oops, something went wrong.
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,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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.