lib/hanami/interactor.rb in hanami-utils-1.3.8 vs lib/hanami/interactor.rb in hanami-utils-2.0.0.alpha1

- 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 - # Checks if the current status is successful + # Check 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_method :success?, :successful? + alias success? successful? - # Checks if the current status is not successful + # Check if the current status is not successful # # @return [TrueClass,FalseClass] the result of the check # # @since 0.9.2 def failure? !successful? end - # Forces the status to be a failure + # Force 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 - # Prepares the result before to be returned + # Prepare the result before to be returned # # @param payload [Hash] an updated payload # # @since 0.3.5 # @api private @@ -179,22 +179,14 @@ # # def call # # ... # end # end - 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 + def initialize(*args) + super + ensure + @__result = ::Hanami::Interactor::Result.new end # Triggers the operation and return a result. # # All the instance variables will be available in the result. @@ -359,61 +351,37 @@ # # # Method #call is not defined # end # # Signup.new.call # => NoMethodError - 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 + def call(*args) + @__result = ::Hanami::Interactor::Result.new + _call(*args) { super } end private # @api private # @since 1.1.0 - if RUBY_VERSION >= "3.0" - def _call(*args, **kwargs) - catch :fail do - validate!(*args, **kwargs) - yield - end - - _prepare! + def _call(*args) + catch :fail do + validate!(*args) + yield end - else - def _call(*args) - catch :fail do - validate!(*args) - yield - end - _prepare! - end + _prepare! end # @since 1.1.0 - if RUBY_VERSION >= "3.0" - def validate!(*args, **kwargs) - fail! unless valid?(*args, **kwargs) - end - else - def validate!(*args) - fail! unless valid?(*args) - end + def validate!(*args) + fail! unless valid?(*args) end end private - # Checks if proceed with <tt>#call</tt> invocation. + # Check if proceed with <tt>#call</tt> invokation. # By default it returns <tt>true</tt>. # # Developers can override it. # # @return [TrueClass,FalseClass] the result of the check @@ -421,11 +389,11 @@ # @since 0.3.5 def valid?(*) true end - # Fails and interrupts the current flow. + # Fail and interrupt the current flow. # # @since 0.3.5 # # @example # require 'hanami/interactor' @@ -460,11 +428,11 @@ def fail! @__result.fail! throw :fail end - # Logs an error without interrupting the flow. + # Log an error without interrupting the flow. # # When used, the returned result won't be successful. # # @param message [String] the error message # @@ -515,11 +483,11 @@ def error(message) @__result.add_error message false end - # Logs an error and interrupts the flow. + # Log an error AND interrupting the flow. # # When used, the returned result won't be successful. # # @param message [String] the error message # @@ -578,11 +546,11 @@ end # @since 0.5.0 # @api private def _exposures - Hash[].tap do |result| + ::Hash[].tap do |result| self.class.exposures.each do |name, ivar| result[name] = instance_variable_defined?(ivar) ? instance_variable_get(ivar) : nil end end end @@ -611,19 +579,19 @@ else prepend Hanami::Interactor::Interface end end - # Exposes local instance variables into the returning value of <tt>#call</tt> + # Expose 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 Exposes instance variable + # @example Expose instance variable # # class Signup # include Hanami::Interactor # expose :user #