activeinteractor 1.2.2 → 2.0.0.alpha.1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/LICENSE +1 -1
- data/README.md +4 -64
- data/lib/active_interactor/base.rb +77 -29
- data/lib/active_interactor/errors.rb +12 -0
- data/lib/active_interactor/result.rb +73 -0
- data/lib/active_interactor.rb +3 -101
- data/sig/active_interactor.rbs +3 -0
- metadata +23 -86
- data/.yardopts +0 -12
- data/lib/active_interactor/config.rb +0 -60
- data/lib/active_interactor/configurable.rb +0 -53
- data/lib/active_interactor/context/attributes.rb +0 -179
- data/lib/active_interactor/context/base.rb +0 -333
- data/lib/active_interactor/context/errors.rb +0 -57
- data/lib/active_interactor/context/loader.rb +0 -50
- data/lib/active_interactor/context/status.rb +0 -106
- data/lib/active_interactor/error.rb +0 -46
- data/lib/active_interactor/interactor/callbacks.rb +0 -293
- data/lib/active_interactor/interactor/context.rb +0 -378
- data/lib/active_interactor/interactor/perform.rb +0 -260
- data/lib/active_interactor/interactor/worker.rb +0 -120
- data/lib/active_interactor/models.rb +0 -50
- data/lib/active_interactor/organizer/base.rb +0 -18
- data/lib/active_interactor/organizer/callbacks.rb +0 -275
- data/lib/active_interactor/organizer/interactor_interface.rb +0 -127
- data/lib/active_interactor/organizer/interactor_interface_collection.rb +0 -62
- data/lib/active_interactor/organizer/organize.rb +0 -67
- data/lib/active_interactor/organizer/perform.rb +0 -112
- data/lib/active_interactor/rails/orm/active_record.rb +0 -5
- data/lib/active_interactor/rails/orm/dynamoid.rb +0 -5
- data/lib/active_interactor/rails/orm/mongoid.rb +0 -5
- data/lib/active_interactor/rails/railtie.rb +0 -21
- data/lib/active_interactor/rails.rb +0 -4
- data/lib/active_interactor/version.rb +0 -45
- data/lib/rails/generators/active_interactor/application_context_generator.rb +0 -21
- data/lib/rails/generators/active_interactor/application_interactor_generator.rb +0 -21
- data/lib/rails/generators/active_interactor/application_organizer_generator.rb +0 -21
- data/lib/rails/generators/active_interactor/base.rb +0 -29
- data/lib/rails/generators/active_interactor/generator.rb +0 -21
- data/lib/rails/generators/active_interactor/install_generator.rb +0 -16
- data/lib/rails/generators/active_interactor.rb +0 -5
- data/lib/rails/generators/interactor/context/rspec_generator.rb +0 -17
- data/lib/rails/generators/interactor/context/test_unit_generator.rb +0 -17
- data/lib/rails/generators/interactor/context_generator.rb +0 -22
- data/lib/rails/generators/interactor/generates_context.rb +0 -28
- data/lib/rails/generators/interactor/interactor_generator.rb +0 -25
- data/lib/rails/generators/interactor/organizer_generator.rb +0 -39
- data/lib/rails/generators/interactor/rspec_generator.rb +0 -15
- data/lib/rails/generators/interactor/test_unit_generator.rb +0 -15
- data/lib/rails/generators/templates/active_interactor.erb +0 -17
- data/lib/rails/generators/templates/application_context.rb +0 -4
- data/lib/rails/generators/templates/application_interactor.rb +0 -4
- data/lib/rails/generators/templates/application_organizer.rb +0 -4
- data/lib/rails/generators/templates/context.erb +0 -7
- data/lib/rails/generators/templates/context_spec.erb +0 -7
- data/lib/rails/generators/templates/context_test_unit.erb +0 -9
- data/lib/rails/generators/templates/interactor.erb +0 -15
- data/lib/rails/generators/templates/interactor_spec.erb +0 -7
- data/lib/rails/generators/templates/interactor_test_unit.erb +0 -9
- data/lib/rails/generators/templates/organizer.erb +0 -13
@@ -1,378 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveInteractor
|
4
|
-
module Interactor
|
5
|
-
# Interactor context methods. Because {Context} is a module classes should include {Context} rather than inherit
|
6
|
-
# from it.
|
7
|
-
#
|
8
|
-
# @author Aaron Allen <hello@aaronmallen.me>
|
9
|
-
# @since 0.1.0
|
10
|
-
module Context
|
11
|
-
# Interactor context class methods. Because {ClassMethods} is a module classes should extend {ClassMethods}
|
12
|
-
# rather than inherit from it.
|
13
|
-
#
|
14
|
-
# @author Aaron Allen <hello@aaronmallen.me>
|
15
|
-
# @since 0.1.0
|
16
|
-
module ClassMethods
|
17
|
-
# @!method context_attributes(*attributes)
|
18
|
-
# Call {ActiveInteractor::Context::Attributes#attributes .attributes} on the {Base interactor} class'
|
19
|
-
# {#context_class context class}
|
20
|
-
#
|
21
|
-
# @since 0.1.0
|
22
|
-
#
|
23
|
-
# @example
|
24
|
-
# class MyInteractor < ActiveInteractor::Base
|
25
|
-
# context_attributes :first_name, :last_name
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
# MyInteractor.context_class.attributes
|
29
|
-
# #=> [:first_name, :last_name]
|
30
|
-
#
|
31
|
-
# @!method context_attribute_missing(match, *args, &block)
|
32
|
-
# Call {ActiveInteractor::Context::Base.attribute_missing .attribute_missing} on the {Base interactor} class'
|
33
|
-
# {#context_class context class}
|
34
|
-
#
|
35
|
-
# @since 1.0.1
|
36
|
-
#
|
37
|
-
# @!method context_respond_to_without_attributes?(method, include_private_methods = false)
|
38
|
-
# Call {ActiveInteractor::Context::Base.respond_to_without_attributes? .respond_to_without_attributes?} on the
|
39
|
-
# {Base interactor} class' {#context_class context class}
|
40
|
-
#
|
41
|
-
# @since 1.0.1
|
42
|
-
delegate :attributes, :attribute_missing, :respond_to_without_attributes?, to: :context_class, prefix: :context
|
43
|
-
|
44
|
-
# @!method context_attribute(name, type=Type::Value.new, **options)
|
45
|
-
# Call {ActiveInteractor::Context::Base.attribute .attribute} on the {Base interactor} class'
|
46
|
-
# {#context_class context class}
|
47
|
-
#
|
48
|
-
# @since 1.0.1
|
49
|
-
#
|
50
|
-
# @example Setting default values on the {ActiveInteractor::Context::Base context} class
|
51
|
-
# class MyInteractor < ActiveInteractor::Base
|
52
|
-
# context_attribute :first_name, default: -> { 'Aaron' }
|
53
|
-
# end
|
54
|
-
#
|
55
|
-
# MyInteractor.perform
|
56
|
-
# #=> <#MyInteractor::Context first_name='Aaron'>
|
57
|
-
#
|
58
|
-
# MyInteractor.perform(first_name: 'Bob')
|
59
|
-
# #=> <#MyInteractor::Context first_name='Bob'>
|
60
|
-
#
|
61
|
-
# @!method context_attribute_names
|
62
|
-
# Call {ActiveInteractor::Context::Base.attribute_names .attribute_names} on the {Base interactor} class'
|
63
|
-
# {#context_class context class}
|
64
|
-
#
|
65
|
-
# @since 1.0.1
|
66
|
-
delegate(*ActiveModel::Attributes::ClassMethods.instance_methods, to: :context_class, prefix: :context)
|
67
|
-
|
68
|
-
# @!method context_attribute_method?(attribute)
|
69
|
-
# Call {ActiveInteractor::Context::Base.attribute_method? .attribute_method?} on the {Base interactor} class'
|
70
|
-
# {#context_class context class}
|
71
|
-
#
|
72
|
-
# @since 0.1.0
|
73
|
-
#
|
74
|
-
# @!method context_clear_validators!(attribute)
|
75
|
-
# Call {ActiveInteractor::Context::Base.clear_validators! .clear_validators!} on the {Base interactor} class'
|
76
|
-
# {#context_class context class}
|
77
|
-
#
|
78
|
-
# @since 0.1.0
|
79
|
-
#
|
80
|
-
# @!method context_validate(*args, &block)
|
81
|
-
# Call {ActiveInteractor::Context::Base.validate .validate} on the {Base interactor} class'
|
82
|
-
# {#context_class context class}
|
83
|
-
#
|
84
|
-
# @since 0.1.0
|
85
|
-
#
|
86
|
-
# @!method context_validates(*attributes)
|
87
|
-
# Call {ActiveInteractor::Context::Base.validates .validates} on the {Base interactor} class'
|
88
|
-
# {#context_class context class}
|
89
|
-
#
|
90
|
-
# @since 0.1.0
|
91
|
-
#
|
92
|
-
# @!method context_validates!(*attributes)
|
93
|
-
# Call {ActiveInteractor::Context::Base.validates! .validates!} on the {Base interactor} class'
|
94
|
-
# {#context_class context class}
|
95
|
-
#
|
96
|
-
# @since 0.1.0
|
97
|
-
#
|
98
|
-
# @!method context_validates_each(*attr_names, &block)
|
99
|
-
# Call {ActiveInteractor::Context::Base.validates_each .validates_each} on the {Base interactor} class'
|
100
|
-
# {#context_class context class}
|
101
|
-
#
|
102
|
-
# @since 0.1.0
|
103
|
-
#
|
104
|
-
# @!method context_validates_with(*args, &block)
|
105
|
-
# Call {ActiveInteractor::Context::Base.validates_with .validates_with} on the {Base interactor} class'
|
106
|
-
# {#context_class context class}
|
107
|
-
#
|
108
|
-
# @since 0.1.0
|
109
|
-
#
|
110
|
-
# @!method context_validators
|
111
|
-
# Call {ActiveInteractor::Context::Base.validators .validators} on the {Base interactor} class'
|
112
|
-
# {#context_class context class}
|
113
|
-
#
|
114
|
-
# @since 0.1.0
|
115
|
-
#
|
116
|
-
# @!method context_validators_on(*attributes)
|
117
|
-
# Call {ActiveInteractor::Context::Base.validators_on .validators_on} on the {Base interactor} class'
|
118
|
-
# {#context_class context class}
|
119
|
-
#
|
120
|
-
# @since 0.1.0
|
121
|
-
delegate(*ActiveModel::Validations::ClassMethods.instance_methods, to: :context_class, prefix: :context)
|
122
|
-
|
123
|
-
# @!method context_validates_absence_of(*attr_names)
|
124
|
-
# Call {ActiveInteractor::Context::Base.validates_absence_of .validates_absence_of} on the {Base interactor}
|
125
|
-
# class' {#context_class context class}
|
126
|
-
#
|
127
|
-
# @since 0.1.0
|
128
|
-
#
|
129
|
-
# @!method context_validates_acceptance_of(*attr_names)
|
130
|
-
# Call {ActiveInteractor::Context::Base.validates_acceptance_of .validates_acceptance_of} on the
|
131
|
-
# {Base interactor} class' {#context_class context class}
|
132
|
-
#
|
133
|
-
# @since 0.1.0
|
134
|
-
#
|
135
|
-
# @!method context_validates_confirmation_of(*attr_names)
|
136
|
-
# Call {ActiveInteractor::Context::Base.validates_confirmation_of .validates_confirmation_of} on the
|
137
|
-
# {Base interactor} class' {#context_class context class}
|
138
|
-
#
|
139
|
-
# @since 0.1.0
|
140
|
-
#
|
141
|
-
# @!method context_validates_exclusion_of(*attr_names)
|
142
|
-
# Call {ActiveInteractor::Context::Base.validates_exclusion_of .validates_exclusion_of} on the
|
143
|
-
# {Base interactor} class' {#context_class context class}
|
144
|
-
#
|
145
|
-
# @since 0.1.0
|
146
|
-
#
|
147
|
-
# @!method context_validates_format_of(*attr_names)
|
148
|
-
# Call {ActiveInteractor::Context::Base.validates_format_of .validates_format_of} on the {Base interactor}
|
149
|
-
# class' {#context_class context class}
|
150
|
-
#
|
151
|
-
# @since 0.1.0
|
152
|
-
#
|
153
|
-
# @!method context_validates_inclusion_of(*attr_names)
|
154
|
-
# Call {ActiveInteractor::Context::Base.validates_inclusion_of .validates_inclusion_of} on the
|
155
|
-
# {Base interactor} class' {#context_class context class}
|
156
|
-
#
|
157
|
-
# @since 0.1.0
|
158
|
-
#
|
159
|
-
# @!method context_validates_length_of(*attr_names)
|
160
|
-
# Call {ActiveInteractor::Context::Base.validates_length_of .validates_length_of} on the {Base interactor}
|
161
|
-
# class' {#context_class context class}
|
162
|
-
#
|
163
|
-
# @since 0.1.0
|
164
|
-
#
|
165
|
-
# @!method context_validates_numericality_of(*attr_names)
|
166
|
-
# Call {ActiveInteractor::Context::Base.validates_numericality_of .validates_numericality_of} on the
|
167
|
-
# {Base interactor} class' {#context_class context class}
|
168
|
-
#
|
169
|
-
# @since 0.1.0
|
170
|
-
#
|
171
|
-
# @!method context_validates_presence_of(*attr_names)
|
172
|
-
# Call {ActiveInteractor::Context::Base.validates_presence_of .validates_presence_of} on the {Base interactor}
|
173
|
-
# class' {#context_class context class}
|
174
|
-
#
|
175
|
-
# @since 0.1.0
|
176
|
-
#
|
177
|
-
# @!method context_validates_size_of(*attr_names)
|
178
|
-
# Call {ActiveInteractor::Context::Base.validates_size_of .validates_size_of} on the {Base interactor} class'
|
179
|
-
# {#context_class context class}
|
180
|
-
#
|
181
|
-
# @since 0.1.0
|
182
|
-
delegate(*ActiveModel::Validations::HelperMethods.instance_methods, to: :context_class, prefix: :context)
|
183
|
-
|
184
|
-
# Get the {Base interactor} class' {ActiveInteractor::Context::Base context} class. If no class is found or no
|
185
|
-
# class is set via the {#contextualize_with} method a new class is created.
|
186
|
-
#
|
187
|
-
# @see ActiveInteractor::Context::Loader.find_or_create
|
188
|
-
#
|
189
|
-
# @return [Const] the {Base interactor} class' {ActiveInteractor::Context::Base context} class
|
190
|
-
def context_class
|
191
|
-
@context_class ||= ActiveInteractor::Context::Loader.find_or_create(self)
|
192
|
-
end
|
193
|
-
|
194
|
-
# Set the {Base interactor} class' {#context_class context class}
|
195
|
-
#
|
196
|
-
# @since 1.0.0
|
197
|
-
#
|
198
|
-
# @example
|
199
|
-
# class User < ActiveRecord::Base
|
200
|
-
# end
|
201
|
-
#
|
202
|
-
# class MyInteractor < ActiveInteractor::Base
|
203
|
-
# contextualize_with :user
|
204
|
-
# end
|
205
|
-
#
|
206
|
-
# MyInteractor.context_class
|
207
|
-
# #=> User
|
208
|
-
#
|
209
|
-
# @param klass [Const, Symbol, String] the class to use as context
|
210
|
-
# @raise [Error::InvalidContextClass] if the class can not be found.
|
211
|
-
# @return [Const] the {Base interactor} class' {ActiveInteractor::Context::Base context} class
|
212
|
-
def contextualize_with(klass)
|
213
|
-
@context_class = begin
|
214
|
-
context_class = klass.to_s.camelize.safe_constantize
|
215
|
-
raise(ActiveInteractor::Error::InvalidContextClass, klass) unless context_class
|
216
|
-
|
217
|
-
context_class
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
# @!method context_attribute_missing(match, *args, &block)
|
223
|
-
# Call {ActiveInteractor::Context::Base#attribute_missing #attribute_missing} on the {Base interactor} instance's
|
224
|
-
# {ActiveInteractor::Context::Base context} instance
|
225
|
-
#
|
226
|
-
# @since 1.0.1
|
227
|
-
#
|
228
|
-
# @!method context_attribute_names
|
229
|
-
# Call {ActiveInteractor::Context::Base#attribute_names #attribute_names} on the {Base interactor} instance's
|
230
|
-
# {ActiveInteractor::Context::Base context} instance
|
231
|
-
#
|
232
|
-
# @since 1.0.1
|
233
|
-
#
|
234
|
-
# @!method context_fail!(errors = nil)
|
235
|
-
# Call {ActiveInteractor::Context::Status#fail! #fail!} on the {Base interactor} instance's
|
236
|
-
# {ActiveInteractor::Context::Base context} instance
|
237
|
-
#
|
238
|
-
# @since 1.0.0
|
239
|
-
#
|
240
|
-
# @!method context_respond_to_without_attributes?(method, include_private_methods = false)
|
241
|
-
# Call {ActiveInteractor::Context::Base#respond_to_without_attributes? #respond_to_without_attributes?} on the
|
242
|
-
# {Base interactor} instance's {ActiveInteractor::Context::Base context} instance
|
243
|
-
#
|
244
|
-
# @since 1.0.1
|
245
|
-
#
|
246
|
-
# @!method context_rollback!
|
247
|
-
# Call {ActiveInteractor::Context::Status#rollback! #rollback!} on the {Base interactor} instance's
|
248
|
-
# {ActiveInteractor::Context::Base context} instance
|
249
|
-
#
|
250
|
-
# @since 1.0.0
|
251
|
-
delegate :attribute_missing, :attribute_names, :fail!, :respond_to_without_attributes?, :rollback!,
|
252
|
-
to: :context, prefix: true
|
253
|
-
|
254
|
-
# @!method context_errors
|
255
|
-
# Call {ActiveInteractor::Context::Base#errors #errors} on the {Base interactor} instance's
|
256
|
-
# {ActiveInteractor::Context::Base context} instance
|
257
|
-
#
|
258
|
-
# @since 0.1.0
|
259
|
-
#
|
260
|
-
# @!method context_invalid?(context = nil)
|
261
|
-
# Call {ActiveInteractor::Context::Base#invalid? #invalid?} on the {Base interactor} instance's
|
262
|
-
# {ActiveInteractor::Context::Base context} instance
|
263
|
-
#
|
264
|
-
# @since 0.1.0
|
265
|
-
#
|
266
|
-
# @!method context_valid?(context = nil)
|
267
|
-
# Call {ActiveInteractor::Context::Base#valid? #valid?} on the {Base interactor} instance's
|
268
|
-
# {ActiveInteractor::Context::Base context} instance
|
269
|
-
#
|
270
|
-
# @since 0.1.0
|
271
|
-
#
|
272
|
-
# @!method context_validate(context = nil)
|
273
|
-
# Call {ActiveInteractor::Context::Base#validate #validate} on the {Base interactor} instance's
|
274
|
-
# {ActiveInteractor::Context::Base context} instance
|
275
|
-
#
|
276
|
-
# @since 0.1.0
|
277
|
-
#
|
278
|
-
# @!method context_validate!(context = nil)
|
279
|
-
# Call {ActiveInteractor::Context::Base#validate! #validate!} on the {Base interactor} instance's
|
280
|
-
# {ActiveInteractor::Context::Base context} instance
|
281
|
-
#
|
282
|
-
# @since 0.1.0
|
283
|
-
#
|
284
|
-
# @!method context_validates_with(*args, &block)
|
285
|
-
# Call {ActiveInteractor::Context::Base#validates_with #validates_with} on the {Base interactor} instance's
|
286
|
-
# {ActiveInteractor::Context::Base context} instance
|
287
|
-
#
|
288
|
-
# @since 0.1.0
|
289
|
-
delegate(*ActiveModel::Validations.instance_methods, to: :context, prefix: true)
|
290
|
-
|
291
|
-
# @!method context_validates_absence_of(*attr_names)
|
292
|
-
# Call {ActiveInteractor::Context::Base#validates_absence_of #validates_absence_of} on the {Base interactor}
|
293
|
-
# instance's {ActiveInteractor::Context::Base context} instance
|
294
|
-
#
|
295
|
-
# @since 0.1.0
|
296
|
-
#
|
297
|
-
# @!method context_validates_acceptance_of(*attr_names)
|
298
|
-
# Call {ActiveInteractor::Context::Base#validates_acceptance_of #validates_acceptance_of} on the
|
299
|
-
# {Base interactor} instance's {ActiveInteractor::Context::Base context} instance
|
300
|
-
#
|
301
|
-
# @since 0.1.0
|
302
|
-
#
|
303
|
-
# @!method context_validates_confirmation_of(*attr_names)
|
304
|
-
# Call {ActiveInteractor::Context::Base#validates_confirmation_of #validates_confirmation_of} on the
|
305
|
-
# {Base interactor} instance's {ActiveInteractor::Context::Base context} instance
|
306
|
-
#
|
307
|
-
# @since 0.1.0
|
308
|
-
#
|
309
|
-
# @!method context_validates_exclusion_of(*attr_names)
|
310
|
-
# Call {ActiveInteractor::Context::Base#validates_exclusion_of #validates_exclusion_of} on the {Base interactor}
|
311
|
-
# instance's {ActiveInteractor::Context::Base context} instance
|
312
|
-
#
|
313
|
-
# @since 0.1.0
|
314
|
-
#
|
315
|
-
# @!method context_validates_format_of(*attr_names)
|
316
|
-
# Call {ActiveInteractor::Context::Base#validates_format_of #validates_format_of} on the {Base interactor}
|
317
|
-
# instance's {ActiveInteractor::Context::Base context} instance
|
318
|
-
#
|
319
|
-
# @since 0.1.0
|
320
|
-
#
|
321
|
-
# @!method context_validates_inclusion_of(*attr_names)
|
322
|
-
# Call {ActiveInteractor::Context::Base#validates_inclusion_of #validates_inclusion_of} on the
|
323
|
-
# {Base interactor} instance's {ActiveInteractor::Context::Base context} instance
|
324
|
-
#
|
325
|
-
# @since 0.1.0
|
326
|
-
#
|
327
|
-
# @!method context_validates_length_of(*attr_names)
|
328
|
-
# Call {ActiveInteractor::Context::Base#validates_length_of #validates_length_of} on the {Base interactor}
|
329
|
-
# instance's {ActiveInteractor::Context::Base context} instance
|
330
|
-
#
|
331
|
-
# @since 0.1.0
|
332
|
-
#
|
333
|
-
# @!method context_validates_numericality_of(*attr_names)
|
334
|
-
# Call {ActiveInteractor::Context::Base#validates_numericality_of #validates_numericality_of} on the
|
335
|
-
# {Base interactor} instance's {ActiveInteractor::Context::Base context} instance
|
336
|
-
#
|
337
|
-
# @since 0.1.0
|
338
|
-
#
|
339
|
-
# @!method context_validates_presence_of(*attr_names)
|
340
|
-
# Call {ActiveInteractor::Context::Base#validates_presence_of #validates_presence_of} on the {Base interactor}
|
341
|
-
# instance's {ActiveInteractor::Context::Base context} instance
|
342
|
-
#
|
343
|
-
# @since 0.1.0
|
344
|
-
#
|
345
|
-
# @!method context_validates_size_of(*attr_names)
|
346
|
-
# Call {ActiveInteractor::Context::Base#validates_size_of #validates_size_of} on the {Base interactor}
|
347
|
-
# instance's {ActiveInteractor::Context::Base context} instance
|
348
|
-
#
|
349
|
-
# @since 0.1.0
|
350
|
-
delegate(*ActiveModel::Validations::HelperMethods.instance_methods, to: :context, prefix: true)
|
351
|
-
|
352
|
-
# Initialize a new instance of {Base}
|
353
|
-
#
|
354
|
-
# @param context [Hash, Class] attributes to assign to the {Base interactor} instance's
|
355
|
-
# {ActiveInteractor::Context::Base context} instance
|
356
|
-
# @return [Base] a new instance of {Base}
|
357
|
-
def initialize(context = {})
|
358
|
-
@context = self.class.context_class.new(context)
|
359
|
-
end
|
360
|
-
|
361
|
-
# Mark the {Base interactor} instance as called on the instance's {ActiveInteractor::Context::Base context}
|
362
|
-
# instance and return the {ActiveInteractor::Context::Base context} instance.
|
363
|
-
#
|
364
|
-
# @since 1.0.0
|
365
|
-
#
|
366
|
-
# @return [Class] the {ActiveInteractor::Context::Base context} instance
|
367
|
-
def finalize_context!
|
368
|
-
context.called!(self)
|
369
|
-
context.resolve
|
370
|
-
context
|
371
|
-
end
|
372
|
-
|
373
|
-
private
|
374
|
-
|
375
|
-
attr_accessor :context
|
376
|
-
end
|
377
|
-
end
|
378
|
-
end
|
@@ -1,260 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveInteractor
|
4
|
-
module Interactor
|
5
|
-
# Interactor perform methods. Because {Perform} is a module classes should include {Perform} rather than inherit
|
6
|
-
# from it.
|
7
|
-
#
|
8
|
-
# @author Aaron Allen <hello@aaronmallen.me>
|
9
|
-
# @since 1.0.0
|
10
|
-
module Perform
|
11
|
-
# Interactor perform class methods. Because {ClassMethods} is a module classes should extend {ClassMethods}
|
12
|
-
# rather than inherit from it.
|
13
|
-
#
|
14
|
-
# @author Aaron Allen <hello@aaronmallen.me>
|
15
|
-
# @since 1.0.0
|
16
|
-
module ClassMethods
|
17
|
-
# Initialize a new {Base interactor} instance and call its {Interactor::Perform#perform #perform} method. This
|
18
|
-
# is the default api to interact with all {Base interactors}.
|
19
|
-
#
|
20
|
-
# @since 0.1.0
|
21
|
-
#
|
22
|
-
# @example
|
23
|
-
# class MyInteractor < ActiveInteractor::Base
|
24
|
-
# def perform
|
25
|
-
# puts "I performed"
|
26
|
-
# end
|
27
|
-
# end
|
28
|
-
#
|
29
|
-
# MyInteractor.perform
|
30
|
-
# "I performed"
|
31
|
-
# #=> <#MyInteractor::Context>
|
32
|
-
#
|
33
|
-
# @param context [Hash, Class] attributes to assign to the {Base interactor} instance's
|
34
|
-
# {ActiveInteractor::Context::Base context} instance
|
35
|
-
# @param options [Hash, Perform::Options] options to use for the {.perform}. See {Perform::Options}
|
36
|
-
# @return [Class] an instance of the {Base interactor} class' {ActiveInteractor::Context::Base context}
|
37
|
-
# instance
|
38
|
-
def perform(context = {}, options = {})
|
39
|
-
new(context).with_options(options).execute_perform
|
40
|
-
end
|
41
|
-
|
42
|
-
# Initialize a new {Base interactor} instance and call its {Interactor::Perform#perform #perform} method without
|
43
|
-
# rescuing {ActiveInteractor::Error::ContextFailure}.
|
44
|
-
#
|
45
|
-
# @since 0.1.0
|
46
|
-
#
|
47
|
-
# @example Calling {Perform::ClassMethods#perform! .perform!} without failure
|
48
|
-
# class MyInteractor < ActiveInteractor::Base
|
49
|
-
# def perform
|
50
|
-
# puts "I performed"
|
51
|
-
# end
|
52
|
-
# end
|
53
|
-
#
|
54
|
-
# MyInteractor.perform!
|
55
|
-
# "I performed"
|
56
|
-
# #=> <#MyInteractor::Context>
|
57
|
-
#
|
58
|
-
# @example Calling {Perform::ClassMethods#perform! .perform!} with failure
|
59
|
-
# class MyInteractor < ActiveInteractor::Base
|
60
|
-
# def perform
|
61
|
-
# context.fail!
|
62
|
-
# end
|
63
|
-
# end
|
64
|
-
#
|
65
|
-
# MyInteractor.perform!
|
66
|
-
# ActiveInteractor::Error::ContextFailure "<#MyInteractor::Context>"
|
67
|
-
#
|
68
|
-
# @param context [Hash, Class] attributes to assign to the {Base interactor} instance's
|
69
|
-
# {ActiveInteractor::Context::Base context} instance
|
70
|
-
# @param options [Hash, Perform::Options] options to use for the {.perform}. See {Perform::Options}
|
71
|
-
# @raise [Error::ContextFailure] if the {ActiveInteractor::Context::Base context} instance
|
72
|
-
# fails.
|
73
|
-
# @return [Class] an instance of the {Base interactor} class' {ActiveInteractor::Context::Base context}
|
74
|
-
# instance
|
75
|
-
def perform!(context = {}, options = {})
|
76
|
-
new(context).with_options(options).execute_perform!
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Interactor {Interactor::Perform#perform #perform} options
|
81
|
-
#
|
82
|
-
# @author Aaron Allen <hello@aaronmallen.me>
|
83
|
-
# @since 1.0.0
|
84
|
-
#
|
85
|
-
# @!attribute [rw] skip_each_perform_callbacks
|
86
|
-
# if `true` an {Organizer::Base organizer} will be instructed to skip
|
87
|
-
# {Organizer::Callbacks::ClassMethods each_perform} callbacks.
|
88
|
-
#
|
89
|
-
# @since 1.0.0
|
90
|
-
#
|
91
|
-
# @return [Boolean] whether or not to skip {Organizer::Callbacks::ClassMethods each_perform} callbacks
|
92
|
-
#
|
93
|
-
# @!attribute [rw] skip_perform_callbacks
|
94
|
-
# if `true` an {Base interactor} will be instructed to skip {Interactor::Callbacks::ClassMethods perform}
|
95
|
-
# callbacks.
|
96
|
-
#
|
97
|
-
# @since 1.0.0
|
98
|
-
#
|
99
|
-
# @return [Boolean] whether or not to skip {Interactor::Callbacks::ClassMethods perform} callbacks.
|
100
|
-
#
|
101
|
-
# @!attribute [rw] skip_rollback
|
102
|
-
# if `true` an {Base interactor} will be instructed to skip {Interactor::Perform#rollback #rollback} on
|
103
|
-
# {Context::Base context} {ActiveInteractor::Context::Status#fail! failure}.
|
104
|
-
#
|
105
|
-
# @since 1.0.0
|
106
|
-
#
|
107
|
-
# @return [Boolean] whether or not to skip {Interactor::Perform#rollback #rollback}
|
108
|
-
#
|
109
|
-
# @!attribute [rw] skip_rollback_callbacks
|
110
|
-
# if `true` an {Base interactor} will be instructed to skip {Interactor::Callbacks::ClassMethods rollback}
|
111
|
-
# callbacks on {Context::Base context} {ActiveInteractor::Context::Status#fail! failure}.
|
112
|
-
#
|
113
|
-
# @since 1.0.0
|
114
|
-
#
|
115
|
-
# @return [Boolean] whether or not to skip {Interactor::Callbacks::ClassMethods rollback} callbacks.
|
116
|
-
#
|
117
|
-
# @!attribute [rw] validate
|
118
|
-
# if `false` an {Base interactor} will not run validations.
|
119
|
-
#
|
120
|
-
# @since 1.0.0
|
121
|
-
#
|
122
|
-
# @return [Boolean] whether or to run validations.
|
123
|
-
#
|
124
|
-
# @!attribute [rw] validate_on_calling
|
125
|
-
# if `false` an {Base interactor} will not run validations with the validation context `:calling`.
|
126
|
-
#
|
127
|
-
# @since 1.0.0
|
128
|
-
#
|
129
|
-
# @return [Boolean] whether or to run validations with the validation context `:calling`
|
130
|
-
#
|
131
|
-
# @!attribute [rw] validate_on_called
|
132
|
-
# if `false` an {Base interactor} will not run validations with the validation context `:called`.
|
133
|
-
#
|
134
|
-
# @since 1.0.0
|
135
|
-
#
|
136
|
-
# @return [Boolean] whether or to run validation with the validation context `:called`.
|
137
|
-
#
|
138
|
-
# @!method initialize(options = {})
|
139
|
-
# Initialize a new instance of {Options}
|
140
|
-
#
|
141
|
-
# @since 1.0.0
|
142
|
-
#
|
143
|
-
# @param options [Hash{Symbol=>*}] the attributes to assign to {Options}
|
144
|
-
# @option options [Boolean] :skip_each_perform_callbacks (false) the {Options#skip_each_perform_callbacks}
|
145
|
-
# attribute
|
146
|
-
# @option options [Boolean] :skip_perform_callbacks (false) the {Options#skip_perform_callbacks} attribute
|
147
|
-
# @option options [Boolean] :skip_rollback (false) the {Options#skip_rollback} attribute
|
148
|
-
# @option options [Boolean] :skip_rollback_callbacks (false) the {Options#skip_rollback_callbacks} attribute
|
149
|
-
# @option options [Boolean] :validate (true) the {Options#validate} attribute
|
150
|
-
# @option options [Boolean] :validate_on_calling (true) the {Options#validate_on_calling} attribute
|
151
|
-
# @option options [Boolean] :validate_on_called (true) the {Options#validate_on_called} attribute
|
152
|
-
# @return [Options] a new instance of {Options}
|
153
|
-
class Options
|
154
|
-
include ActiveInteractor::Configurable
|
155
|
-
defaults skip_each_perform_callbacks: false, skip_perform_callbacks: false, skip_rollback: false,
|
156
|
-
skip_rollback_callbacks: false, validate: true, validate_on_calling: true, validate_on_called: true
|
157
|
-
end
|
158
|
-
|
159
|
-
# @!method execute_perform
|
160
|
-
# Run the {Base interactor} instance's {#perform} with callbacks and validation.
|
161
|
-
#
|
162
|
-
# @api private
|
163
|
-
# @since 0.1.0
|
164
|
-
#
|
165
|
-
# @!method execute_perform!
|
166
|
-
# Run the {Base interactor} instance's {#perform} with callbacks and validation without rescuing
|
167
|
-
# {Error::ContextFailure}.
|
168
|
-
#
|
169
|
-
# @api private
|
170
|
-
# @since 0.1.0
|
171
|
-
delegate :execute_perform, :execute_perform!, to: :worker
|
172
|
-
|
173
|
-
# Duplicate an {Base interactor} instance as well as it's {#options} and {ActiveInteractor::Context::Base context}
|
174
|
-
# instances.
|
175
|
-
#
|
176
|
-
# @return [Base] a duplicated {Base interactor} instance
|
177
|
-
def deep_dup
|
178
|
-
dupped = dup
|
179
|
-
%w[@context @options].each do |variable|
|
180
|
-
dupped.instance_variable_set(variable, instance_variable_get(variable)&.dup)
|
181
|
-
end
|
182
|
-
dupped
|
183
|
-
end
|
184
|
-
|
185
|
-
# Options for the {Base interactor} {#perform}
|
186
|
-
#
|
187
|
-
# @return [Options] an instance of {Options}
|
188
|
-
def options
|
189
|
-
@options ||= ActiveInteractor::Interactor::Perform::Options.new
|
190
|
-
end
|
191
|
-
|
192
|
-
# The steps to run when an {Base interactor} is called. An {Base interactor's} {#perform} method should never be
|
193
|
-
# called directly on an {Base interactor} instance and should instead be invoked by the {Base interactor's} class
|
194
|
-
# method {Perform::ClassMethods#perform .perform}.
|
195
|
-
#
|
196
|
-
# @since 0.1.0
|
197
|
-
#
|
198
|
-
# @abstract {Base interactors} should override {#perform} with the appropriate steps and
|
199
|
-
# {ActiveInteractor::Context::Base context} mutations required for the {Base interactor} to do its work.
|
200
|
-
#
|
201
|
-
# @example
|
202
|
-
# class MyInteractor < ActiveInteractor::Base
|
203
|
-
# def perform
|
204
|
-
# context.first_name = 'Aaron'
|
205
|
-
# end
|
206
|
-
# end
|
207
|
-
#
|
208
|
-
# MyInteractor.perform
|
209
|
-
# #=> <#MyInteractor::Context first_name='Aaron'>
|
210
|
-
def perform; end
|
211
|
-
|
212
|
-
# The steps to run when an {Base interactor} {ActiveInteractor::Context::Status#fail! fails}. An
|
213
|
-
# {Base interactor's} {#rollback} method should never be called directly and is instead called by the
|
214
|
-
# {Base interactor's} {ActiveInteractor::Context::Base context} when
|
215
|
-
# {ActiveInteractor::Context::Status#fail! #fail} is called.
|
216
|
-
#
|
217
|
-
# @since 0.1.0
|
218
|
-
#
|
219
|
-
# @abstract {Base interactors} should override {#rollback} with the appropriate steps and
|
220
|
-
# {ActiveInteractor::Context::Base context} mutations required for the {Base interactor} to roll back its work.
|
221
|
-
#
|
222
|
-
# @example
|
223
|
-
# class MyInteractor < ActiveInteractor::Base
|
224
|
-
# def perform
|
225
|
-
# context.first_name = 'Aaron'
|
226
|
-
# context.fail!
|
227
|
-
# end
|
228
|
-
#
|
229
|
-
# def rollback
|
230
|
-
# context.first_name = 'Bob'
|
231
|
-
# end
|
232
|
-
# end
|
233
|
-
#
|
234
|
-
# MyInteractor.perform
|
235
|
-
# #=> <#MyInteractor::Context first_name='Bob'>
|
236
|
-
def rollback; end
|
237
|
-
|
238
|
-
# Set {Options options} for an {Base interactor's} {#perform}
|
239
|
-
#
|
240
|
-
# @api private
|
241
|
-
#
|
242
|
-
# @param options [Hash, Perform::Options] options to use for the perform call. See {Perform::Options}
|
243
|
-
# @return [self] the {Base interactor} instance
|
244
|
-
def with_options(options)
|
245
|
-
@options = if options.is_a?(ActiveInteractor::Interactor::Perform::Options)
|
246
|
-
options
|
247
|
-
else
|
248
|
-
ActiveInteractor::Interactor::Perform::Options.new(options)
|
249
|
-
end
|
250
|
-
self
|
251
|
-
end
|
252
|
-
|
253
|
-
private
|
254
|
-
|
255
|
-
def worker
|
256
|
-
ActiveInteractor::Interactor::Worker.new(self)
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|