Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sam Stokes
committed
Jan 5, 2011
1 parent
60c3929
commit 946442f
Showing
5 changed files
with
167 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module DeferrableGratification | ||
# Allows registering a 'bothback' that will be fired on either success or | ||
# failure, analogous to the +ensure+ clause of a +begin+/+rescue+ block. | ||
# | ||
# Include this into a class that has already included +Deferrable+. | ||
module Bothback | ||
# Register +block+ to be called on either success or failure. | ||
# This is just a shorthand for registering the same +block+ as both a | ||
# callback and an errback. | ||
def bothback(&block) | ||
callback(&block) | ||
errback(&block) | ||
end | ||
end | ||
end |
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,34 @@ | ||
module DeferrableGratification | ||
# Allows JQuery-style fluent syntax for registering several callbacks and | ||
# errbacks on the same Deferrable. e.g. | ||
# | ||
# DeferrableMonkeyShaver.new(monkey). | ||
# callback { puts "Monkey is shaved" }. | ||
# callback { monkey.entertain! }. | ||
# errback {|e| puts "Unable to shave monkey! #{e}" }. | ||
# errback {|_| monkey.terminate! }. | ||
# shave | ||
# | ||
# Include this into a class that has already included +Deferrable+. | ||
module Fluent | ||
# Register +block+ to be called on success. | ||
# | ||
# @return +self+ | ||
# | ||
# @see EventMachine::Deferrable#callback | ||
def callback(&block) | ||
super(&block) | ||
self | ||
end | ||
|
||
# Register +block+ to be called on failure. | ||
# | ||
# @return +self+ | ||
# | ||
# @see EventMachine::Deferrable#errback | ||
def errback(&block) | ||
super(&block) | ||
self | ||
end | ||
end | ||
end |
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,41 @@ | ||
require 'deferrable_gratification' | ||
|
||
require 'eventmachine' | ||
require 'em/deferrable' | ||
|
||
describe DeferrableGratification::Bothback do | ||
class BothbackDeferrable < EventMachine::DefaultDeferrable | ||
include DeferrableGratification::Bothback | ||
end | ||
|
||
subject { BothbackDeferrable.new } | ||
|
||
it 'should allow registering a "bothback"' do | ||
lambda do | ||
subject.bothback { release_lock } | ||
end.should_not raise_error | ||
end | ||
|
||
describe 'after registering a bothback' do | ||
before do | ||
@called = false | ||
subject.bothback { @called = true } | ||
end | ||
|
||
describe 'on success' do | ||
before { subject.succeed :yay } | ||
|
||
it 'should call the bothback' do | ||
@called.should be_true | ||
end | ||
end | ||
|
||
describe 'on failure' do | ||
before { subject.fail :boo } | ||
|
||
it 'should call the bothback' do | ||
@called.should be_true | ||
end | ||
end | ||
end | ||
end |
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,75 @@ | ||
require 'deferrable_gratification' | ||
|
||
require 'eventmachine' | ||
require 'em/deferrable' | ||
|
||
describe DeferrableGratification::Fluent do | ||
class FluentDeferrable < EventMachine::DefaultDeferrable | ||
include DeferrableGratification::Fluent | ||
end | ||
|
||
subject { FluentDeferrable.new } | ||
|
||
it 'should allow fluently adding multiple callbacks' do | ||
lambda do | ||
subject. | ||
callback {|result| process(result) }. | ||
callback { log_action }. | ||
callback {|result| puts result } | ||
end.should_not raise_error | ||
end | ||
|
||
it 'should allow fluently adding multiple errbacks' do | ||
lambda do | ||
subject. | ||
errback {|error| report(error) }. | ||
errback { count_error } | ||
end.should_not raise_error | ||
end | ||
|
||
it 'should allow fluently mixing callbacks and errbacks' do | ||
lambda do | ||
subject. | ||
callback {|result| process(result) }. | ||
errback { count_error }. | ||
callback { log_action } | ||
end.should_not raise_error | ||
end | ||
|
||
describe 'after fluently registering a bunch of callbacks and errbacks' do | ||
before do | ||
@callsback = [] | ||
@errsback = [] | ||
|
||
subject. | ||
callback { @callsback << 'called' }. | ||
callback {|result| @callsback << result }. | ||
errback { @errsback << 'errored' }. | ||
errback {|error| @errsback << error } | ||
end | ||
|
||
describe 'on success' do | ||
before { subject.succeed :yay } | ||
|
||
it 'should call all the callbacks' do | ||
@callsback.should == ['called', :yay] | ||
end | ||
|
||
it 'should call none of the errbacks' do | ||
@errsback.should be_empty | ||
end | ||
end | ||
|
||
describe 'on failure' do | ||
before { subject.fail :boo } | ||
|
||
it 'should call none of the callbacks' do | ||
@callsback.should be_empty | ||
end | ||
|
||
it 'should call all the errbacks' do | ||
@errsback.should == ['errored', :boo] | ||
end | ||
end | ||
end | ||
end |