lib/hanami/interactor.rb in hanami-utils-0.8.0 vs lib/hanami/interactor.rb in hanami-utils-0.9.0
- old
+ new
@@ -15,11 +15,18 @@
#
# @since 0.3.5
# @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,
+ successful?: true,
+ failing?: true,
+ fail!: true,
+ prepare!: true,
+ errors: true,
+ error: true].freeze
# Initialize a new result
#
# @param payload [Hash] a payload to carry on
#
@@ -35,15 +42,27 @@
# Check if the current status is successful
#
# @return [TrueClass,FalseClass] the result of the check
#
- # @since 0.3.5
- def success?
+ # @since 0.8.1
+ def successful?
@success && errors.empty?
end
+ # @since 0.3.5
+ alias success? successful?
+
+ # Check if the current status is not successful
+ #
+ # @return [TrueClass,FalseClass] the result of the check
+ #
+ # @since 0.8.1
+ def failing?
+ !successful?
+ end
+
# Force the status to be a failure
#
# @since 0.3.5
def fail!
@success = false
@@ -200,11 +219,12 @@
# @user = UserRepository.persist(@user)
# end
# end
#
# result = Signup.new(name: 'Luca').call
- # result.success? # => true
+ # result.failing? # => false
+ # result.successful? # => true
#
# result.user # => #<User:0x007fa311105778 @id=1 @name="Luca">
# result.params # => { :name=>"Luca" }
# result.foo # => raises NoMethodError
#
@@ -230,11 +250,12 @@
# @params.valid?
# end
# end
#
# result = Signup.new(name: nil).call
- # result.success? # => false
+ # result.successful? # => false
+ # result.failing? # => true
#
# result.user # => #<User:0x007fa311105778 @id=nil @name="Luca">
#
# @example Bad usage
# require 'hanami/interactor'
@@ -297,11 +318,11 @@
# fail!
# end
# end
#
# result = CreateEmailTest.new(account_id: 1).call
- # result.success? # => false
+ # result.successful? # => false
def fail!
@__result.fail!
throw :fail
end
@@ -349,11 +370,11 @@
# @logger << __method__
# end
# end
#
# result = CreateRecord.new.call
- # result.success? # => false
+ # result.successful? # => false
#
# result.errors # => ["Prepare data error", "Persist error"]
# result.logger # => [:prepare_data!, :persist!, :sync!]
def error(message)
@__result.add_error message
@@ -403,10 +424,10 @@
# @logger << __method__
# end
# end
#
# result = CreateRecord.new.call
- # result.success? # => false
+ # result.successful? # => false
#
# result.errors # => ["Prepare data error", "Persist error"]
# result.logger # => [:prepare_data!, :persist!]
def error!(message)
error(message)