Class: ActiveInteractor::Interactor::Base

Inherits:
Object
  • Object
show all
Extended by:
Type::DeclerationMethods::ClassMethods
Includes:
Type::DeclerationMethods, ActiveSupport::Callbacks
Defined in:
lib/active_interactor/interactor/base.rb

Overview

The Base Class inherited by all Interactors

Constant Summary

Constants included from Type::DeclerationMethods

Type::DeclerationMethods::Boolean

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



170
171
172
# File 'lib/active_interactor/interactor/base.rb', line 170

def initialize(options = {})
  @options = Options.new(options.deep_dup)
end

Instance Attribute Details

#contextObject (readonly, protected)

Returns the value of attribute context.



203
204
205
# File 'lib/active_interactor/interactor/base.rb', line 203

def context
  @context
end

Class Method Details

.accepts_arguments_matching(set_input_context_class) ⇒ Object Also known as: input_context, input_type



22
23
24
# File 'lib/active_interactor/interactor/base.rb', line 22

def accepts_arguments_matching(set_input_context_class)
  @input_context_class = set_input_context_class
end

.after_failObject



28
29
30
# File 'lib/active_interactor/interactor/base.rb', line 28

def after_fail(...)
  set_callback(:fail, :after, ...)
end

.after_input_context_createObject



32
33
34
# File 'lib/active_interactor/interactor/base.rb', line 32

def after_input_context_create(...)
  set_callback(:input_context_create, :after, ...)
end

.after_input_context_validationObject



36
37
38
# File 'lib/active_interactor/interactor/base.rb', line 36

def after_input_context_validation(...)
  set_callback(:input_context_validation, :after, ...)
end

.after_output_context_createObject



40
41
42
# File 'lib/active_interactor/interactor/base.rb', line 40

def after_output_context_create(...)
  set_callback(:output_context_create, :after, ...)
end

.after_output_context_validationObject



44
45
46
# File 'lib/active_interactor/interactor/base.rb', line 44

def after_output_context_validation(...)
  set_callback(:output_context_validation, :after, ...)
end

.after_performObject



48
49
50
# File 'lib/active_interactor/interactor/base.rb', line 48

def after_perform(...)
  set_callback(:perform, :after, ...)
end

.after_rollbackObject



52
53
54
# File 'lib/active_interactor/interactor/base.rb', line 52

def after_rollback(...)
  set_callback(:rollback, :after, ...)
end

.after_runtime_context_createObject



56
57
58
# File 'lib/active_interactor/interactor/base.rb', line 56

def after_runtime_context_create(...)
  set_callback(:runtime_context_create, :after, ...)
end

.anyObject Originally defined in module Type::DeclerationMethods::ClassMethods

.argument(*args, &block) ⇒ Object



15
16
17
# File 'lib/active_interactor/interactor/base.rb', line 15

def argument(*args, &block)
  input_context_class.argument(*args, &block)
end

.argument_names(*args, &block) ⇒ Object



15
16
17
# File 'lib/active_interactor/interactor/base.rb', line 15

def argument_names(*args, &block)
  input_context_class.argument_names(*args, &block)
end

.arguments(*args, &block) ⇒ Object



15
16
17
# File 'lib/active_interactor/interactor/base.rb', line 15

def arguments(*args, &block)
  input_context_class.arguments(*args, &block)
end

.around_failObject



60
61
62
# File 'lib/active_interactor/interactor/base.rb', line 60

def around_fail(...)
  set_callback(:fail, :around, ...)
end

.around_input_context_createObject



64
65
66
# File 'lib/active_interactor/interactor/base.rb', line 64

def around_input_context_create(...)
  set_callback(:input_context_create, :around, ...)
end

.around_input_context_validationObject



68
69
70
# File 'lib/active_interactor/interactor/base.rb', line 68

def around_input_context_validation(...)
  set_callback(:input_context_validation, :around, ...)
end

.around_output_context_createObject



72
73
74
# File 'lib/active_interactor/interactor/base.rb', line 72

def around_output_context_create(...)
  set_callback(:output_context_create, :around, ...)
end

.around_output_context_validationObject



76
77
78
# File 'lib/active_interactor/interactor/base.rb', line 76

def around_output_context_validation(...)
  set_callback(:output_context_validation, :around, ...)
end

.around_performObject



80
81
82
# File 'lib/active_interactor/interactor/base.rb', line 80

def around_perform(...)
  set_callback(:perform, :around, ...)
end

.around_rollbackObject



84
85
86
# File 'lib/active_interactor/interactor/base.rb', line 84

def around_rollback(...)
  set_callback(:rollback, :around, ...)
