class ActionInteractor::Composite

Attributes

interactors[R]

Action Interactor Composite

An interactor class which containing multiple interactors using the composite pattern.

It can be used for execute multiple operations and it will be marked as successful if all operations executed successful. (otherwise it will be marked as failure.)

Public Class Methods

new(payload = {}) click to toggle source

Initialize with payload and an array for containing interactors.

Calls superclass method ActionInteractor::Base::new
# File lib/action_interactor/composite.rb, line 16
def initialize(payload = {})
  super
  @interactors = []
end

Public Instance Methods

add(interactor) click to toggle source

Add an interactor to the interactors array.

# File lib/action_interactor/composite.rb, line 35
def add(interactor)
  interactors << interactor
end
delete(interactor) click to toggle source

Delete the interactor from the interactors array.

# File lib/action_interactor/composite.rb, line 40
def delete(interactor)
  interactors.delete(interactor)
end
execute() click to toggle source

Execute containing interactors' `execute` method with given payload.

# File lib/action_interactor/composite.rb, line 22
def execute
  return if finished?
  return failure! if payload.nil?

  interactors.each_with_index do |interactor, index|
    execute_sub_interactor(interactor, index)
    return failure! if interactor.failure?
  end

  successful!
end

Private Instance Methods

execute_sub_interactor(interactor, index) click to toggle source
# File lib/action_interactor/composite.rb, line 46
def execute_sub_interactor(interactor, index)
  interactor.execute
  if interactor.successful?
    interactor.results.each_pair do |attr_name, value|
      results.add(sub_attr_key(interactor, index, attr_name), value)
    end
  else interactor.failure?
    interactor.errors.each_pair do |attr_name, value|
      errors.add(sub_attr_key(interactor, index, attr_name), value)
    end
  end
end
sub_attr_key(interactor, index, attr_name) click to toggle source
# File lib/action_interactor/composite.rb, line 59
def sub_attr_key(interactor, index, attr_name)
  "#{interactor.interactor_name}_#{index}__#{attr_name}".to_sym
end