Skip to content

Commit

Permalink
Add Bothback and Fluent extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Stokes committed Jan 5, 2011
1 parent 60c3929 commit 946442f
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/deferrable_gratification.rb
@@ -1,5 +1,7 @@
require File.join(File.dirname(__FILE__), *%w[deferrable_gratification bothback])
require File.join(File.dirname(__FILE__), *%w[deferrable_gratification combinators])
require File.join(File.dirname(__FILE__), *%w[deferrable_gratification default_deferrable])
require File.join(File.dirname(__FILE__), *%w[deferrable_gratification fluent])
require File.join(File.dirname(__FILE__), *%w[deferrable_gratification primitives])

module DeferrableGratification
Expand Down
15 changes: 15 additions & 0 deletions lib/deferrable_gratification/bothback.rb
@@ -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
34 changes: 34 additions & 0 deletions lib/deferrable_gratification/fluent.rb
@@ -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
41 changes: 41 additions & 0 deletions spec/deferrable_gratification/bothback_spec.rb
@@ -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
75 changes: 75 additions & 0 deletions spec/deferrable_gratification/fluent_spec.rb
@@ -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

0 comments on commit 946442f

Please sign in to comment.