Skip to content

Commit

Permalink
Add method #failure? to Interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrachev committed Jul 16, 2016
1 parent 647f755 commit dd1109b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
19 changes: 18 additions & 1 deletion lib/hanami/interactor.rb
Expand Up @@ -17,7 +17,13 @@ class Result < Utils::BasicObject
# @api private
#
# @see Hanami::Interactor::Result#respond_to_missing?
METHODS = ::Hash[initialize: true, success?: true, fail!: true, prepare!: true, errors: true, error: true].freeze
METHODS = ::Hash[initialize: true,
success?: true,
failure?: true,
fail!: true,
prepare!: true,
errors: true,
error: true].freeze

# Initialize a new result
#
Expand All @@ -42,6 +48,15 @@ def success?
@success && errors.empty?
end

# Check if the current status is not successful
#
# @return [TrueClass,FalseClass] the result of the check
#
# @since 0.8.0
def failure?
!success?
end

# Force the status to be a failure
#
# @since 0.3.5
Expand Down Expand Up @@ -202,6 +217,7 @@ def initialize(*args)
# end
#
# result = Signup.new(name: 'Luca').call
# result.failure? # => false
# result.success? # => true
#
# result.user # => #<User:0x007fa311105778 @id=1 @name="Luca">
Expand Down Expand Up @@ -233,6 +249,7 @@ def initialize(*args)
#
# result = Signup.new(name: nil).call
# result.success? # => false
# result.failure? # => true
#
# result.user # => #<User:0x007fa311105778 @id=nil @name="Luca">
#
Expand Down
15 changes: 9 additions & 6 deletions test/interactor_test.rb
Expand Up @@ -192,12 +192,12 @@ def initialize(_user, params)

it 'exposes a convenient API for handling failures' do
result = Signup.new({}).call
assert !result.success?, "Expected `result' to NOT be successful"
assert result.failure?, "Expected `result' to NOT be successful"
end

it "doesn't invoke it if the preconditions are failing" do
result = Signup.new(force_failure: true).call
assert !result.success?, "Expected `result' to NOT be successful"
assert result.failure?, "Expected `result' to NOT be successful"
end

it "raises error when #call isn't implemented" do
Expand Down Expand Up @@ -225,7 +225,7 @@ def initialize(_user, params)
describe '#error' do
it "isn't successful" do
result = ErrorInteractor.new.call
assert !result.success?, "Expected `result' to not be successful"
assert result.failure?, "Expected `result' to not be successful"
end

it 'accumulates errors' do
Expand All @@ -251,7 +251,7 @@ def initialize(_user, params)
describe '#error!' do
it "isn't successful" do
result = ErrorBangInteractor.new.call
assert !result.success?, "Expected `result' to not be successful"
assert result.failure?, "Expected `result' to not be successful"
end

it 'stops at the first error' do
Expand Down Expand Up @@ -290,7 +290,7 @@ def initialize(_user, params)
it "isn't successful" do
result = Hanami::Interactor::Result.new
result.add_error 'There was a problem'
assert !result.success?, "Expected `result' to NOT be successful"
assert result.failure?, "Expected `result' to NOT be successful"
end
end
end
Expand All @@ -300,7 +300,7 @@ def initialize(_user, params)
result = Hanami::Interactor::Result.new
result.fail!

assert !result.success?, "Expected `result' to NOT be successful"
assert result.failure?, "Expected `result' to NOT be successful"
end
end

Expand Down Expand Up @@ -363,6 +363,9 @@ def initialize(_user, params)

assert result.respond_to?(:success?), "Expected `result' to respond to `#success?'"
assert result.respond_to?('success?'), "Expected `result' to respond to `#success?'"

assert result.respond_to?(:failure?), "Expected `result' to respond to `#failure?'"
assert result.respond_to?('failure?'), "Expected `result' to respond to `#failure?'"
end

it 'returns true for methods derived from payload' do
Expand Down

0 comments on commit dd1109b

Please sign in to comment.