Class: ActionInteractor::Base
- Inherits:
-
Object
- Object
- ActionInteractor::Base
- Defined in:
- lib/action_interactor/base.rb
Overview
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
Direct Known Subclasses
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#interactor_name ⇒ Object
readonly
Returns the value of attribute interactor_name.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
-
.execute(payload) ⇒ Object
Execute the operation with given payload.
-
.execute!(payload) ⇒ Object
Execute the operation with given payload.
Instance Method Summary collapse
-
#abort! ⇒ Object
Mark the operation as aborted.
-
#aborted? ⇒ Boolean
Returns
trueif the operation was not successful and not finished otherwisefalse. -
#execute ⇒ Object
Execute the operation with given payload.
-
#execute! ⇒ Object
Execute the operation with given payload.
-
#failure! ⇒ Object
(also: #fail!)
Mask the operation as failure.
-
#failure? ⇒ Boolean
Returns
trueif not marked as success or there are some errors otherwisefalse. -
#finished? ⇒ Boolean
Returns
trueif marked as finished otherwisefalse. -
#initialize(payload) ⇒ Base
constructor
Initialize with payload Errors and Results data and initial state will be set.
-
#reset! ⇒ Object
Reset statuses.
-
#successful! ⇒ Object
(also: #success!)
Mark the operation as successful.
-
#successful? ⇒ Boolean
(also: #success?)
Returns
trueif marked as success and there are no errors otherwisefalse. -
#unfinished? ⇒ Boolean
Returns
trueif not marked as finished otherwisefalse.
Constructor Details
#initialize(payload) ⇒ Base
Initialize with payload Errors and Results data and initial state will be set.
30 31 32 33 34 35 36 |
# 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 |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
26 27 28 |
# File 'lib/action_interactor/base.rb', line 26 def errors @errors end |
#interactor_name ⇒ Object (readonly)
Returns the value of attribute interactor_name.
26 27 28 |
# File 'lib/action_interactor/base.rb', line 26 def interactor_name @interactor_name end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
26 27 28 |
# File 'lib/action_interactor/base.rb', line 26 def payload @payload end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
26 27 28 |
# File 'lib/action_interactor/base.rb', line 26 def results @results end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
26 27 28 |
# File 'lib/action_interactor/base.rb', line 26 def state @state end |
Class Method Details
.execute(payload) ⇒ Object
Execute the operation with given payload.
111 112 113 |
# File 'lib/action_interactor/base.rb', line 111 def execute(payload) new(payload).tap(&:execute) end |
.execute!(payload) ⇒ Object
Execute the operation with given payload. If there are some errors, ActionInteractor::ExeuctionError will be raised.
117 118 119 |
# File 'lib/action_interactor/base.rb', line 117 def execute!(payload) new(payload).tap(&:execute!) end |
Instance Method Details
#abort! ⇒ Object
Mark the operation as aborted.
91 92 93 |
# File 'lib/action_interactor/base.rb', line 91 def abort! state.aborted! end |
#aborted? ⇒ Boolean
Returns true if the operation was not successful and not finished otherwise false.
81 82 83 |
# File 'lib/action_interactor/base.rb', line 81 def aborted? state.aborted? end |
#execute ⇒ Object
Execute the operation with given payload. (Should be overridden.)
40 41 42 43 44 45 46 47 48 49 |
# 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 empty), mark as failure. return failure! if payload.nil? # (Implement some codes for the operation.) # if finished execution, mark as successful. successful! end |
#execute! ⇒ Object
Execute the operation with given payload. If there are some errors, ActionInteractor::ExeuctionError will be raised.
53 54 55 56 |
# File 'lib/action_interactor/base.rb', line 53 def execute! execute successful? || raise(ExecutionError.new("Failed to execute the interactor")) end |
#failure! ⇒ Object Also known as: fail!
Mask the operation as failure.
103 104 105 |
# File 'lib/action_interactor/base.rb', line 103 def failure! state.failure! end |
#failure? ⇒ Boolean
Returns true if not marked as success or there are some errors otherwise false.
76 77 78 |
# File 'lib/action_interactor/base.rb', line 76 def failure? !successful? end |
#finished? ⇒ Boolean
Returns true if marked as finished otherwise false.
59 60 61 |
# File 'lib/action_interactor/base.rb', line 59 def finished? state.successful? || state.failure? end |
#reset! ⇒ Object
Reset statuses.
86 87 88 |
# File 'lib/action_interactor/base.rb', line 86 def reset! @state = State.new end |
#successful! ⇒ Object Also known as: success!
Mark the operation as successful.
96 97 98 |
# File 'lib/action_interactor/base.rb', line 96 def successful! state.successful! end |
#successful? ⇒ Boolean Also known as: success?
Returns true if marked as success and there are no errors otherwise false.
69 70 71 |
# File 'lib/action_interactor/base.rb', line 69 def successful? state.successful? && @errors.empty? end |
#unfinished? ⇒ Boolean
Returns true if not marked as finished otherwise false.
64 65 66 |
# File 'lib/action_interactor/base.rb', line 64 def unfinished? !finished? end |