end

.around_runtime_context_createObject



88
89
90
# File 'lib/active_interactor/interactor/base.rb', line 88

def around_runtime_context_create(...)
  set_callback(:runtime_context_create, :around, ...)
end

.before_failObject



92
93
94
# File 'lib/active_interactor/interactor/base.rb', line 92

def before_fail(...)
  set_callback(:fail, :before, ...)
end

.before_input_context_createObject



96
97
98
# File 'lib/active_interactor/interactor/base.rb', line 96

def before_input_context_create(...)
  set_callback(:input_context_create, :before, ...)
end

.before_input_context_validationObject



100
101
102
# File 'lib/active_interactor/interactor/base.rb', line 100

def before_input_context_validation(...)
  set_callback(:input_context_validation, :before, ...)
end

.before_output_context_createObject



104
105
106
# File 'lib/active_interactor/interactor/base.rb', line 104

def before_output_context_create(...)
  set_callback(:output_context_create, :before, ...)
end

.before_output_context_validationObject



108
109
110
# File 'lib/active_interactor/interactor/base.rb', line 108

def before_output_context_validation(...)
  set_callback(:output_context_validation, :before, ...)
end

.before_performObject



112
113
114
# File 'lib/active_interactor/interactor/base.rb', line 112

def before_perform(...)
  set_callback(:perform, :before, ...)
end

.before_rollbackObject



116
117
118
# File 'lib/active_interactor/interactor/base.rb', line 116

def before_rollback(...)
  set_callback(:rollback, :before, ...)
end

.before_runtime_context_createObject



120
121
122
# File 'lib/active_interactor/interactor/base.rb', line 120

def before_runtime_context_create(...)
  set_callback(:runtime_context_create, :before, ...)
end

.field_names(*args, &block) ⇒ Object



16
17
18
# File 'lib/active_interactor/interactor/base.rb', line 16

def field_names(*args, &block)
  output_context_class.field_names(*args, &block)
end

.fields(*args, &block) ⇒ Object



16
17
18
# File 'lib/active_interactor/interactor/base.rb', line 16

def fields(*args, &block)
  output_context_class.fields(*args, &block)
end

.input_context_classObject



124
125
126
# File 'lib/active_interactor/interactor/base.rb', line 124

def input_context_class
  @input_context_class ||= const_set(:InputContext, Class.new(Context::Input))
end

.list(type) ⇒ Object Also known as: array Originally defined in module Type::DeclerationMethods::ClassMethods

.output_context_classObject



128
129
130
# File 'lib/active_interactor/interactor/base.rb', line 128

def output_context_class
  @output_context_class ||= const_set(:OutputContext, Class.new(Context::Output))
end

.perform(input_context = {}, options = {}) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/active_interactor/interactor/base.rb', line 136

def perform(input_context = {}, options = {})
  perform!(input_context, options)
rescue Error => e
  e.result
rescue StandardError => e
  Result.failure(errors: e.message)
end

.perform!(input_context = {}, options = {}) ⇒ Object



132
133
134
# File 'lib/active_interactor/interactor/base.rb', line 132

def perform!(input_context = {}, options = {})
  new(options).perform!(input_context)
end

.result_contextObject (protected)



165
166
167
# File 'lib/active_interactor/interactor/base.rb', line 165

def result_context
  @result_context ||= Context::Result.register_owner(self)
end

.returns(*args, &block) ⇒ Object



16
17
18
# File 'lib/active_interactor/interactor/base.rb', line 16

def returns(*args, &block)
  output_context_class.returns(*args, &block)
end

.returns_data_matching(set_output_context_class) ⇒ Object Also known as: output_context, output_type



144
145
146
# File 'lib/active_interactor/interactor/base.rb', line 144

def returns_data_matching(set_output_context_class)
  @output_context_class = set_output_context_class
end

.runtime_context_classObject



150
151
152
153
154
155
156
157
# File 'lib/active_interactor/interactor/base.rb', line 150

def runtime_context_class
  @runtime_context_class ||= begin
    context_class = const_set(:RuntimeContext, Class.new(Context::Runtime))
    context_class.send(:attribute_set).merge(input_context_class.send(:attribute_set).attributes)
    context_class.send(:attribute_set).merge(output_context_class.send(:attribute_set).attributes)
    context_class
  end
end

.union(*types) ⇒ Object Originally defined in module Type::DeclerationMethods::ClassMethods

.untypedObject Originally defined in module Type::DeclerationMethods::ClassMethods

.with_options(options) ⇒ Object



159
160
161
# File 'lib/active_interactor/interactor/base.rb', line 159

def with_options(options)
  new(options)
