Permalink
Browse files

Implement RCCounter#decrement_by w/guard & block

In Smalltalk RcCounter there is both decrementBy: and
decrementBy:ifLessThan:thenExecute:. Wrap them both up
as #decrement_by in the ruby version
  • Loading branch information...
jc00ke committed Oct 21, 2012
1 parent e7bda33 commit 22887cb98164c58f5bbede8490b45773b7ba4455
Showing with 47 additions and 5 deletions.
  1. +12 −1 lib/ruby/1.8/maglev/rccounter.rb
  2. +35 −4 spec/maglev/reducedconflict/counter/decrement_by_spec.rb
@@ -21,6 +21,17 @@ class RCCounter
primitive 'increment', 'increment'
primitive 'increment_by', 'incrementBy:'
primitive 'decrement', 'decrement'
- primitive 'decrement_by', 'decrementBy:'
+ primitive '__decrement_by', 'decrementBy:'
primitive 'decrement_if_negative&', 'decrementIfNegative:'
+ primitive '__decrement_by_if_less_than&', 'decrementBy:ifLessThan:thenExecute:'
+
+ def decrement_by(amount, guard=nil)
+ if guard && block_given?
+ __decrement_by_if_less_than(amount, guard) do
+ yield
+ end
+ else
+ __decrement_by(amount)
+ end
+ end
end
@@ -3,10 +3,41 @@
describe "RCCounter#decrement_by" do
- it "subtracts x from the counter" do
- counter = RCCounter.new
- counter.decrement_by 10
- counter.value.should == -10
+ describe "with only amount" do
+ it "subtracts x from the counter" do
+ counter = RCCounter.new
+ counter.decrement_by 10
+ counter.value.should == -10
+ end
+ end
+
+ describe "with amount, guard and block" do
+ before do
+ @counter = RCCounter.new
+ end
+
+ describe "when guard is hit" do
+ it "executes the block" do
+ @counter.decrement_by(10, -1) do
+ 'block executed'
+ end.should == 'block executed'
+ end
+ end
+
+ describe "when guard is missed" do
+ it "decrements the value" do
+ @counter.decrement_by(5, -7) do
+ "won't get executed"
+ end
+ @counter.value.should == -5
+ end
+
+ it "returns self" do
+ @counter.decrement_by(5, -7) do
+ "won't get executed"
+ end.should equal(@counter)
+ end
+ end
end
end

0 comments on commit 22887cb

Please sign in to comment.