state_machines 0.202.0 → 0.305.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 +226 -125
- data/lib/state_machines/branch.rb +56 -22
- data/lib/state_machines/callback.rb +28 -8
- data/lib/state_machines/core.rb +1 -0
- data/lib/state_machines/error.rb +50 -2
- data/lib/state_machines/eval_helpers.rb +31 -18
- data/lib/state_machines/event.rb +187 -4
- data/lib/state_machines/event_collection.rb +6 -1
- data/lib/state_machines/extensions.rb +22 -0
- data/lib/state_machines/helper_module.rb +1 -0
- data/lib/state_machines/machine/action_hooks.rb +13 -10
- data/lib/state_machines/machine/callbacks.rb +15 -0
- data/lib/state_machines/machine/class_methods.rb +34 -3
- data/lib/state_machines/machine/configuration.rb +12 -8
- data/lib/state_machines/machine/event_methods.rb +5 -0
- data/lib/state_machines/machine/helper_generators.rb +26 -11
- data/lib/state_machines/machine/state_methods.rb +4 -3
- data/lib/state_machines/machine/utilities.rb +25 -1
- data/lib/state_machines/machine.rb +62 -3
- data/lib/state_machines/machine_collection.rb +7 -6
- data/lib/state_machines/macro_methods.rb +4 -4
- data/lib/state_machines/node_collection.rb +26 -23
- data/lib/state_machines/path.rb +1 -0
- data/lib/state_machines/path_collection.rb +1 -0
- data/lib/state_machines/state.rb +66 -31
- data/lib/state_machines/state_collection.rb +10 -2
- data/lib/state_machines/state_context.rb +7 -1
- data/lib/state_machines/stdio_renderer.rb +8 -6
- data/lib/state_machines/syntax_validator.rb +3 -3
- data/lib/state_machines/test_helper.rb +309 -259
- data/lib/state_machines/transition.rb +136 -230
- data/lib/state_machines/transition_claim.rb +119 -0
- data/lib/state_machines/transition_collection.rb +147 -31
- data/lib/state_machines/version.rb +1 -1
- metadata +8 -15
- data/lib/state_machines/async_mode/async_event_extensions.rb +0 -49
- data/lib/state_machines/async_mode/async_events.rb +0 -286
- data/lib/state_machines/async_mode/async_machine.rb +0 -60
- data/lib/state_machines/async_mode/async_transition_collection.rb +0 -136
- data/lib/state_machines/async_mode/thread_safe_state.rb +0 -47
- data/lib/state_machines/async_mode.rb +0 -64
- data/lib/state_machines/machine/async_extensions.rb +0 -88
- data/lib/state_machines/machine/validation.rb +0 -39
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '0786cd064b1c8a4d4059cf25f0626596722957a07e14b49b8410ee3e89004339'
|
|
4
|
+
data.tar.gz: b767aa9e33559e58d2c1f396d0b37eabf7eda95f5c9a230af4994785a4316fa2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8b2d9d7a22a0831c1d5db45efcc695b759dd2a50cf3c98ac06bcabacb32b3730405d2113c2c3a0aedfb832c17fa330ceeae77a39fa13977cdbb4eb913204b76a
|
|
7
|
+
data.tar.gz: 748b596b642049249be0e6a3aa13ad160becec9d1ea72b962bc83ffc300d91a287cff5aa20f0eee2cbfec961097900da4e0fe620e97db529b58dbacff4e61aff
|
data/README.md
CHANGED
|
@@ -37,7 +37,6 @@ Below is an example of many of the features offered by this plugin, including:
|
|
|
37
37
|
* Transition callbacks
|
|
38
38
|
* Conditional transitions
|
|
39
39
|
* Coordinated state management guards
|
|
40
|
-
* Asynchronous state machines (async: true)
|
|
41
40
|
* State-driven instance behavior
|
|
42
41
|
* Customized state values
|
|
43
42
|
* Parallel events
|
|
@@ -197,22 +196,23 @@ vehicle.state_transitions # => [#<StateMachines:Transition attribute=:stat
|
|
|
197
196
|
vehicle.speed # => 0
|
|
198
197
|
vehicle.moving? # => false
|
|
199
198
|
|
|
200
|
-
vehicle.ignite # =>
|
|
199
|
+
vehicle.ignite # => #<StateMachines::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>
|
|
201
200
|
vehicle.parked? # => false
|
|
202
201
|
vehicle.idling? # => true
|
|
203
202
|
vehicle.speed # => 10
|
|
204
203
|
vehicle # => #<Vehicle:0xb7cf4eac @state="idling", @seatbelt_on=true>
|
|
205
204
|
|
|
206
|
-
vehicle.shift_up # =>
|
|
205
|
+
vehicle.shift_up # => #<StateMachines::Transition attribute=:state event=:shift_up from="idling" from_name=:idling to="first_gear" to_name=:first_gear>
|
|
207
206
|
vehicle.speed # => 10
|
|
208
207
|
vehicle.moving? # => true
|
|
209
208
|
vehicle # => #<Vehicle:0xb7cf4eac @state="first_gear", @seatbelt_on=true>
|
|
210
209
|
|
|
211
210
|
# A generic event helper is available to fire without going through the event's instance method
|
|
212
|
-
vehicle.fire_state_event(:shift_up) # =>
|
|
211
|
+
vehicle.fire_state_event(:shift_up) # => #<StateMachines::Transition attribute=:state event=:shift_up ...>
|
|
213
212
|
|
|
214
213
|
# Call state-driven behavior that's undefined for the state raises a NoMethodError
|
|
215
|
-
|
|
214
|
+
# (StateMachines::InvalidContext)
|
|
215
|
+
vehicle.speed # => StateMachines::InvalidContext: State :second_gear for :state is not a valid context for calling #speed
|
|
216
216
|
vehicle # => #<Vehicle:0xb7cf4eac @state="second_gear", @seatbelt_on=true>
|
|
217
217
|
|
|
218
218
|
# The bang (!) operator can raise exceptions if the event fails
|
|
@@ -223,7 +223,7 @@ vehicle.state?(:parked) # => false
|
|
|
223
223
|
vehicle.state?(:invalid) # => IndexError: :invalid is an invalid name
|
|
224
224
|
|
|
225
225
|
# Transition callbacks can receive arguments
|
|
226
|
-
vehicle.park(meter_number: '12345') # =>
|
|
226
|
+
vehicle.park(meter_number: '12345') # => #<StateMachines::Transition attribute=:state event=:park ...>
|
|
227
227
|
vehicle.parked? # => true
|
|
228
228
|
vehicle.parking_meter_number # => "12345"
|
|
229
229
|
|
|
@@ -232,7 +232,7 @@ vehicle.alarm_state # => 1
|
|
|
232
232
|
vehicle.alarm_state_name # => :active
|
|
233
233
|
|
|
234
234
|
vehicle.can_disable_alarm? # => true
|
|
235
|
-
vehicle.disable_alarm # =>
|
|
235
|
+
vehicle.disable_alarm # => #<StateMachines::Transition attribute=:alarm_state event=:disable ...>
|
|
236
236
|
vehicle.alarm_state # => 0
|
|
237
237
|
vehicle.alarm_state_name # => :off
|
|
238
238
|
vehicle.can_enable_alarm? # => true
|
|
@@ -246,6 +246,169 @@ vehicle.state_name # => :first_gear
|
|
|
246
246
|
vehicle.alarm_state_name # => :active
|
|
247
247
|
|
|
248
248
|
vehicle.fire_events!(:ignite, :enable_alarm) # => StateMachines:InvalidParallelTransition: Cannot run events in parallel: ignite, enable_alarm
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Refusal Reasons
|
|
252
|
+
|
|
253
|
+
An event method returns the performed `StateMachines::Transition` on success and
|
|
254
|
+
`false` when the event is refused, so `if vehicle.ignite` keeps working. (Compare
|
|
255
|
+
with `if`, not `== true`.) To tell *why* it was refused, every event gets a
|
|
256
|
+
`<event>_refusal` reader next to `can_<event>?`, `<event>_transition` and
|
|
257
|
+
`<event>!`. It returns the reason of the most recent attempt to fire that event
|
|
258
|
+
on that object, or `nil` once an attempt succeeds:
|
|
259
|
+
|
|
260
|
+
| Reason | Meaning |
|
|
261
|
+
|------------------|--------------------------------------------------------------------------|
|
|
262
|
+
| `:no_transition` | No transition is defined for the event from the current state |
|
|
263
|
+
| `:guard` | A transition from the current state exists, but its `if:`/`unless:` (or state guards) rejected it |
|
|
264
|
+
| `:halted` | A `before_transition` or `around_transition` callback halted it |
|
|
265
|
+
| `:action_failed` | The machine's action (e.g. `save`) did not succeed |
|
|
266
|
+
| `:contended` | A concurrent transition held or moved the attribute first (only machines that claim their transitions, e.g. [state_machines-async](https://github.com/state-machines/state_machines-async)) |
|
|
267
|
+
|
|
268
|
+
```ruby
|
|
269
|
+
vehicle = Vehicle.new # parked
|
|
270
|
+
vehicle.ignite_refusal # => nil (never refused)
|
|
271
|
+
|
|
272
|
+
vehicle.park # => false
|
|
273
|
+
vehicle.park_refusal # => :no_transition
|
|
274
|
+
|
|
275
|
+
vehicle.ignite # => #<StateMachines::Transition attribute=:state event=:ignite ...>
|
|
276
|
+
vehicle.ignite_refusal # => nil
|
|
277
|
+
|
|
278
|
+
# The bang method raises with the same reason
|
|
279
|
+
begin
|
|
280
|
+
vehicle.park!
|
|
281
|
+
rescue StateMachines::InvalidTransition => e
|
|
282
|
+
e.reason # => :no_transition
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# A transition performed directly carries its reason too
|
|
286
|
+
transition = vehicle.shift_up_transition
|
|
287
|
+
transition.perform # => false (say a before_transition halted it)
|
|
288
|
+
transition.refusal # => :halted
|
|
289
|
+
vehicle.shift_up_refusal # => :halted
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
The reason is kept per event and per object, so firing another event does not
|
|
293
|
+
overwrite it. The guard conditions are not evaluated a second time to tell
|
|
294
|
+
`:guard` from `:no_transition`.
|
|
295
|
+
|
|
296
|
+
Parallel events report a reason only for the events that caused the refusal; the
|
|
297
|
+
others read `nil`. An action failure is shared, so every event in the set reports
|
|
298
|
+
`:action_failed`:
|
|
299
|
+
|
|
300
|
+
```ruby
|
|
301
|
+
vehicle.fire_events(:ignite, :enable_alarm) # => false
|
|
302
|
+
vehicle.ignite_refusal # => :no_transition
|
|
303
|
+
vehicle.enable_alarm_refusal # => nil
|
|
304
|
+
|
|
305
|
+
begin
|
|
306
|
+
vehicle.fire_events!(:ignite, :enable_alarm)
|
|
307
|
+
rescue StateMachines::InvalidParallelTransition => e
|
|
308
|
+
e.reasons # => {ignite: :no_transition}
|
|
309
|
+
end
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
When the action fails because an event attribute it runs was refused (the ORM
|
|
313
|
+
integrations perform `state_event` / `alarm_state_event` inside `save`), the
|
|
314
|
+
fired event carries that inner reason, matching the bang method's message:
|
|
315
|
+
an `alarm_state_event` halted inside `save` makes `ignite` read `:halted`. A
|
|
316
|
+
plain action failure (`save` returning false, a failed validation) stays
|
|
317
|
+
`:action_failed`.
|
|
318
|
+
|
|
319
|
+
Every attempt replaces the previous reason, including one that ends in an
|
|
320
|
+
exception: a success whose `after_transition` raises, or a callback or guard
|
|
321
|
+
that raises before any outcome, leaves the reader at `nil`. (An action that
|
|
322
|
+
raises is `:action_failed`.)
|
|
323
|
+
|
|
324
|
+
Whether an `after_transition` callback that raises undoes the transition
|
|
325
|
+
depends on the action. Without an action hook, the callback runs after the
|
|
326
|
+
transition is complete: the new state stays and the reader is `nil`. When the
|
|
327
|
+
machine hooks its action (the ORM integrations hook `save`), `after_transition`
|
|
328
|
+
callbacks run inside that action, so a raise counts as the action failing: the
|
|
329
|
+
state rolls back with it and the reader is `:action_failed`. The exception is
|
|
330
|
+
re-raised in both cases.
|
|
331
|
+
|
|
332
|
+
A copy made with `dup` or `clone` starts with no reasons, since it has made no
|
|
333
|
+
attempts. A frozen object records nothing: the event still returns `false`, and
|
|
334
|
+
the reader returns `nil`.
|
|
335
|
+
|
|
336
|
+
The reader reports the most recent attempt **on the object**, not on the
|
|
337
|
+
caller. When several threads or fibers fire events on the same object
|
|
338
|
+
concurrently, another caller may overwrite the reason between your attempt and
|
|
339
|
+
your read. Use the bang method and rescue `StateMachines::InvalidTransition`,
|
|
340
|
+
whose `#reason` belongs to that call, or `StateMachines::Transition#refusal` on a
|
|
341
|
+
transition you performed yourself:
|
|
342
|
+
|
|
343
|
+
```ruby
|
|
344
|
+
begin
|
|
345
|
+
vehicle.ignite!
|
|
346
|
+
rescue StateMachines::InvalidTransition => e
|
|
347
|
+
e.reason # => this call's reason, e.g. :contended
|
|
348
|
+
end
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
The reader is also available on the event itself:
|
|
352
|
+
`Vehicle.state_machine.events[:park].refusal(vehicle)`.
|
|
353
|
+
|
|
354
|
+
## Branch Callbacks
|
|
355
|
+
|
|
356
|
+
A `transition` can carry its own `before:`, `after:` and `around:` callbacks.
|
|
357
|
+
They run only when that transition is the branch chosen for the event, so they
|
|
358
|
+
share its guards without repeating them in an `after_transition`:
|
|
359
|
+
|
|
360
|
+
```ruby
|
|
361
|
+
class Vehicle
|
|
362
|
+
state_machine initial: :parked do
|
|
363
|
+
event :ignite do
|
|
364
|
+
transition parked: :idling, if: :warm?
|
|
365
|
+
transition parked: :preheating, after: :start_glow_plugs
|
|
366
|
+
transition stalled: :idling, before: :check_battery, after: %i[log_restart notify_owner]
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
Each takes what `after_transition`'s `do:` takes: a method name (Symbol or
|
|
373
|
+
String), an Array of them, or a Proc/Method. They are ordinary callbacks:
|
|
374
|
+
`bind_to_object`, the integration's terminator and `throw :halt` behave as for
|
|
375
|
+
`before_transition`/`after_transition`.
|
|
376
|
+
|
|
377
|
+
They wrap closest to the state change: the machine's `before_transition` and
|
|
378
|
+
`around_transition` callbacks run first, then the branch's `before:`/`around:`
|
|
379
|
+
(in the order written), then the change; then the branch's `after:`, then the
|
|
380
|
+
machine's `after_transition`. A branch `after:` runs only when the transition
|
|
381
|
+
succeeds; there is no branch-level `after_failure` (use `after_failure`).
|
|
382
|
+
`assert_before_transition`/`assert_after_transition` see branch callbacks, matched
|
|
383
|
+
against their branch.
|
|
384
|
+
|
|
385
|
+
A transition takes its callback chain when it starts: a `before_transition`
|
|
386
|
+
registered while that chain is running applies from the next transition on.
|
|
387
|
+
|
|
388
|
+
Validating before saving (ActiveModel and ActiveRecord with a pending event, e.g.
|
|
389
|
+
`valid?` before `save`) runs a preview of the transition: every
|
|
390
|
+
`before_transition` callback, skipping the `around_transition` callbacks (not
|
|
391
|
+
entered, their conditions not evaluated), then the validation in the new state,
|
|
392
|
+
and no after callbacks. Validation therefore sees everything the save's before
|
|
393
|
+
callbacks set. The save does not repeat those before callbacks; its around
|
|
394
|
+
callbacks wrap the save alone, on its own stack. So when `valid?` ran first, a
|
|
395
|
+
`before_transition` declared after an `around_transition` has already run outside
|
|
396
|
+
that around; a save without a prior `valid?` interleaves the callbacks exactly as
|
|
397
|
+
declared. An around callback never stays suspended between two calls.
|
|
398
|
+
|
|
399
|
+
`before`, `after`, `around` and `do` are reserved keys in a transition's hash, like
|
|
400
|
+
`from`, `to`, `if` and `unless`. `do:` is refused, since a branch has three kinds
|
|
401
|
+
of callbacks. A state named `:after` (or `:before`, `:around`) is written with
|
|
402
|
+
`from:`/`to:`:
|
|
403
|
+
|
|
404
|
+
```ruby
|
|
405
|
+
transition from: :after, to: :parked
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
The branches keep their callbacks in `Branch#callbacks`
|
|
409
|
+
(`Vehicle.state_machine.events[:ignite].branches.last.callbacks`), as
|
|
410
|
+
`StateMachines::Callback` objects with `#type` and `#callables`, for renderers
|
|
411
|
+
and tooling.
|
|
249
412
|
|
|
250
413
|
# Coordinated State Management
|
|
251
414
|
|
|
@@ -287,7 +450,7 @@ system = TorpedoSystem.new
|
|
|
287
450
|
system.fire_torpedo # => false (bay doors are closed)
|
|
288
451
|
|
|
289
452
|
system.open_bay_doors!
|
|
290
|
-
system.fire_torpedo # =>
|
|
453
|
+
system.fire_torpedo # => #<StateMachines::Transition ...> (bay doors are now open)
|
|
291
454
|
```
|
|
292
455
|
|
|
293
456
|
### Multiple State Guards
|
|
@@ -319,134 +482,41 @@ end
|
|
|
319
482
|
|
|
320
483
|
## Error Handling
|
|
321
484
|
|
|
322
|
-
|
|
485
|
+
A state guard must be a non-empty Hash of state machine name => state name; anything else raises
|
|
486
|
+
`ArgumentError` when the transition is defined.
|
|
487
|
+
|
|
488
|
+
Machine and state names are resolved when the guard is evaluated (the referenced machine may be
|
|
489
|
+
defined later in the class), so typos raise `ArgumentError` on the first `can_*?`/fire call:
|
|
323
490
|
|
|
324
491
|
```ruby
|
|
492
|
+
# Wrong shape - raises at definition time
|
|
493
|
+
transition green: :red, if_state: :shields
|
|
494
|
+
# => ArgumentError: :if_state must be a non-empty Hash of state machine name => state name, got :shields
|
|
495
|
+
|
|
325
496
|
# Referencing a non-existent state machine
|
|
326
|
-
|
|
497
|
+
transition green: :red, if_state: { nonexistent_machine: :some_state }
|
|
498
|
+
StarshipBridge.new.can_red_alert?
|
|
327
499
|
# => ArgumentError: State machine 'nonexistent_machine' is not defined for StarshipBridge
|
|
328
500
|
|
|
329
501
|
# Referencing a non-existent state
|
|
330
|
-
|
|
502
|
+
transition green: :red, if_state: { shields: :nonexistent_state }
|
|
503
|
+
StarshipBridge.new.can_red_alert?
|
|
331
504
|
# => ArgumentError: State 'nonexistent_state' is not defined in state machine 'shields'
|
|
332
505
|
```
|
|
333
506
|
|
|
334
507
|
# Asynchronous State Machines
|
|
335
508
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
## Platform Compatibility
|
|
341
|
-
|
|
342
|
-
**Supported Platforms:**
|
|
343
|
-
* MRI Ruby (CRuby) 3.2+
|
|
344
|
-
* Other Ruby engines with full Fiber scheduler support
|
|
345
|
-
|
|
346
|
-
**Unsupported Platforms:**
|
|
347
|
-
* JRuby - Falls back to synchronous mode with warnings
|
|
348
|
-
* TruffleRuby - Falls back to synchronous mode with warnings
|
|
349
|
-
|
|
350
|
-
## Basic Async Usage
|
|
351
|
-
|
|
352
|
-
Enable async mode by adding `async: true` to your state machine declaration:
|
|
353
|
-
|
|
354
|
-
```ruby
|
|
355
|
-
class AutonomousDrone < StarfleetShip
|
|
356
|
-
# Async-enabled state machine for autonomous operation
|
|
357
|
-
state_machine :status, async: true, initial: :docked do
|
|
358
|
-
event :launch do
|
|
359
|
-
transition docked: :flying
|
|
360
|
-
end
|
|
361
|
-
|
|
362
|
-
event :land do
|
|
363
|
-
transition flying: :docked
|
|
364
|
-
end
|
|
365
|
-
end
|
|
366
|
-
|
|
367
|
-
# Mixed configuration: some machines async, others sync
|
|
368
|
-
state_machine :teleporter_status, async: true, initial: :offline do
|
|
369
|
-
event :power_up do
|
|
370
|
-
transition offline: :charging
|
|
371
|
-
end
|
|
372
|
-
|
|
373
|
-
event :teleport do
|
|
374
|
-
transition ready: :teleporting
|
|
375
|
-
end
|
|
376
|
-
end
|
|
377
|
-
|
|
378
|
-
# Weapons remain synchronous for safety
|
|
379
|
-
state_machine :weapons, initial: :disarmed do
|
|
380
|
-
event :arm do
|
|
381
|
-
transition disarmed: :armed
|
|
382
|
-
end
|
|
383
|
-
end
|
|
384
|
-
end
|
|
385
|
-
```
|
|
386
|
-
|
|
387
|
-
## Async Event Methods
|
|
388
|
-
|
|
389
|
-
Async-enabled machines automatically generate async versions of event methods:
|
|
390
|
-
|
|
391
|
-
```ruby
|
|
392
|
-
drone = AutonomousDrone.new
|
|
393
|
-
|
|
394
|
-
# Within an Async context
|
|
395
|
-
Async do
|
|
396
|
-
# Async event firing - returns Async::Task
|
|
397
|
-
task = drone.launch_async
|
|
398
|
-
result = task.wait # => true
|
|
399
|
-
|
|
400
|
-
# Bang methods for strict error handling
|
|
401
|
-
drone.power_up_async! # => Async::Task (raises on failure)
|
|
402
|
-
|
|
403
|
-
# Generic async event firing
|
|
404
|
-
drone.fire_event_async(:teleport) # => Async::Task
|
|
405
|
-
end
|
|
406
|
-
|
|
407
|
-
# Outside Async context - raises error
|
|
408
|
-
drone.launch_async # => RuntimeError: launch_async must be called within an Async context
|
|
409
|
-
```
|
|
410
|
-
|
|
411
|
-
## Thread Safety
|
|
509
|
+
Async mode (`state_machine ..., async: true`, `<event>_async` methods, `fire_event_async`) lives
|
|
510
|
+
in the [state_machines-async](https://github.com/state-machines/state_machines-async) gem. Without it
|
|
511
|
+
loaded, `async: true` raises `ArgumentError` naming the gem rather than being ignored.
|
|
412
512
|
|
|
413
|
-
|
|
513
|
+
Extensions plug into machine hooks: `Machine#claims_transitions?` makes `TransitionCollection#perform`
|
|
514
|
+
claim the machine's attribute before performing (concurrent transitions out of one state then perform once,
|
|
515
|
+
the others are refused as `:contended`); `Machine#creation_options` / `Machine#after_definition` let a new
|
|
516
|
+
machine accept an extension's option (from the caller or the integration's defaults), and
|
|
517
|
+
`Machine#reopen_options` / `Machine#reopen` let a reopened machine accept it.
|
|
414
518
|
|
|
415
519
|
```ruby
|
|
416
|
-
# Concurrent operations are automatically thread-safe
|
|
417
|
-
threads = []
|
|
418
|
-
10.times do
|
|
419
|
-
threads << Thread.new do
|
|
420
|
-
Async do
|
|
421
|
-
drone.launch_async.wait
|
|
422
|
-
drone.land_async.wait
|
|
423
|
-
end
|
|
424
|
-
end
|
|
425
|
-
end
|
|
426
|
-
threads.each(&:join)
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
## Performance Considerations
|
|
430
|
-
|
|
431
|
-
* **Thread Safety**: Uses `Concurrent::ReentrantReadWriteLock` for optimal read/write performance.
|
|
432
|
-
* **Memory**: Each async-enabled object gets its own mutex (lazy-loaded).
|
|
433
|
-
* **Marshalling**: Objects with async state machines can be serialized (mutex excluded/recreated).
|
|
434
|
-
* **Mixed Mode**: You can mix async and sync state machines in the same class.
|
|
435
|
-
|
|
436
|
-
## Dependencies
|
|
437
|
-
|
|
438
|
-
Async functionality requires:
|
|
439
|
-
|
|
440
|
-
```ruby
|
|
441
|
-
# Gemfile (automatically scoped to MRI Ruby)
|
|
442
|
-
platform :ruby do
|
|
443
|
-
gem 'async', '>= 2.25.0'
|
|
444
|
-
gem 'concurrent-ruby', '>= 1.3.5'
|
|
445
|
-
end
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
*Note: These gems are only installed on supported platforms. JRuby/TruffleRuby won't attempt installation.*
|
|
449
|
-
|
|
450
520
|
# Human-friendly names can be accessed for states/events
|
|
451
521
|
Vehicle.human_state_name(:first_gear) # => "first gear"
|
|
452
522
|
Vehicle.human_alarm_state_name(:active) # => "active"
|
|
@@ -616,6 +686,37 @@ machine = Vehicle.state_machine(:state)
|
|
|
616
686
|
assert_after_transition(machine, on: :crash, do: :tow)
|
|
617
687
|
```
|
|
618
688
|
|
|
689
|
+
#### Refusal Reason Assertions
|
|
690
|
+
|
|
691
|
+
Assert *why* an event was refused (see [Refusal Reasons](#refusal-reasons)). Reasons are
|
|
692
|
+
validated against `:no_transition`, `:guard`, `:halted`, `:action_failed` and `:contended`, so a
|
|
693
|
+
typo raises `ArgumentError` instead of reporting a bogus refusal:
|
|
694
|
+
|
|
695
|
+
```ruby
|
|
696
|
+
# Fires the non-bang event: it must return false and leave the state unchanged
|
|
697
|
+
assert_sm_refused(vehicle, :park) # Any reason; returns the reason
|
|
698
|
+
assert_sm_refused(vehicle, :ignite, :guard) # Specific reason
|
|
699
|
+
|
|
700
|
+
# Reads the most recent refusal without firing
|
|
701
|
+
vehicle.park
|
|
702
|
+
assert_sm_refusal(vehicle, :park, :no_transition)
|
|
703
|
+
vehicle.ignite
|
|
704
|
+
refute_sm_refusal(vehicle, :ignite) # Nil after a success
|
|
705
|
+
|
|
706
|
+
# The bang raises StateMachines::InvalidTransition with that #reason (returns the error)
|
|
707
|
+
assert_sm_raises_refusal(vehicle, :park, :no_transition)
|
|
708
|
+
|
|
709
|
+
# fire_events returns false and fire_events! raises InvalidParallelTransition with these #reasons
|
|
710
|
+
assert_sm_parallel_refused(vehicle, %i[park enable_alarm], { park: :no_transition })
|
|
711
|
+
|
|
712
|
+
# Namespaced machines accept the plain or the qualified event name
|
|
713
|
+
assert_sm_refusal(vehicle, :enable, :guard, machine_name: :alarm_state)
|
|
714
|
+
assert_sm_refusal(vehicle, :enable_alarm, :guard, machine_name: :alarm_state)
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
RSpec-style aliases: `expect_to_be_refused`, `expect_refusal`, `expect_no_refusal`,
|
|
718
|
+
`expect_to_raise_refusal`, `expect_parallel_refused`.
|
|
719
|
+
|
|
619
720
|
#### Multiple State Machine Support
|
|
620
721
|
|
|
621
722
|
The TestHelper fully supports objects with multiple state machines:
|
|
@@ -685,7 +786,7 @@ method like so:
|
|
|
685
786
|
|
|
686
787
|
```ruby
|
|
687
788
|
vehicle = Vehicle.create # => #<Vehicle id=1 state="parked">
|
|
688
|
-
vehicle.ignite # =>
|
|
789
|
+
vehicle.ignite # => #<StateMachines::Transition attribute=:state event=:ignite ...>
|
|
689
790
|
vehicle.state # => "idling"
|
|
690
791
|
```
|
|
691
792
|
|
|
@@ -986,7 +1087,7 @@ end
|
|
|
986
1087
|
|
|
987
1088
|
vehicle = Vehicle.new # => #<Vehicle:0xb708412c @state="parked" ...>
|
|
988
1089
|
vehicle.state # => "parked"
|
|
989
|
-
vehicle.machine.ignite # =>
|
|
1090
|
+
vehicle.machine.ignite # => #<StateMachines::Transition attribute=:state event=:ignite ...>
|
|
990
1091
|
vehicle.machine.state # => "idling"
|
|
991
1092
|
vehicle.state # => "idling"
|
|
992
1093
|
vehicle.machine.state_transitions # => [#<StateMachines:Transition ...>]
|
|
@@ -1001,7 +1102,7 @@ transitions.
|
|
|
1001
1102
|
|
|
1002
1103
|
Ruby versions officially supported and tested:
|
|
1003
1104
|
|
|
1004
|
-
* Ruby (MRI) 3.
|
|
1105
|
+
* Ruby (MRI) 3.4+
|
|
1005
1106
|
|
|
1006
1107
|
For graphing state machine:
|
|
1007
1108
|
|
|
@@ -16,6 +16,14 @@ module StateMachines
|
|
|
16
16
|
# The condition that must *not* be met on an object
|
|
17
17
|
attr_reader :unless_condition
|
|
18
18
|
|
|
19
|
+
# Conditions on the state of other machines
|
|
20
|
+
attr_reader :if_state_condition, :unless_state_condition,
|
|
21
|
+
:if_all_states_condition, :unless_all_states_condition,
|
|
22
|
+
:if_any_state_condition, :unless_any_state_condition
|
|
23
|
+
|
|
24
|
+
# The state guards that are set, keyed by option
|
|
25
|
+
attr_reader :state_guards
|
|
26
|
+
|
|
19
27
|
# The requirement for verifying the event being matched
|
|
20
28
|
attr_reader :event_requirement
|
|
21
29
|
|
|
@@ -29,17 +37,46 @@ module StateMachines
|
|
|
29
37
|
# * +to+ / +except_to+
|
|
30
38
|
attr_reader :known_states
|
|
31
39
|
|
|
40
|
+
# The callbacks scoped to this branch by an event's
|
|
41
|
+
# <tt>transition ..., before: / after: / around:</tt>, in the order they
|
|
42
|
+
# were given, as StateMachines::Callback instances (see Callback#type and
|
|
43
|
+
# Callback#callables). They run only when this branch is the one selected
|
|
44
|
+
# for a transition, so this branch's guards apply to them. Frozen; empty
|
|
45
|
+
# for branches without callbacks.
|
|
46
|
+
attr_reader :callbacks
|
|
47
|
+
|
|
48
|
+
# Key under which Event#transition hands over the branch callbacks: not a
|
|
49
|
+
# Symbol, so no state name can collide with it
|
|
50
|
+
CALLBACKS = Object.new.freeze
|
|
51
|
+
private_constant :CALLBACKS
|
|
52
|
+
|
|
53
|
+
# Builds the options hash for a branch with callbacks (used by Event)
|
|
54
|
+
def self.options_with_callbacks(options, callbacks) # :nodoc:
|
|
55
|
+
options.merge(CALLBACKS => callbacks)
|
|
56
|
+
end
|
|
57
|
+
|
|
32
58
|
# Creates a new branch
|
|
33
59
|
def initialize(options = {}) # :nodoc:
|
|
60
|
+
options = options.dup
|
|
61
|
+
@callbacks = (options.delete(CALLBACKS) || []).freeze
|
|
62
|
+
|
|
34
63
|
# Build conditionals
|
|
35
64
|
@if_condition = options.delete(:if)
|
|
36
65
|
@unless_condition = options.delete(:unless)
|
|
37
|
-
@if_state_condition = options
|
|
38
|
-
@unless_state_condition = options
|
|
39
|
-
@if_all_states_condition = options
|
|
40
|
-
@unless_all_states_condition = options
|
|
41
|
-
@if_any_state_condition = options
|
|
42
|
-
@unless_any_state_condition = options
|
|
66
|
+
@if_state_condition = build_state_guard(options, :if_state)
|
|
67
|
+
@unless_state_condition = build_state_guard(options, :unless_state)
|
|
68
|
+
@if_all_states_condition = build_state_guard(options, :if_all_states)
|
|
69
|
+
@unless_all_states_condition = build_state_guard(options, :unless_all_states)
|
|
70
|
+
@if_any_state_condition = build_state_guard(options, :if_any_state)
|
|
71
|
+
@unless_any_state_condition = build_state_guard(options, :unless_any_state)
|
|
72
|
+
@state_guards = {
|
|
73
|
+
if_state: @if_state_condition,
|
|
74
|
+
unless_state: @unless_state_condition,
|
|
75
|
+
if_all_states: @if_all_states_condition,
|
|
76
|
+
unless_all_states: @unless_all_states_condition,
|
|
77
|
+
if_any_state: @if_any_state_condition,
|
|
78
|
+
unless_any_state: @unless_any_state_condition
|
|
79
|
+
}.compact.freeze
|
|
43
80
|
|
|
44
81
|
# Build event requirement
|
|
45
82
|
@event_requirement = build_matcher(options, :on, :except_on)
|
|
@@ -94,7 +131,7 @@ module StateMachines
|
|
|
94
131
|
def matches?(object, query = {})
|
|
95
132
|
!match(object, query).nil?
|
|
96
133
|
end
|
|
97
|
-
|
|
134
|
+
|
|
98
135
|
# Alias for Minitest's assert_match
|
|
99
136
|
alias =~ matches?
|
|
100
137
|
|
|
@@ -134,7 +171,7 @@ module StateMachines
|
|
|
134
171
|
end
|
|
135
172
|
|
|
136
173
|
def draw(graph, event, valid_states, io = $stdout)
|
|
137
|
-
machine.renderer.draw_branch(self, graph, event, valid_states, io)
|
|
174
|
+
event.machine.renderer.draw_branch(self, graph, event, valid_states, io)
|
|
138
175
|
end
|
|
139
176
|
|
|
140
177
|
protected
|
|
@@ -158,6 +195,15 @@ module StateMachines
|
|
|
158
195
|
end
|
|
159
196
|
end
|
|
160
197
|
|
|
198
|
+
# Frozen copy, since branches are shared with subclasses
|
|
199
|
+
def build_state_guard(options, option)
|
|
200
|
+
conditions = options.delete(option)
|
|
201
|
+
return if conditions.nil?
|
|
202
|
+
raise ArgumentError, ":#{option} must be a non-empty Hash of state machine name => state name, got #{conditions.inspect}" unless conditions.is_a?(Hash) && !conditions.empty?
|
|
203
|
+
|
|
204
|
+
conditions.dup.freeze
|
|
205
|
+
end
|
|
206
|
+
|
|
161
207
|
# Verifies that all configured requirements (event and state) match the
|
|
162
208
|
# given query. If a match is found, then a hash containing the
|
|
163
209
|
# event/state requirements that passed will be returned; otherwise, nil.
|
|
@@ -199,16 +245,6 @@ module StateMachines
|
|
|
199
245
|
|
|
200
246
|
return false unless if_passes && unless_passes
|
|
201
247
|
|
|
202
|
-
# Consolidate all state guards
|
|
203
|
-
state_guards = {
|
|
204
|
-
if_state: @if_state_condition,
|
|
205
|
-
unless_state: @unless_state_condition,
|
|
206
|
-
if_all_states: @if_all_states_condition,
|
|
207
|
-
unless_all_states: @unless_all_states_condition,
|
|
208
|
-
if_any_state: @if_any_state_condition,
|
|
209
|
-
unless_any_state: @unless_any_state_condition
|
|
210
|
-
}.compact
|
|
211
|
-
|
|
212
248
|
return true if state_guards.empty?
|
|
213
249
|
|
|
214
250
|
validate_and_check_state_guards(object, state_guards)
|
|
@@ -221,14 +257,12 @@ module StateMachines
|
|
|
221
257
|
case guard_type
|
|
222
258
|
when :if_state, :if_all_states
|
|
223
259
|
conditions.all? { |machine, state| check_state(object, machine, state) }
|
|
224
|
-
when :unless_state
|
|
260
|
+
when :unless_state, :unless_any_state
|
|
225
261
|
conditions.none? { |machine, state| check_state(object, machine, state) }
|
|
226
262
|
when :if_any_state
|
|
227
263
|
conditions.any? { |machine, state| check_state(object, machine, state) }
|
|
228
264
|
when :unless_all_states
|
|
229
265
|
!conditions.all? { |machine, state| check_state(object, machine, state) }
|
|
230
|
-
when :unless_any_state
|
|
231
|
-
conditions.none? { |machine, state| check_state(object, machine, state) }
|
|
232
266
|
end
|
|
233
267
|
end
|
|
234
268
|
end
|
|
@@ -240,7 +274,7 @@ module StateMachines
|
|
|
240
274
|
state = machine.states[state_name]
|
|
241
275
|
raise ArgumentError, "State '#{state_name}' is not defined in state machine '#{machine_name}'" unless state
|
|
242
276
|
|
|
243
|
-
state.matches?(
|
|
277
|
+
state.matches?(machine.read(object, :state))
|
|
244
278
|
end
|
|
245
279
|
end
|
|
246
280
|
end
|
|
@@ -108,6 +108,18 @@ module StateMachines
|
|
|
108
108
|
# See StateMachines::Branch for more information.
|
|
109
109
|
attr_reader :branch
|
|
110
110
|
|
|
111
|
+
# The methods this callback runs, as configured: Symbols / Strings naming
|
|
112
|
+
# methods on the object, and the user's own Procs and Methods, in order
|
|
113
|
+
# (positional arguments, then +:do+, then the block). With
|
|
114
|
+
# +bind_to_object+ the Procs are still the user's own, so their identity,
|
|
115
|
+
# +source_location+ and +inspect+ describe the user's code. Frozen.
|
|
116
|
+
#
|
|
117
|
+
# callback = StateMachines::Callback.new(:before, :log, do: ->(vehicle) {})
|
|
118
|
+
# callback.callables # => [:log, #<Proc:0x...(lambda)>]
|
|
119
|
+
def callables
|
|
120
|
+
@methods
|
|
121
|
+
end
|
|
122
|
+
|
|
111
123
|
# Creates a new callback that can get called based on the configured
|
|
112
124
|
# options.
|
|
113
125
|
#
|
|
@@ -134,11 +146,13 @@ module StateMachines
|
|
|
134
146
|
|
|
135
147
|
options = { bind_to_object: self.class.bind_to_object, terminator: self.class.terminator }.merge(options)
|
|
136
148
|
|
|
137
|
-
#
|
|
149
|
+
# Blocks bound to the object run through a proxy, but @methods keeps the
|
|
150
|
+
# user's own blocks: their identity, source_location and inspect are
|
|
151
|
+
# what introspection and renderers report, not the proxy's
|
|
138
152
|
bind_to_object = options.delete(:bind_to_object)
|
|
139
|
-
@
|
|
140
|
-
|
|
141
|
-
|
|
153
|
+
@bound_methods = {}.compare_by_identity
|
|
154
|
+
@methods.each { |method| @bound_methods[method] ||= bound_method(method) if bind_to_object && method.is_a?(Proc) }
|
|
155
|
+
@methods.freeze
|
|
142
156
|
|
|
143
157
|
@terminator = options.delete(:terminator)
|
|
144
158
|
@branch = Branch.new(options)
|
|
@@ -176,15 +190,15 @@ module StateMachines
|
|
|
176
190
|
# For all other types of callbacks, this will evaluate each method in
|
|
177
191
|
# order. The callback will only halt if the resulting value from the
|
|
178
192
|
# method passes the terminator.
|
|
179
|
-
def run_methods(object, context = {}, index = 0,
|
|
193
|
+
def run_methods(object, context = {}, index = 0, *, &)
|
|
180
194
|
case type
|
|
181
195
|
when :around
|
|
182
196
|
current_method = @methods[index]
|
|
183
197
|
if current_method
|
|
184
198
|
yielded = false
|
|
185
|
-
evaluate_method(object, current_method, *
|
|
199
|
+
evaluate_method(object, invoker(current_method), *) do
|
|
186
200
|
yielded = true
|
|
187
|
-
run_methods(object, context, index + 1,
|
|
201
|
+
run_methods(object, context, index + 1, *, &)
|
|
188
202
|
end
|
|
189
203
|
|
|
190
204
|
throw :halt unless yielded
|
|
@@ -193,12 +207,18 @@ module StateMachines
|
|
|
193
207
|
end
|
|
194
208
|
else
|
|
195
209
|
@methods.each do |method|
|
|
196
|
-
result = evaluate_method(object, method, *
|
|
210
|
+
result = evaluate_method(object, invoker(method), *)
|
|
197
211
|
throw :halt if @terminator&.call(result)
|
|
198
212
|
end
|
|
199
213
|
end
|
|
200
214
|
end
|
|
201
215
|
|
|
216
|
+
# The callable to evaluate for a configured method: its bound proxy when
|
|
217
|
+
# the callback binds blocks to the object, otherwise the method itself
|
|
218
|
+
def invoker(method)
|
|
219
|
+
@bound_methods.fetch(method, method)
|
|
220
|
+
end
|
|
221
|
+
|
|
202
222
|
# Generates a method that can be bound to the object being transitioned
|
|
203
223
|
# when the callback is invoked
|
|
204
224
|
def bound_method(block)
|