end

Instance Method Details

#create_input_contextObject (protected)



205
206
207
208
209
# File 'lib/active_interactor/interactor/base.rb', line 205

def create_input_context
  run_callbacks :input_context_create do
    @input_context = self.class.input_context_class.new(@raw_input.deep_dup)
  end
end

#create_output_contextObject (protected)



211
212
213
214
215
# File 'lib/active_interactor/interactor/base.rb', line 211

def create_output_context
  run_callbacks :output_context_create do
    @output_context = self.class.output_context_class.new(@context.attributes.deep_dup)
  end
end

#create_runtime_contextObject (protected)



217
218
219
220
221
# File 'lib/active_interactor/interactor/base.rb', line 217

def create_runtime_context
  run_callbacks :runtime_context_create do
    @context = self.class.runtime_context_class.new(@input_context.to_h.deep_dup)
  end
end

#execute_performObject (protected)



223
224
225
226
227
228
229
230
# File 'lib/active_interactor/interactor/base.rb', line 223

def execute_perform
  with_notification(:perform) do |payload|
    interact
    create_output_context
    validate_output_context!
    payload[:result] = Result.success(data: output_to_result_context!)
  end
end

#execute_perform_with_callbacksObject (protected)



232
233
234
235
236
# File 'lib/active_interactor/interactor/base.rb', line 232

def execute_perform_with_callbacks
  run_callbacks :perform do
    execute_perform
  end
end

#execute_rollback_with_callbacksObject (protected)



238
239
240
241
242
# File 'lib/active_interactor/interactor/base.rb', line 238

def execute_rollback_with_callbacks
  run_callbacks :rollback do
    rollback
  end
end

#fail!(errors = {}) ⇒ Object (protected)

Raises:



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/active_interactor/interactor/base.rb', line 244

def fail!(errors = {})
  run_callbacks :fail do
    result = nil
    with_notification(:rollback) do |payload|
      perform_rollback unless @options.skip_rollback
      result = Result.failure(data: @output, errors: errors)
      payload[:result] = result
    end
  end

  raise Error, result
end

#interactObject



174
# File 'lib/active_interactor/interactor/base.rb', line 174

def interact; end

#output_to_result_context!Object (protected)



257
258
259
# File 'lib/active_interactor/interactor/base.rb', line 257

def output_to_result_context!
  self.class.send(:result_context).for_output_context(self.class, @output_context)
end

#perform(input_context = {}) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/active_interactor/interactor/base.rb', line 186

def perform(input_context = {})
  perform!(input_context)
rescue Error => e
  e.result
rescue StandardError => e
  Result.failure(errors: e.message)
end

#perform!(input_context = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/active_interactor/interactor/base.rb', line 176

def perform!(input_context = {})
  @raw_input = input_context.deep_dup
  create_input_context
  validate_input_context!
  create_runtime_context
  return execute_perform_with_callbacks unless @options.skip_perform_callbacks

  execute_perform
end

#perform_rollbackObject (protected)



261
262
263
264
265
# File 'lib/active_interactor/interactor/base.rb', line 261

def perform_rollback
  return execute_rollback_with_callbacks unless @options.skip_rollback_callbacks

  rollback
end

#rollbackObject



194
# File 'lib/active_interactor/interactor/base.rb', line 194

def rollback; end

#validate_input_context!Object (protected)

Raises:



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/active_interactor/interactor/base.rb', line 267

def validate_input_context!
  return unless @options.validate && @options.validate_input_context

  run_callbacks :input_context_validation do
    @input_context.valid?
  end
  return if @input_context.errors.empty?

  raise Error,
        Result.failure(errors: @input_context.errors,
                       status: Result::STATUS[:failed_at_input])
end

#validate_output_context!Object (protected)

Raises:



280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/active_interactor/interactor/base.rb', line 280

def validate_output_context!
  return unless @options.validate && @options.validate_output_context

  run_callbacks :output_context_validation do
    @output_context.valid?
  end

  return if @output_context.errors.empty?

  raise Error,
        Result.failure(errors: @output_context.errors,
                       status: Result::STATUS[:failed_at_output])
end

#with_notification(action) ⇒ Object (protected)



294
295
296
297
298
# File 'lib/active_interactor/interactor/base.rb', line 294

def with_notification(action)
  ActiveSupport::Notifications.instrument("#{self.class.name}::#{action.to_s.classify}") do |payload|
    yield payload if block_given?
  end
end

#with_options(options) ⇒ Object



196
197
198
199
# File 'lib/active_interactor/interactor/base.rb', line 196

def with_options(options)
  @options = Options.new(options.deep_dup)
  self
end