Class: ActionInteractor::Composite

Inherits:
Base
  • Object
show all
Defined in:
lib/action_interactor/composite.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#errors, #interactor_name, #payload, #results, #state

Instance Method Summary collapse

Methods inherited from Base

#abort!, #aborted?, execute, execute!, #execute!, #failure!, #failure?, #finished?, #reset!, #successful!, #successful?, #unfinished?

Constructor Details

#initialize(payload = {}) ⇒ Composite

Initialize with payload and an array for containing interactors.



16
17
18
19
# File 'lib/action_interactor/composite.rb', line 16

def initialize(payload = {})
  super
  @interactors = []
end

Instance Attribute Details

#interactorsObject (readonly)

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.)



13
14
15
# File 'lib/action_interactor/composite.rb', line 13

def interactors
  @interactors
end

Instance Method Details

#add(interactor) ⇒ Object

Add an interactor to the interactors array.



35
36
37
# File 'lib/action_interactor/composite.rb', line 35

def add(interactor)
  interactors << interactor
end

#delete(interactor) ⇒ Object

Delete the interactor from the interactors array.



40
41
42
# File 'lib/action_interactor/composite.rb', line 40

def delete(interactor)
  interactors.delete(interactor)
end

#executeObject

Execute containing interactors’ ‘execute` method with given payload.



22
23
24
25
26
27
28
29
30
31
32
# 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