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

errors[R]
interactor_name[R]
payload[R]
results[R]
state[R]

Public Class Methods

execute(payload = {}) click to toggle source

Execute the operation with given payload.

# File lib/action_interactor/base.rb, line 111
def execute(payload = {})
  new(payload).tap(&:execute)
end
execute!(payload = {}) click to toggle source

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
new(payload = {}) click to toggle source

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

abort!() click to toggle source

Mark the operation as aborted.

# File lib/action_interactor/base.rb, line 91
def abort!
  state.aborted!
end
aborted?() click to toggle source

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() click to toggle source

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!() click to toggle source

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
fail!()
Alias for: failure!
failure!() click to toggle source

Mask the operation as failure.

# File lib/action_interactor/base.rb, line 103
def failure!
  state.failure!
end
Also aliased as: fail!
failure?() click to toggle source

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
finished?() click to toggle source

Returns `true` if marked as finished otherwise `false`.

# File lib/action_interactor/base.rb, line 59
def finished?
  state.successful? || state.failure?
end
reset!() click to toggle source

Reset statuses.

# File lib/action_interactor/base.rb, line 86
def reset!
  @state = State.new
end
success!()
Alias for: successful!
success?()
Alias for: successful?
successful!() click to toggle source

Mark the operation as successful.

# File lib/action_interactor/base.rb, line 96
def successful!
  state.successful!
end
Also aliased as: success!
successful?() click to toggle source

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
Also aliased as: success?
unfinished?() click to toggle source

Returns `true` if not marked as finished otherwise `false`.

# File lib/action_interactor/base.rb, line 64
def unfinished?
  !finished?
end

Private Instance Methods

underscore(name) click to toggle source
# 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