Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

.inc() function (but poorly implemented)

  • Loading branch information...
commit 1a2617c966cfc05b68a92aae598de469386da89a 1 parent c636e82
@SFEley SFEley authored
View
2  README.markdown
@@ -29,7 +29,7 @@ We got 'em. Candy pieces can contain each other recursively, to any arbitrary d
seafood: 'Maryland blue crabs',
scotch: ['Glenmorangie Port Wood Finish',
'Balvenie Single Barrel']}
- me.spouse = Person.embed(first_name: 'Anna', eyes: :blue)
+ me.spouse = Person.piece(first_name: 'Anna', eyes: :blue)
me.spouse.eyes # => :blue
me.favorites.scotch[1] # => 'Balvenie Single Barrel'
View
2  lib/candy/array.rb
@@ -48,7 +48,7 @@ def <<(val)
# (Thus supporting real-time concurrency for queue-like behavior.)
def shift(n=1)
doc = @__candy_parent.collection.find_and_modify query: {"_id" => @__candy_parent.id}, update: {'$pop' => {@__candy_parent_key => -1}}, new: false
- @__candy = doc[@__candy_parent_key.to_s]
+ @__candy = from_candy(doc[@__candy_parent_key.to_s])
@__candy.shift
end
View
7 lib/candy/piece.rb
@@ -195,6 +195,13 @@ def set(fields)
operate :set, fields
end
+ # Increments the specified field by the (optional) specified amount and returns the new value.
+ def inc(field, value=1)
+ result = collection.find_and_modify query: {"_id" => id}, update: {'$inc' => {Wrapper.wrap_key(field) => value}}, new: true
+ @__candy = from_candy(result)
+ candy[field]
+ end
+
# A generic updater that performs the atomic operation specified on a value nested arbitrarily deeply.
#
def operate(operator, fields)
View
16 spec/candy/piece_spec.rb
@@ -205,6 +205,22 @@ class Jawbreaker
@verifier.count.should == 3
Zagnut.crunchy(:not_quite).color.should == 'brown'
end
+
+ it "can increment a value simply" do
+ @this.inc(:ounces).should == 18
+ @verifier.find_one(ounces: 18)["crunchy"].should == :very
+ end
+
+ it "can increment a value by a specified positive amount" do
+ @this.inc(:ounces, 5).should == 22
+ @verifier.find_one(ounces: 22)["crunchy"].should == :very
+ end
+
+ it "can increment a value by a specified negative amount" do
+ @this.inc(:ounces, -5).should == 12
+ @verifier.find_one(ounces: 12)["crunchy"].should == :very
+ end
+
end
describe "embedding" do
Please sign in to comment.
Something went wrong with that request. Please try again.