state_machines 0.201.0 → 0.300.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/README.md +152 -14
- data/lib/state_machines/async_mode/async_event_extensions.rb +1 -1
- data/lib/state_machines/async_mode/async_events.rb +38 -26
- data/lib/state_machines/async_mode/async_machine.rb +2 -2
- data/lib/state_machines/async_mode/async_transition_collection.rb +14 -86
- data/lib/state_machines/async_mode/thread_safe_state.rb +9 -4
- data/lib/state_machines/branch.rb +35 -22
- data/lib/state_machines/callback.rb +4 -4
- data/lib/state_machines/core.rb +1 -0
- data/lib/state_machines/error.rb +37 -0
- data/lib/state_machines/event.rb +125 -2
- data/lib/state_machines/event_collection.rb +6 -1
- data/lib/state_machines/extensions.rb +13 -0
- data/lib/state_machines/machine/event_methods.rb +5 -0
- data/lib/state_machines/machine/utilities.rb +27 -0
- data/lib/state_machines/machine.rb +2 -3
- data/lib/state_machines/machine_collection.rb +7 -6
- data/lib/state_machines/macro_methods.rb +4 -4
- data/lib/state_machines/state.rb +3 -3
- data/lib/state_machines/stdio_renderer.rb +8 -6
- data/lib/state_machines/test_helper.rb +210 -1
- data/lib/state_machines/transition.rb +29 -13
- data/lib/state_machines/transition_claim.rb +118 -0
- data/lib/state_machines/transition_collection.rb +142 -18
- data/lib/state_machines/version.rb +1 -1
- metadata +8 -7
|
@@ -59,7 +59,77 @@ module StateMachines
|
|
|
59
59
|
#
|
|
60
60
|
# If a block is passed to this method, that block will be called instead
|
|
61
61
|
# of invoking each transition's action.
|
|
62
|
-
|
|
62
|
+
#
|
|
63
|
+
# When the collection is refused, each transition that caused it carries
|
|
64
|
+
# the reason in {StateMachines::Transition#refusal}, and the reason is
|
|
65
|
+
# recorded for its event on the object. The outcome of this attempt is
|
|
66
|
+
# recorded however it ends: a success or an exception raised before any
|
|
67
|
+
# outcome (e.g. by a callback) clears the reasons left by an earlier
|
|
68
|
+
# attempt.
|
|
69
|
+
#
|
|
70
|
+
# When the action fails because an event attribute it performed was
|
|
71
|
+
# refused (a collection nested inside the action, as the ORM integrations
|
|
72
|
+
# run around save), the transitions carry that inner reason instead of
|
|
73
|
+
# :action_failed.
|
|
74
|
+
#
|
|
75
|
+
# @return [Object, false] the action's result, or whether every transition
|
|
76
|
+
# succeeded
|
|
77
|
+
# @see StateMachines::Event#refusal
|
|
78
|
+
def perform(&)
|
|
79
|
+
each { |transition| transition.refusal = nil }
|
|
80
|
+
claims = TransitionClaim.claim(self) { |transition| transition.refusal = :contended }
|
|
81
|
+
return false unless claims
|
|
82
|
+
|
|
83
|
+
result = perform_transitions(&)
|
|
84
|
+
assign_refusals unless success?
|
|
85
|
+
result
|
|
86
|
+
ensure
|
|
87
|
+
record_refusals
|
|
88
|
+
TransitionClaim.release(claims) if claims
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
class << self
|
|
92
|
+
# Reports the reason an event attribute performed inside an action was
|
|
93
|
+
# refused to the innermost collection running that action on the same
|
|
94
|
+
# object, which then carries it instead of :action_failed if the action
|
|
95
|
+
# fails.
|
|
96
|
+
#
|
|
97
|
+
# @api private
|
|
98
|
+
# @param object [Object] the object the action runs on
|
|
99
|
+
# @param reason [Symbol, nil] the inner refusal reason
|
|
100
|
+
# @return [void]
|
|
101
|
+
def report_nested_refusal(object, reason)
|
|
102
|
+
return unless reason
|
|
103
|
+
|
|
104
|
+
running = Thread.current[RUNNING_ACTIONS_KEY]&.reverse_each&.find { |collection| collection.object.equal?(object) }
|
|
105
|
+
running&.nested_refusal ||= reason
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The fiber-local stack of collections whose actions are running
|
|
110
|
+
RUNNING_ACTIONS_KEY = :state_machines_running_actions
|
|
111
|
+
|
|
112
|
+
# The reason an event attribute performed inside this collection's action
|
|
113
|
+
# was refused (see .report_nested_refusal)
|
|
114
|
+
#
|
|
115
|
+
# @api private
|
|
116
|
+
# @return [Symbol, nil]
|
|
117
|
+
attr_accessor :nested_refusal
|
|
118
|
+
|
|
119
|
+
# Gets the object being transitioned
|
|
120
|
+
#
|
|
121
|
+
# @return [Object]
|
|
122
|
+
def object
|
|
123
|
+
first.object
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
protected
|
|
127
|
+
|
|
128
|
+
attr_reader :results # :nodoc:
|
|
129
|
+
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
def perform_transitions(&)
|
|
63
133
|
reset
|
|
64
134
|
|
|
65
135
|
if valid?
|
|
@@ -69,10 +139,11 @@ module StateMachines
|
|
|
69
139
|
transition.machine.write(object, :event_transition, transition)
|
|
70
140
|
end
|
|
71
141
|
|
|
142
|
+
@actions_run = true
|
|
72
143
|
run_actions
|
|
73
144
|
else
|
|
74
145
|
within_transaction do
|
|
75
|
-
catch(:halt) { run_callbacks(&
|
|
146
|
+
catch(:halt) { run_callbacks(&) }
|
|
76
147
|
rollback unless success?
|
|
77
148
|
end
|
|
78
149
|
end
|
|
@@ -85,12 +156,6 @@ module StateMachines
|
|
|
85
156
|
end
|
|
86
157
|
end
|
|
87
158
|
|
|
88
|
-
protected
|
|
89
|
-
|
|
90
|
-
attr_reader :results # :nodoc:
|
|
91
|
-
|
|
92
|
-
private
|
|
93
|
-
|
|
94
159
|
# Is this a valid set of transitions? If the collection was creating with
|
|
95
160
|
# any +false+ values for transitions, then the the collection will be
|
|
96
161
|
# marked as invalid.
|
|
@@ -106,11 +171,6 @@ module StateMachines
|
|
|
106
171
|
@success
|
|
107
172
|
end
|
|
108
173
|
|
|
109
|
-
# Gets the object being transitioned
|
|
110
|
-
def object
|
|
111
|
-
first.object
|
|
112
|
-
end
|
|
113
|
-
|
|
114
174
|
# Gets the list of actions to run. If configured to skip actions, then
|
|
115
175
|
# this will return an empty collection.
|
|
116
176
|
def actions
|
|
@@ -129,6 +189,36 @@ module StateMachines
|
|
|
129
189
|
def reset
|
|
130
190
|
@results = {}
|
|
131
191
|
@success = false
|
|
192
|
+
@actions_run = false
|
|
193
|
+
@halted_by = nil
|
|
194
|
+
@nested_refusal = nil
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Assigns the reason this collection was refused to the transitions that
|
|
198
|
+
# caused it. A reason already assigned by a collection nested inside the
|
|
199
|
+
# action (an action hook performing the same transitions) is kept, since
|
|
200
|
+
# it knows why the action failed; so is a reason reported by an event
|
|
201
|
+
# attribute refused inside the action (see .report_nested_refusal).
|
|
202
|
+
#
|
|
203
|
+
# @return [void]
|
|
204
|
+
def assign_refusals
|
|
205
|
+
return if any?(&:refusal)
|
|
206
|
+
|
|
207
|
+
if @actions_run
|
|
208
|
+
reason = nested_refusal || :action_failed
|
|
209
|
+
each { |transition| transition.refusal = reason }
|
|
210
|
+
elsif @halted_by
|
|
211
|
+
@halted_by.refusal = :halted
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Records each transition's refusal (or its absence) for its event on the
|
|
216
|
+
# object
|
|
217
|
+
#
|
|
218
|
+
# @return [void]
|
|
219
|
+
# @see StateMachines::Event#record_refusal
|
|
220
|
+
def record_refusals
|
|
221
|
+
each { |transition| transition.machine.events.fetch(transition.event).record_refusal(transition.object, transition.refusal) }
|
|
132
222
|
end
|
|
133
223
|
|
|
134
224
|
# Runs each transition's callbacks recursively. Once all before callbacks
|
|
@@ -136,14 +226,14 @@ module StateMachines
|
|
|
136
226
|
# configured actions will be run.
|
|
137
227
|
#
|
|
138
228
|
# If any transition fails to run its callbacks, :halt will be thrown.
|
|
139
|
-
def run_callbacks(index = 0, &
|
|
229
|
+
def run_callbacks(index = 0, &)
|
|
140
230
|
if (transition = self[index])
|
|
141
231
|
# Pass through any options that affect callback execution (e.g., fiber: false)
|
|
142
232
|
callback_options = { after: !skip_after }
|
|
143
233
|
callback_options[:fiber] = options[:fiber] if options.key?(:fiber)
|
|
144
234
|
|
|
145
235
|
callback_result = transition.run_callbacks(callback_options) do
|
|
146
|
-
run_callbacks(index + 1, &
|
|
236
|
+
run_callbacks(index + 1, &)
|
|
147
237
|
{ result: results[transition.action], success: success? }
|
|
148
238
|
end
|
|
149
239
|
|
|
@@ -151,10 +241,16 @@ module StateMachines
|
|
|
151
241
|
# consider it successful (the pause was intentional)
|
|
152
242
|
@success = true if skip_after && transition.paused?
|
|
153
243
|
|
|
154
|
-
|
|
244
|
+
unless callback_result
|
|
245
|
+
# The innermost transition to halt is the one that caused it; the
|
|
246
|
+
# ones wrapping it halt as a consequence
|
|
247
|
+
@halted_by ||= transition
|
|
248
|
+
throw :halt
|
|
249
|
+
end
|
|
155
250
|
else
|
|
156
251
|
persist
|
|
157
|
-
|
|
252
|
+
@actions_run = true
|
|
253
|
+
run_actions(&)
|
|
158
254
|
end
|
|
159
255
|
end
|
|
160
256
|
|
|
@@ -189,10 +285,27 @@ module StateMachines
|
|
|
189
285
|
# Wraps the given block with a rescue handler so that any exceptions that
|
|
190
286
|
# occur will automatically result in the transition rolling back any changes
|
|
191
287
|
# that were made to the object involved.
|
|
288
|
+
#
|
|
289
|
+
# An action that raises (e.g. ActiveRecord's save!) has failed, so the
|
|
290
|
+
# transitions are refused with :action_failed before the exception
|
|
291
|
+
# propagates.
|
|
292
|
+
#
|
|
293
|
+
# While the block runs, the collection is registered as running its
|
|
294
|
+
# action on the object, so that event attributes refused inside it can
|
|
295
|
+
# report their reason (see .report_nested_refusal).
|
|
296
|
+
#
|
|
297
|
+
# @raise [StandardError] whatever the block raised, after the rollback
|
|
192
298
|
def catch_exceptions
|
|
193
|
-
|
|
299
|
+
running = (Thread.current[RUNNING_ACTIONS_KEY] ||= [])
|
|
300
|
+
running.push(self)
|
|
301
|
+
begin
|
|
302
|
+
yield
|
|
303
|
+
ensure
|
|
304
|
+
running.pop
|
|
305
|
+
end
|
|
194
306
|
rescue StandardError
|
|
195
307
|
rollback
|
|
308
|
+
each { |transition| transition.refusal ||= :action_failed }
|
|
196
309
|
raise
|
|
197
310
|
end
|
|
198
311
|
|
|
@@ -217,6 +330,17 @@ module StateMachines
|
|
|
217
330
|
super(transitions, { use_transactions: false, actions: false }.merge(options))
|
|
218
331
|
end
|
|
219
332
|
|
|
333
|
+
# Performs the event attributes' transitions around the action. When
|
|
334
|
+
# they are refused, the reason is reported to the collection running the
|
|
335
|
+
# action, if any (see TransitionCollection.report_nested_refusal).
|
|
336
|
+
#
|
|
337
|
+
# @return [Object, false] see TransitionCollection#perform
|
|
338
|
+
def perform(&)
|
|
339
|
+
result = super
|
|
340
|
+
TransitionCollection.report_nested_refusal(object, find(&:refusal)&.refusal) unless empty? || success?
|
|
341
|
+
result
|
|
342
|
+
end
|
|
343
|
+
|
|
220
344
|
private
|
|
221
345
|
|
|
222
346
|
# Hooks into running transition callbacks so that event / event transition
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: state_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.300.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -28,16 +28,16 @@ dependencies:
|
|
|
28
28
|
name: minitest
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- -
|
|
31
|
+
- - "~>"
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version:
|
|
33
|
+
version: '6.0'
|
|
34
34
|
type: :development
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
|
-
- -
|
|
38
|
+
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version:
|
|
40
|
+
version: '6.0'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: rake
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -111,6 +111,7 @@ files:
|
|
|
111
111
|
- lib/state_machines/syntax_validator.rb
|
|
112
112
|
- lib/state_machines/test_helper.rb
|
|
113
113
|
- lib/state_machines/transition.rb
|
|
114
|
+
- lib/state_machines/transition_claim.rb
|
|
114
115
|
- lib/state_machines/transition_collection.rb
|
|
115
116
|
- lib/state_machines/version.rb
|
|
116
117
|
homepage: https://github.com/state-machines/state_machines
|
|
@@ -128,14 +129,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
128
129
|
requirements:
|
|
129
130
|
- - ">="
|
|
130
131
|
- !ruby/object:Gem::Version
|
|
131
|
-
version:
|
|
132
|
+
version: '4.0'
|
|
132
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
134
|
requirements:
|
|
134
135
|
- - ">="
|
|
135
136
|
- !ruby/object:Gem::Version
|
|
136
137
|
version: '0'
|
|
137
138
|
requirements: []
|
|
138
|
-
rubygems_version: 4.0.
|
|
139
|
+
rubygems_version: 4.0.20
|
|
139
140
|
specification_version: 4
|
|
140
141
|
summary: State machines for attributes
|
|
141
142
|
test_files: []
|