lib/hanami/interactor.rb in hanami-utils-2.0.0.alpha1 vs lib/hanami/interactor.rb in hanami-utils-2.0.0.alpha2
- old
+ new
@@ -17,18 +17,18 @@
#
# @since 0.3.5
# @api private
#
# @see Hanami::Interactor::Result#respond_to_missing?
- METHODS = ::Hash[initialize: true,
- success?: true,
+ METHODS = ::Hash[initialize: true,
+ success?: true,
successful?: true,
- failure?: true,
- fail!: true,
- prepare!: true,
- errors: true,
- error: true].freeze
+ failure?: true,
+ fail!: true,
+ prepare!: true,
+ errors: true,
+ error: true].freeze
# Initialize a new result
#
# @param payload [Hash] a payload to carry on
#
@@ -40,32 +40,32 @@
@payload = payload
@errors = []
@success = true
end
- # Check if the current status is successful
+ # Checks if the current status is successful
#
# @return [TrueClass,FalseClass] the result of the check
#
# @since 0.8.1
def successful?
@success && errors.empty?
end
# @since 0.3.5
- alias success? successful?
+ alias_method :success?, :successful?
- # Check if the current status is not successful
+ # Checks if the current status is not successful
#
# @return [TrueClass,FalseClass] the result of the check
#
# @since 0.9.2
def failure?
!successful?
end
- # Force the status to be a failure
+ # Forces the status to be a failure
#
# @since 0.3.5
def fail!
@success = false
end
@@ -104,11 +104,11 @@
# @see Hanami::Interactor#error!
def error
errors.first
end
- # Prepare the result before to be returned
+ # Prepares the result before to be returned
#
# @param payload [Hash] an updated payload
#
# @since 0.3.5
# @api private
@@ -179,14 +179,22 @@
#
# def call
# # ...
# end
# end
- def initialize(*args)
- super
- ensure
- @__result = ::Hanami::Interactor::Result.new
+ if RUBY_VERSION >= "3.0"
+ def initialize(*args, **kwargs)
+ super
+ ensure
+ @__result = ::Hanami::Interactor::Result.new
+ end
+ else
+ def initialize(*args)
+ super
+ ensure
+ @__result = ::Hanami::Interactor::Result.new
+ end
end
# Triggers the operation and return a result.
#
# All the instance variables will be available in the result.
@@ -351,37 +359,61 @@
#
# # Method #call is not defined
# end
#
# Signup.new.call # => NoMethodError
- def call(*args)
- @__result = ::Hanami::Interactor::Result.new
- _call(*args) { super }
+ if RUBY_VERSION >= "3.0"
+ def call(*args, **kwargs)
+ @__result = ::Hanami::Interactor::Result.new
+ _call(*args, **kwargs) { super }
+ end
+ else
+ def call(*args)
+ @__result = ::Hanami::Interactor::Result.new
+ _call(*args) { super }
+ end
end
private
# @api private
# @since 1.1.0
- def _call(*args)
- catch :fail do
- validate!(*args)
- yield
+ if RUBY_VERSION >= "3.0"
+ def _call(*args, **kwargs)
+ catch :fail do
+ validate!(*args, **kwargs)
+ yield
+ end
+
+ _prepare!
end
+ else
+ def _call(*args)
+ catch :fail do
+ validate!(*args)
+ yield
+ end
- _prepare!
+ _prepare!
+ end
end
# @since 1.1.0
- def validate!(*args)
- fail! unless valid?(*args)
+ if RUBY_VERSION >= "3.0"
+ def validate!(*args, **kwargs)
+ fail! unless valid?(*args, **kwargs)
+ end
+ else
+ def validate!(*args)
+ fail! unless valid?(*args)
+ end
end
end
private
- # Check if proceed with <tt>#call</tt> invokation.
+ # Checks if proceed with <tt>#call</tt> invocation.
# By default it returns <tt>true</tt>.
#
# Developers can override it.
#
# @return [TrueClass,FalseClass] the result of the check
@@ -389,11 +421,11 @@
# @since 0.3.5
def valid?(*)
true
end
- # Fail and interrupt the current flow.
+ # Fails and interrupts the current flow.
#
# @since 0.3.5
#
# @example
# require 'hanami/interactor'
@@ -428,11 +460,11 @@
def fail!
@__result.fail!
throw :fail
end
- # Log an error without interrupting the flow.
+ # Logs an error without interrupting the flow.
#
# When used, the returned result won't be successful.
#
# @param message [String] the error message
#
@@ -483,11 +515,11 @@
def error(message)
@__result.add_error message
false
end
- # Log an error AND interrupting the flow.
+ # Logs an error and interrupts the flow.
#
# When used, the returned result won't be successful.
#
# @param message [String] the error message
#
@@ -579,19 +611,19 @@
else
prepend Hanami::Interactor::Interface
end
end
- # Expose local instance variables into the returning value of <tt>#call</tt>
+ # Exposes local instance variables into the returning value of <tt>#call</tt>
#
# @param instance_variable_names [Symbol,Array<Symbol>] one or more instance
# variable names
#
# @since 0.5.0
#
# @see Hanami::Interactor::Result
#
- # @example Expose instance variable
+ # @example Exposes instance variable
#
# class Signup
# include Hanami::Interactor
# expose :user
#