Skip to content

Commit

Permalink
Merge pull request #3 from Yesware/failure-value
Browse files Browse the repository at this point in the history
Failure value
  • Loading branch information
Tim Perkins committed Jun 2, 2014
2 parents 5b872b0 + 316abe7 commit 96ef6b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# deferrable_gratification changes

## v1.0.0
- Remove the `Fluent` module as the same syntax is available in
`EventMachine` 1.0.3.
- Add `failure_value` to `DeferrableGratification::Primitives` to fail without
an exception. Any value may be specified, but the main use is to fail with
only an error string.
1 change: 0 additions & 1 deletion lib/deferrable_gratification/default_deferrable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module DeferrableGratification
# Barebones Deferrable that includes the {DeferrableGratification}
# extensions:
#
# @see Fluent
# @see Bothback
# @see Combinators
class DefaultDeferrable
Expand Down
10 changes: 10 additions & 0 deletions lib/deferrable_gratification/primitives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def failure(exception_class_or_message, message_or_nil = nil)
end
end

# Return a Deferrable which immediately fails with the specified value.
# This is useful for failing with the string for an error message instead
# of an exception.
#
# @param value [Object] The value to fail with.
# @return [EM::Deferrable] A deferrable that fails with the specified value.
def failure_value(value)
blank.tap { |d| d.fail(value) }
end

# Return a completely uninteresting Deferrable.
def blank
DeferrableGratification::DefaultDeferrable.new
Expand Down
29 changes: 29 additions & 0 deletions spec/deferrable_gratification/primitives_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,33 @@
it { should fail_with(RangeError, 'you shall not pass!') }
end
end

describe '.failure_value' do
describe 'DG.failure_value with a string' do
subject { DG.failure_value('i am a string') }

it 'fails with the specified string' do
error = nil
subject.errback do |err|
error = err
end

error.should == 'i am a string'
end
end

describe 'DG.failure_value with an arbitrary object' do
let(:obj) { double(Object) }
subject { DG.failure_value(obj) }

it 'fails with the specified object' do
error = nil
subject.errback do |err|
error = err
end

error.should equal obj
end
end
end
end

0 comments on commit 96ef6b3

Please sign in to comment.