class ActionInteractor::Base
Action Interactor Base¶ ↑
This is a base class for an interactor (data processing unit). It gets a payload (input) as an initialization parameter and execute some methods which is described in `execute` method. After that, the results can be obtained by `results` method. In Ruby on Rails, it can be used for doing some business logic like new user registration process. For example inserting user data in the database and creating a notification message, registering a job for sending the message.
class RegistrationInteractor < ActionInteractor::Base
def execute return failure! unless payload[:name] user = User.create!(name: payload[:name]) notiticaion = user.notifications.create!(name: 'Welcome') RegistrationNotificationJob.perform_later! results.add(:user, user) successful! end
end
Attributes
Public Class Methods
Execute the operation with given payload.
# File lib/action_interactor/base.rb, line 111 def execute(payload = {}) new(payload).tap(&:execute) end
Execute the operation with given payload. If there are some errors, ActionInteractor::ExeuctionError will be raised.
# File lib/action_interactor/base.rb, line 117 def execute!(payload = {}) new(payload).tap(&:execute!) end
Initialize with payload Errors
and Results
data and initial state will be set.
# File lib/action_interactor/base.rb, line 30 def initialize(payload = {}) @payload = payload @errors = payload[:errors] || Errors.new @results = payload[:results] || Results.new @state = payload[:state] || ExecutionState.new @interactor_name = payload[:interactor_name] || underscore(self.class.name) end
Public Instance Methods
Mark the operation as aborted.
# File lib/action_interactor/base.rb, line 91 def abort! state.aborted! end
Returns `true` if the operation was not successful and not finished otherwise `false`.
# File lib/action_interactor/base.rb, line 81 def aborted? state.aborted? end
Execute the operation with given payload. (Should be overridden.)
# File lib/action_interactor/base.rb, line 40 def execute # if the interactor already finished execution, do nothing. return if finished? # if contract is not satisfied= (ex: payload is nil), mark as failure. return failure! if payload.nil? # (Implement some codes for the operation.) # if finished execution, mark as successful. successful! end
Execute the operation with given payload. If there are some errors, ActionInteractor::ExeuctionError will be raised.
# File lib/action_interactor/base.rb, line 53 def execute! execute successful? || raise(ExecutionError.new("Failed to execute the interactor")) end
Mask the operation as failure.
# File lib/action_interactor/base.rb, line 103 def failure! state.failure! end
Returns `true` if not marked as success or there are some errors otherwise `false`.
# File lib/action_interactor/base.rb, line 76 def failure? !successful? end
Returns `true` if marked as finished otherwise `false`.
# File lib/action_interactor/base.rb, line 59 def finished? state.successful? || state.failure? end
Reset statuses.
# File lib/action_interactor/base.rb, line 86 def reset! @state = State.new end
Mark the operation as successful.
# File lib/action_interactor/base.rb, line 96 def successful! state.successful! end
Returns `true` if marked as success and there are no errors otherwise `false`.
# File lib/action_interactor/base.rb, line 69 def successful? state.successful? && @errors.empty? end
Returns `true` if not marked as finished otherwise `false`.
# File lib/action_interactor/base.rb, line 64 def unfinished? !finished? end
Private Instance Methods
# File lib/action_interactor/base.rb, line 124 def underscore(name) name.gsub(/::/, "__") .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr("-", "_") .downcase end