interaktor 0.4.0 → 0.5.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.
data/.rubocop.yml DELETED
@@ -1,245 +0,0 @@
1
- require:
2
- - rubocop-rspec
3
- - rubocop-performance
4
-
5
- AllCops:
6
- TargetRubyVersion: 2.5
7
- NewCops: enable
8
- Exclude:
9
- - "bin/**/*"
10
- - "tmp/**/*"
11
- - "vendor/**/*"
12
-
13
- ###########
14
- # BUNDLER #
15
- ###########
16
-
17
- Bundler:
18
- Enabled: true
19
-
20
- Bundler/GemComment:
21
- Enabled: true
22
- OnlyFor: ["bitbucket", "gist", "git", "github", "source"]
23
- IgnoredGems: []
24
-
25
- ###########
26
- # GEMSPEC #
27
- ###########
28
-
29
- Gemspec:
30
- Enabled: true
31
-
32
- ##########
33
- # LAYOUT #
34
- ##########
35
-
36
- Layout:
37
- Enabled: true
38
-
39
- Layout/LineLength:
40
- Enabled: true
41
- Max: 120
42
-
43
- Layout/RescueEnsureAlignment:
44
- Enabled: false
45
-
46
- Layout/CaseIndentation:
47
- Enabled: false
48
-
49
- Layout/SpaceAroundOperators:
50
- Enabled: true
51
- EnforcedStyleForExponentOperator: space
52
-
53
- # Rufo already agrees with these
54
-
55
- Layout/MultilineMethodCallIndentation:
56
- Enabled: false
57
-
58
- Layout/ArgumentAlignment:
59
- Enabled: false
60
-
61
- Layout/FirstHashElementIndentation:
62
- Enabled: false
63
-
64
- Layout/FirstArrayElementIndentation:
65
- Enabled: false
66
-
67
- Layout/IndentationWidth:
68
- Enabled: false
69
-
70
- Layout/ElseAlignment:
71
- Enabled: false
72
-
73
- Layout/EndAlignment:
74
- Enabled: false
75
-
76
- Layout/EmptyLinesAroundMethodBody:
77
- Enabled: false
78
-
79
- # Make these agree with Rufo
80
-
81
- Layout/SpaceInsideBlockBraces:
82
- Enabled: true
83
- EnforcedStyleForEmptyBraces: space
84
-
85
- Layout/HeredocIndentation:
86
- Enabled: false
87
-
88
- Layout/ClosingHeredocIndentation:
89
- Enabled: false
90
-
91
- ########
92
- # LINT #
93
- ########
94
-
95
- Lint:
96
- Enabled: true
97
-
98
- # This one was giving a false positive
99
- Lint/RedundantCopDisableDirective:
100
- Enabled: false
101
-
102
- ###########
103
- # METRICS #
104
- ###########
105
-
106
- Metrics:
107
- Enabled: true
108
-
109
- Metrics/MethodLength:
110
- Enabled: true
111
- Max: 50
112
-
113
- Metrics/ClassLength:
114
- Enabled: false
115
-
116
- Metrics/ModuleLength:
117
- Enabled: false
118
-
119
- Metrics/BlockLength:
120
- Enabled: true
121
- Max: 25
122
- Exclude:
123
- - "spec/**/*"
124
- - "test/**/*"
125
-
126
- Metrics/CyclomaticComplexity:
127
- Enabled: true
128
- Max: 15
129
-
130
- Metrics/PerceivedComplexity:
131
- Enabled: true
132
- Max: 15
133
-
134
- Metrics/AbcSize:
135
- Enabled: true
136
- Max: 30
137
-
138
- ##########
139
- # NAMING #
140
- ##########
141
-
142
- Naming:
143
- Enabled: true
144
-
145
- ############
146
- # SECURITY #
147
- ############
148
-
149
- Security:
150
- Enabled: true
151
-
152
- #########
153
- # STYLE #
154
- #########
155
-
156
- Style:
157
- Enabled: true
158
-
159
- Style/BlockDelimiters:
160
- Enabled: true
161
- EnforcedStyle: braces_for_chaining
162
- Exclude:
163
- - "spec/**/*"
164
- - "test/**/*"
165
-
166
- Style/SymbolArray:
167
- Enabled: false
168
-
169
- Style/WordArray:
170
- Enabled: false
171
-
172
- Style/ClassAndModuleChildren:
173
- Enabled: false
174
-
175
- Style/Documentation:
176
- Enabled: false
177
-
178
- Style/DocumentationMethod:
179
- Enabled: true
180
- RequireForNonPublicMethods: true
181
-
182
- Style/NumericPredicate:
183
- Enabled: true
184
- EnforcedStyle: predicate
185
-
186
- Style/StringLiteralsInInterpolation:
187
- Enabled: true
188
- EnforcedStyle: double_quotes
189
-
190
- # Can never agree with Rufo
191
-
192
- Style/TrailingCommaInHashLiteral:
193
- Enabled: false
194
-
195
- Style/TrailingCommaInArrayLiteral:
196
- Enabled: false
197
-
198
- Style/TrailingCommaInArguments:
199
- Enabled: true
200
- EnforcedStyleForMultiline: comma
201
-
202
- Style/StringLiterals:
203
- Enabled: true
204
- EnforcedStyle: double_quotes
205
-
206
- Style/FrozenStringLiteralComment:
207
- Enabled: false
208
-
209
- Style/RedundantReturn:
210
- Enabled: false
211
-
212
- Style/TernaryParentheses:
213
- Enabled: false
214
-
215
- Style/RedundantParentheses:
216
- Enabled: false
217
-
218
- Style/AccessorGrouping:
219
- Enabled: false
220
-
221
- #########
222
- # RSPEC #
223
- #########
224
-
225
- RSpec:
226
- Enabled: true
227
-
228
- RSpec/ExampleLength:
229
- Enabled: false
230
-
231
- RSpec/MultipleExpectations:
232
- Enabled: false
233
-
234
- RSpec/AnyInstance:
235
- Enabled: false
236
-
237
- RSpec/MessageSpies:
238
- Enabled: false
239
-
240
- ###############
241
- # PERFORMANCE #
242
- ###############
243
-
244
- Performance:
245
- Enabled: true
@@ -1,116 +0,0 @@
1
- require "ostruct"
2
-
3
- # The object for tracking state of an Interaktor's invocation. The context is
4
- # used to initialize the interaktor with the information required for
5
- # invocation. The interaktor manipulates the context to produce the result of
6
- # invocation. The context is the mechanism by which success and failure are
7
- # determined and the context is responsible for tracking individual interaktor
8
- # invocations for the purpose of rollback. It may be manipulated using
9
- # arbitrary getter and setter methods.
10
- class Interaktor::Context < OpenStruct
11
- # Initialize an Interaktor::Context or preserve an existing one. If the
12
- # argument given is an Interaktor::Context, the argument is returned.
13
- # Otherwise, a new Interaktor::Context is initialized from the provided hash.
14
- # Used during interaktor initialization.
15
- #
16
- # @param context [Hash, Interaktor::Context] the context object as a hash
17
- # with attributes or an already-built context
18
- #
19
- # @return [Interaktor::Context]
20
- def self.build(context = {})
21
- context.is_a?(Interaktor::Context) ? context : new(context)
22
- end
23
-
24
- # Whether the Interaktor::Context is successful. By default, a new context is
25
- # successful and only changes when explicitly failed. This method is the
26
- # inverse of the `#failure?` method.
27
- #
28
- # @return [Boolean] true by default, or false if failed
29
- def success?
30
- !failure?
31
- end
32
-
33
- # Whether the Interaktor::Context has failed. By default, a new context is
34
- # successful and only changes when explicitly failed. This method is the
35
- # inverse of the `#success?` method.
36
- #
37
- # @return [Boolean] false by default, or true if failed
38
- def failure?
39
- @failure || false
40
- end
41
-
42
- # Fail the Interaktor::Context. Failing a context raises an error that may be
43
- # rescued by the calling interaktor. The context is also flagged as having
44
- # failed. Optionally the caller may provide a hash of key/value pairs to be
45
- # merged into the context before failure.
46
- #
47
- # @param context [Hash] data to be merged into the existing context
48
- #
49
- # @raises [Interaktor::Failure]
50
- #
51
- # @return [void]
52
- def fail!(context = {})
53
- context.each { |key, value| self[key.to_sym] = value }
54
- @failure = true
55
- raise Interaktor::Failure, self
56
- end
57
-
58
- # @param context [Hash] data to be merged into the existing context
59
- #
60
- # @raises [Interaktor::Failure]
61
- #
62
- # @return [void]
63
- def success!(context = {})
64
- context.each { |key, value| self[key.to_sym] = value }
65
- early_return!
66
- end
67
-
68
- # Roll back the Interaktor::Context. Any interaktors to which this context
69
- # has been passed and which have been successfully called are asked to roll
70
- # themselves back by invoking their `#rollback` methods.
71
- #
72
- # @return [Boolean] true if rolled back successfully, false if already
73
- # rolled back
74
- def rollback!
75
- return false if @rolled_back
76
-
77
- _called.reverse_each(&:rollback)
78
- @rolled_back = true
79
- end
80
-
81
- # Track that an Interaktor has been called. The `#called!` method is used by
82
- # the interaktor being invoked with this context. After an interaktor is
83
- # successfully called, the interaktor instance is tracked in the context for
84
- # the purpose of potential future rollback.
85
- #
86
- # @param interaktor [Interaktor] an interaktor that has been successfully
87
- # called
88
- #
89
- # @return [void]
90
- def called!(interaktor)
91
- _called << interaktor
92
- end
93
-
94
- # An array of successfully called Interaktor instances invoked against this
95
- # Interaktor::Context instance.
96
- #
97
- # @return [Array<Interaktor>]
98
- def _called
99
- @called ||= []
100
- end
101
-
102
- # Trigger an early return throw.
103
- #
104
- # @return [void]
105
- def early_return!
106
- @early_return = true
107
- throw :early_return, self
108
- end
109
-
110
- # Whether or not the context has been returned from early.
111
- #
112
- # @return [Boolean]
113
- def early_return?
114
- (@early_return == true) || false
115
- end
116
- end