state_machines 0.202.0 → 0.301.2

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.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +160 -14
  3. data/lib/state_machines/async_mode/async_event_extensions.rb +6 -10
  4. data/lib/state_machines/async_mode/async_events.rb +56 -72
  5. data/lib/state_machines/async_mode/async_machine.rb +3 -5
  6. data/lib/state_machines/async_mode/async_transition_collection.rb +32 -104
  7. data/lib/state_machines/async_mode/thread_safe_state.rb +8 -4
  8. data/lib/state_machines/branch.rb +35 -22
  9. data/lib/state_machines/callback.rb +4 -4
  10. data/lib/state_machines/core.rb +1 -0
  11. data/lib/state_machines/error.rb +37 -0
  12. data/lib/state_machines/event.rb +125 -2
  13. data/lib/state_machines/event_collection.rb +6 -1
  14. data/lib/state_machines/extensions.rb +22 -0
  15. data/lib/state_machines/helper_module.rb +1 -0
  16. data/lib/state_machines/machine/class_methods.rb +15 -3
  17. data/lib/state_machines/machine/configuration.rb +5 -3
  18. data/lib/state_machines/machine/event_methods.rb +5 -0
  19. data/lib/state_machines/machine/helper_generators.rb +3 -1
  20. data/lib/state_machines/machine/utilities.rb +1 -1
  21. data/lib/state_machines/machine.rb +8 -0
  22. data/lib/state_machines/machine_collection.rb +7 -6
  23. data/lib/state_machines/macro_methods.rb +4 -4
  24. data/lib/state_machines/path.rb +1 -0
  25. data/lib/state_machines/path_collection.rb +1 -0
  26. data/lib/state_machines/state_context.rb +1 -0
  27. data/lib/state_machines/stdio_renderer.rb +8 -6
  28. data/lib/state_machines/syntax_validator.rb +3 -3
  29. data/lib/state_machines/test_helper.rb +228 -27
  30. data/lib/state_machines/transition.rb +19 -5
  31. data/lib/state_machines/transition_claim.rb +118 -0
  32. data/lib/state_machines/transition_collection.rb +142 -18
  33. data/lib/state_machines/version.rb +1 -1
  34. metadata +8 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e579c5afa633f1402bfd1e1a5ebd0c446ff410eb3b06940d74dce0d0e024f530
4
- data.tar.gz: 629c2f403c71e966e98c5d35c64fa8ffa99caaa8e5aced5e205a569eb867e811
3
+ metadata.gz: 4f3fa05a4995ba6d3ff323c8cbf7303a061bf082237353066c01b023129d4c7f
4
+ data.tar.gz: c56a60d5f74f1da251670ff4f5c782d68837a6db2f2f4c200d744adebe620b7f
5
5
  SHA512:
6
- metadata.gz: 1bf56f62b3f6e495fc799ba48d499c0bb7325dfdb3c24d1f8aafc2c9daaade581bef63495e8562a633ee32d2ca3870e8e8131052d97bb152c793ab86f0a3c0fb
7
- data.tar.gz: f2ae6e891727b60a4310160fbfa7044b4b949a8bccb9f8f80d7d3733dda6e528bf14f3449e7ab420b35d84ef971a6e6349e43a162e16b735e0be61e2ae7571be
6
+ metadata.gz: f6ef8c77f4652d1281e4b66252e0f72395efc2143180b1e91cb437ac7934ca5d9238f3e9b56c5ab3ca7ede1e3a4123fe3ffde899b02f565601b056e7477f0884
7
+ data.tar.gz: 643d203b7719acd77ec70df09bd4c46b722127bf0529e3108285ac5120ea5b0269fe195e09448664cdabcf70a86232a6496ff8f33e8e4943bc64d806b00c28d6
data/README.md CHANGED
@@ -197,19 +197,19 @@ vehicle.state_transitions # => [#<StateMachines:Transition attribute=:stat
197
197
  vehicle.speed # => 0
198
198
  vehicle.moving? # => false
199
199
 
200
- vehicle.ignite # => true
200
+ vehicle.ignite # => #<StateMachines::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>
201
201
  vehicle.parked? # => false
202
202
  vehicle.idling? # => true
203
203
  vehicle.speed # => 10
204
204
  vehicle # => #<Vehicle:0xb7cf4eac @state="idling", @seatbelt_on=true>
205
205
 
206
- vehicle.shift_up # => true
206
+ vehicle.shift_up # => #<StateMachines::Transition attribute=:state event=:shift_up from="idling" from_name=:idling to="first_gear" to_name=:first_gear>
207
207
  vehicle.speed # => 10
208
208
  vehicle.moving? # => true
209
209
  vehicle # => #<Vehicle:0xb7cf4eac @state="first_gear", @seatbelt_on=true>
210
210
 
211
211
  # A generic event helper is available to fire without going through the event's instance method
212
- vehicle.fire_state_event(:shift_up) # => true
212
+ vehicle.fire_state_event(:shift_up) # => #<StateMachines::Transition attribute=:state event=:shift_up ...>
213
213
 
214
214
  # Call state-driven behavior that's undefined for the state raises a NoMethodError
215
215
  vehicle.speed # => NoMethodError: super: no superclass method `speed' for #<Vehicle:0xb7cf4eac>
@@ -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') # => true
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 # => true
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,110 @@ 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` | Async mode only: a concurrent transition held or moved the attribute first |
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)`.
249
353
 
250
354
  # Coordinated State Management
251
355
 
@@ -287,7 +391,7 @@ system = TorpedoSystem.new
287
391
  system.fire_torpedo # => false (bay doors are closed)
288
392
 
289
393
  system.open_bay_doors!
290
- system.fire_torpedo # => true (bay doors are now open)
394
+ system.fire_torpedo # => #<StateMachines::Transition ...> (bay doors are now open)
291
395
  ```
292
396
 
293
397
  ### Multiple State Guards
@@ -319,15 +423,25 @@ end
319
423
 
320
424
  ## Error Handling
321
425
 
322
- State guards provide comprehensive error checking:
426
+ A state guard must be a non-empty Hash of state machine name => state name; anything else raises
427
+ `ArgumentError` when the transition is defined.
428
+
429
+ Machine and state names are resolved when the guard is evaluated (the referenced machine may be
430
+ defined later in the class), so typos raise `ArgumentError` on the first `can_*?`/fire call:
323
431
 
324
432
  ```ruby
433
+ # Wrong shape - raises at definition time
434
+ transition green: :red, if_state: :shields
435
+ # => ArgumentError: :if_state must be a non-empty Hash of state machine name => state name, got :shields
436
+
325
437
  # Referencing a non-existent state machine
326
- event :invalid, if_state: { nonexistent_machine: :some_state }
438
+ transition green: :red, if_state: { nonexistent_machine: :some_state }
439
+ StarshipBridge.new.can_red_alert?
327
440
  # => ArgumentError: State machine 'nonexistent_machine' is not defined for StarshipBridge
328
441
 
329
442
  # Referencing a non-existent state
330
- event :another_invalid, if_state: { shields: :nonexistent_state }
443
+ transition green: :red, if_state: { shields: :nonexistent_state }
444
+ StarshipBridge.new.can_red_alert?
331
445
  # => ArgumentError: State 'nonexistent_state' is not defined in state machine 'shields'
332
446
  ```
333
447
 
@@ -340,7 +454,7 @@ This feature is powered by the [async](https://github.com/socketry/async) gem an
340
454
  ## Platform Compatibility
341
455
 
342
456
  **Supported Platforms:**
343
- * MRI Ruby (CRuby) 3.2+
457
+ * MRI Ruby (CRuby) 3.4+
344
458
  * Other Ruby engines with full Fiber scheduler support
345
459
 
346
460
  **Unsupported Platforms:**
@@ -395,7 +509,7 @@ drone = AutonomousDrone.new
395
509
  Async do
396
510
  # Async event firing - returns Async::Task
397
511
  task = drone.launch_async
398
- result = task.wait # => true
512
+ result = task.wait # => #<StateMachines::Transition ...>
399
513
 
400
514
  # Bang methods for strict error handling
401
515
  drone.power_up_async! # => Async::Task (raises on failure)
@@ -447,6 +561,7 @@ end
447
561
 
448
562
  *Note: These gems are only installed on supported platforms. JRuby/TruffleRuby won't attempt installation.*
449
563
 
564
+ ```ruby
450
565
  # Human-friendly names can be accessed for states/events
451
566
  Vehicle.human_state_name(:first_gear) # => "first gear"
452
567
  Vehicle.human_alarm_state_name(:active) # => "active"
@@ -616,6 +731,37 @@ machine = Vehicle.state_machine(:state)
616
731
  assert_after_transition(machine, on: :crash, do: :tow)
617
732
  ```
618
733
 
734
+ #### Refusal Reason Assertions
735
+
736
+ Assert *why* an event was refused (see [Refusal Reasons](#refusal-reasons)). Reasons are
737
+ validated against `:no_transition`, `:guard`, `:halted`, `:action_failed` and `:contended`, so a
738
+ typo raises `ArgumentError` instead of reporting a bogus refusal:
739
+
740
+ ```ruby
741
+ # Fires the non-bang event: it must return false and leave the state unchanged
742
+ assert_sm_refused(vehicle, :park) # Any reason; returns the reason
743
+ assert_sm_refused(vehicle, :ignite, :guard) # Specific reason
744
+
745
+ # Reads the most recent refusal without firing
746
+ vehicle.park
747
+ assert_sm_refusal(vehicle, :park, :no_transition)
748
+ vehicle.ignite
749
+ refute_sm_refusal(vehicle, :ignite) # Nil after a success
750
+
751
+ # The bang raises StateMachines::InvalidTransition with that #reason (returns the error)
752
+ assert_sm_raises_refusal(vehicle, :park, :no_transition)
753
+
754
+ # fire_events returns false and fire_events! raises InvalidParallelTransition with these #reasons
755
+ assert_sm_parallel_refused(vehicle, %i[park enable_alarm], { park: :no_transition })
756
+
757
+ # Namespaced machines accept the plain or the qualified event name
758
+ assert_sm_refusal(vehicle, :enable, :guard, machine_name: :alarm_state)
759
+ assert_sm_refusal(vehicle, :enable_alarm, :guard, machine_name: :alarm_state)
760
+ ```
761
+
762
+ RSpec-style aliases: `expect_to_be_refused`, `expect_refusal`, `expect_no_refusal`,
763
+ `expect_to_raise_refusal`, `expect_parallel_refused`.
764
+
619
765
  #### Multiple State Machine Support
620
766
 
621
767
  The TestHelper fully supports objects with multiple state machines:
@@ -685,7 +831,7 @@ method like so:
685
831
 
686
832
  ```ruby
687
833
  vehicle = Vehicle.create # => #<Vehicle id=1 state="parked">
688
- vehicle.ignite # => true
834
+ vehicle.ignite # => #<StateMachines::Transition attribute=:state event=:ignite ...>
689
835
  vehicle.state # => "idling"
690
836
  ```
691
837
 
@@ -986,7 +1132,7 @@ end
986
1132
 
987
1133
  vehicle = Vehicle.new # => #<Vehicle:0xb708412c @state="parked" ...>
988
1134
  vehicle.state # => "parked"
989
- vehicle.machine.ignite # => true
1135
+ vehicle.machine.ignite # => #<StateMachines::Transition attribute=:state event=:ignite ...>
990
1136
  vehicle.machine.state # => "idling"
991
1137
  vehicle.state # => "idling"
992
1138
  vehicle.machine.state_transitions # => [#<StateMachines:Transition ...>]
@@ -1001,7 +1147,7 @@ transitions.
1001
1147
 
1002
1148
  Ruby versions officially supported and tested:
1003
1149
 
1004
- * Ruby (MRI) 3.0.0+
1150
+ * Ruby (MRI) 3.4+
1005
1151
 
1006
1152
  For graphing state machine:
1007
1153
 
@@ -5,21 +5,19 @@ module StateMachines
5
5
  # Extensions to Event class for async bang methods
6
6
  module AsyncEventExtensions
7
7
  # Generate async bang methods for events when async mode is enabled
8
- def define_helper(scope, method, *args, &block)
8
+ def define_helper(scope, method, *args, &)
9
9
  result = super
10
10
 
11
11
  # If this is an async-enabled machine and we're defining an event method
12
- if scope == :instance && method !~ /_async[!]?$/ && machine.async_mode_enabled?
12
+ if scope == :instance && method !~ /_async!?$/ && machine.async_mode_enabled?
13
13
  qualified_name = method.to_s
14
14
 
15
15
  # Create async version that returns a task
16
- machine.define_helper(scope, "#{qualified_name}_async") do |machine, object, *method_args, **kwargs|
16
+ machine.define_helper(scope, "#{qualified_name}_async") do |_machine, object, *method_args, **kwargs|
17
17
  # Find the machine that has this event
18
18
  target_machine = object.class.state_machines.values.find { |m| m.events[name] }
19
19
 
20
- unless defined?(::Async::Task) && ::Async::Task.current?
21
- raise RuntimeError, "#{qualified_name}_async must be called within an Async context"
22
- end
20
+ raise "#{qualified_name}_async must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
23
21
 
24
22
  Async do
25
23
  target_machine.events[name].fire(object, *method_args, **kwargs)
@@ -27,13 +25,11 @@ module StateMachines
27
25
  end
28
26
 
29
27
  # Create async bang version that raises exceptions when awaited
30
- machine.define_helper(scope, "#{qualified_name}_async!") do |machine, object, *method_args, **kwargs|
28
+ machine.define_helper(scope, "#{qualified_name}_async!") do |_machine, object, *method_args, **kwargs|
31
29
  # Find the machine that has this event
32
30
  target_machine = object.class.state_machines.values.find { |m| m.events[name] }
33
31
 
34
- unless defined?(::Async::Task) && ::Async::Task.current?
35
- raise RuntimeError, "#{qualified_name}_async! must be called within an Async context"
36
- end
32
+ raise "#{qualified_name}_async! must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
37
33
 
38
34
  Async do
39
35
  # Use fire method which will raise exceptions on invalid transitions
@@ -7,36 +7,36 @@ module StateMachines
7
7
  # Fires an event asynchronously using Async
8
8
  # Returns an Async::Task that can be awaited for the result
9
9
  #
10
- # Example:
10
+ # @return [Async::Task] a task whose result is the performed
11
+ # {StateMachines::Transition}, or false if the event was refused
12
+ # @example
11
13
  # Async do
12
14
  # task = vehicle.async_fire_event(:ignite)
13
- # result = task.wait # => true/false
15
+ # result = task.wait # => #<StateMachines::Transition ...> or false
14
16
  # end
15
- def async_fire_event(event_name, *args)
17
+ def async_fire_event(event_name, *)
16
18
  # Find the machine that has this event
17
19
  machine = self.class.state_machines.values.find { |m| m.events[event_name] }
18
20
 
19
- unless machine
20
- raise ArgumentError, "Event #{event_name} not found in any state machine"
21
- end
21
+ raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
22
22
 
23
23
  # Must be called within an Async context
24
- unless defined?(::Async::Task) && ::Async::Task.current?
25
- raise RuntimeError, "async_fire_event must be called within an Async context. Use: Async { vehicle.async_fire_event(:event) }"
26
- end
24
+ raise 'async_fire_event must be called within an Async context. Use: Async { vehicle.async_fire_event(:event) }' unless defined?(::Async::Task) && ::Async::Task.current?
27
25
 
28
26
  Async do
29
- machine.events[event_name].fire(self, *args)
27
+ machine.events[event_name].fire(self, *)
30
28
  end
31
29
  end
32
30
 
33
31
  # Fires multiple events asynchronously across different state machines
34
32
  # Returns an array of Async::Tasks for concurrent execution
35
33
  #
36
- # Example:
34
+ # @return [Array<Async::Task>] one task per event, each resolving to the
35
+ # performed {StateMachines::Transition} or false
36
+ # @example
37
37
  # Async do
38
38
  # tasks = vehicle.async_fire_events(:ignite, :buy_insurance)
39
- # results = tasks.map(&:wait) # => [true, true]
39
+ # results = tasks.map(&:wait) # => [#<StateMachines::Transition ...>, #<StateMachines::Transition ...>]
40
40
  # end
41
41
  def async_fire_events(*event_names)
42
42
  event_names.map { |event_name| async_fire_event(event_name) }
@@ -45,24 +45,25 @@ module StateMachines
45
45
  # Fires an event asynchronously and waits for completion
46
46
  # This is a convenience method that creates and waits for the task
47
47
  #
48
- # Example:
49
- # result = vehicle.fire_event_async(:ignite) # => true/false
50
- def fire_event_async(event_name, *args)
48
+ # @return [StateMachines::Transition, false] the performed transition, or
49
+ # false if the event was refused
50
+ # @example
51
+ # result = vehicle.fire_event_async(:ignite) # => #<StateMachines::Transition ...> or false
52
+ def fire_event_async(event_name, *)
51
53
  raise NoMethodError, "undefined method `fire_event_async' for #{self}" unless has_async_machines?
54
+
52
55
  # Find the machine that has this event
53
56
  machine = self.class.state_machines.values.find { |m| m.events[event_name] }
54
57
 
55
- unless machine
56
- raise ArgumentError, "Event #{event_name} not found in any state machine"
57
- end
58
+ raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
58
59
 
59
60
  if defined?(::Async::Task) && ::Async::Task.current?
60
61
  # Already in async context, just fire directly
61
- machine.events[event_name].fire(self, *args)
62
+ machine.events[event_name].fire(self, *)
62
63
  else
63
64
  # Create async context and wait for result
64
65
  Async do
65
- machine.events[event_name].fire(self, *args)
66
+ machine.events[event_name].fire(self, *)
66
67
  end.wait
67
68
  end
68
69
  end
@@ -70,10 +71,13 @@ module StateMachines
70
71
  # Fires multiple events asynchronously and waits for all completions
71
72
  # Returns results in the same order as the input events
72
73
  #
73
- # Example:
74
- # results = vehicle.fire_events_async(:ignite, :buy_insurance) # => [true, true]
74
+ # @return [Array<StateMachines::Transition, false>] the performed
75
+ # transition (or false) for each event, in input order
76
+ # @example
77
+ # results = vehicle.fire_events_async(:ignite, :buy_insurance) # => [#<StateMachines::Transition ...>, #<StateMachines::Transition ...>]
75
78
  def fire_events_async(*event_names)
76
79
  raise NoMethodError, "undefined method `fire_events_async' for #{self}" unless has_async_machines?
80
+
77
81
  if defined?(::Async::Task) && ::Async::Task.current?
78
82
  # Already in async context, run concurrently
79
83
  tasks = event_names.map { |event_name| async_fire_event(event_name) }
@@ -100,21 +104,17 @@ module StateMachines
100
104
  # puts "Transition failed: #{e.message}"
101
105
  # end
102
106
  # end
103
- def async_fire_event!(event_name, *args)
107
+ def async_fire_event!(event_name, *)
104
108
  # Find the machine that has this event
105
109
  machine = self.class.state_machines.values.find { |m| m.events[event_name] }
106
110
 
107
- unless machine
108
- raise ArgumentError, "Event #{event_name} not found in any state machine"
109
- end
111
+ raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
110
112
 
111
113
  # Must be called within an Async context
112
- unless defined?(::Async::Task) && ::Async::Task.current?
113
- raise RuntimeError, "async_fire_event! must be called within an Async context. Use: Async { vehicle.async_fire_event!(:event) }"
114
- end
114
+ raise 'async_fire_event! must be called within an Async context. Use: Async { vehicle.async_fire_event!(:event) }' unless defined?(::Async::Task) && ::Async::Task.current?
115
115
 
116
116
  Async do
117
- fire_with_bang(machine, event_name, *args)
117
+ fire_with_bang(machine, event_name, *)
118
118
  end
119
119
  end
120
120
 
@@ -128,29 +128,28 @@ module StateMachines
128
128
  # rescue StateMachines::InvalidTransition => e
129
129
  # puts "Transition failed: #{e.message}"
130
130
  # end
131
- def fire_event_async!(event_name, *args)
131
+ def fire_event_async!(event_name, *)
132
132
  raise NoMethodError, "undefined method `fire_event_async!' for #{self}" unless has_async_machines?
133
+
133
134
  # Find the machine that has this event
134
135
  machine = self.class.state_machines.values.find { |m| m.events[event_name] }
135
136
 
136
- unless machine
137
- raise ArgumentError, "Event #{event_name} not found in any state machine"
138
- end
137
+ raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
139
138
 
140
139
  if defined?(::Async::Task) && ::Async::Task.current?
141
140
  # Already in async context, just fire directly with bang behavior
142
- fire_with_bang(machine, event_name, *args)
141
+ fire_with_bang(machine, event_name, *)
143
142
  else
144
143
  # Create async context and wait for result (may raise exception)
145
144
  Async do
146
- fire_with_bang(machine, event_name, *args)
145
+ fire_with_bang(machine, event_name, *)
147
146
  end.wait
148
147
  end
149
148
  end
150
149
 
151
150
  # Dynamically handle individual event async methods
152
151
  # This provides launch_async, launch_async!, arm_weapons_async, etc.
153
- def method_missing(method_name, *args, **kwargs, &block)
152
+ def method_missing(method_name, *, **, &)
154
153
  method_str = method_name.to_s
155
154
 
156
155
  # Check if this is an async event method
@@ -159,17 +158,13 @@ module StateMachines
159
158
  base_method = method_str.chomp('_async!').to_sym
160
159
 
161
160
  # Check if the base method exists and this machine is async-enabled
162
- if respond_to?(base_method) && async_method_for_event?(base_method)
163
- return handle_individual_event_async_bang(base_method, *args, **kwargs)
164
- end
161
+ return handle_individual_event_async_bang(base_method, *, **) if respond_to?(base_method) && async_method_for_event?(base_method)
165
162
  elsif method_str.end_with?('_async')
166
163
  # Remove the _async suffix to get the base event method
167
164
  base_method = method_str.chomp('_async').to_sym
168
165
 
169
166
  # Check if the base method exists and this machine is async-enabled
170
- if respond_to?(base_method) && async_method_for_event?(base_method)
171
- return handle_individual_event_async(base_method, *args, **kwargs)
172
- end
167
+ return handle_individual_event_async(base_method, *, **) if respond_to?(base_method) && async_method_for_event?(base_method)
173
168
  end
174
169
 
175
170
  # If not an async method, call the original method_missing
@@ -193,7 +188,7 @@ module StateMachines
193
188
 
194
189
  # Check if this object has any async-enabled state machines
195
190
  def has_async_machines?
196
- self.class.state_machines.any? { |name, machine| machine.async_mode_enabled? }
191
+ self.class.state_machines.any? { |_name, machine| machine.async_mode_enabled? }
197
192
  end
198
193
 
199
194
  private
@@ -206,43 +201,36 @@ module StateMachines
206
201
  # Check if this event method should have async versions
207
202
  def async_method_for_event?(event_method)
208
203
  # Find which machine contains this event
209
- self.class.state_machines.each do |name, machine|
210
- if machine.async_mode_enabled?
211
- # Check if this event method belongs to this machine
212
- machine.events.each do |event|
213
- qualified_name = event.qualified_name
214
- if qualified_name.to_sym == event_method || "#{qualified_name}!".to_sym == event_method
215
- return true
216
- end
217
- end
204
+ self.class.state_machines.each do |_name, machine|
205
+ next unless machine.async_mode_enabled?
206
+
207
+ # Check if this event method belongs to this machine
208
+ machine.events.each do |event|
209
+ qualified_name = event.qualified_name
210
+ return true if qualified_name.to_sym == event_method || :"#{qualified_name}!" == event_method
218
211
  end
219
212
  end
220
213
  false
221
214
  end
222
215
 
223
216
  # Handle individual event async methods (returns task)
224
- def handle_individual_event_async(event_method, *args, **kwargs)
225
-
226
- unless defined?(::Async::Task) && ::Async::Task.current?
227
- raise RuntimeError, "#{event_method}_async must be called within an Async context"
228
- end
217
+ def handle_individual_event_async(event_method, *, **)
218
+ raise "#{event_method}_async must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
229
219
 
230
220
  Async do
231
- send(event_method, *args, **kwargs)
221
+ send(event_method, *, **)
232
222
  end
233
223
  end
234
224
 
235
225
  # Handle individual event async bang methods (returns task, raises on failure)
236
- def handle_individual_event_async_bang(event_method, *args, **kwargs)
226
+ def handle_individual_event_async_bang(event_method, *, **)
237
227
  # Extract event name from method and use bang version
238
- bang_method = "#{event_method}!".to_sym
228
+ bang_method = :"#{event_method}!"
239
229
 
240
- unless defined?(::Async::Task) && ::Async::Task.current?
241
- raise RuntimeError, "#{event_method}_async! must be called within an Async context"
242
- end
230
+ raise "#{event_method}_async! must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
243
231
 
244
232
  Async do
245
- send(bang_method, *args, **kwargs)
233
+ send(bang_method, *, **)
246
234
  end
247
235
  end
248
236
 
@@ -251,12 +239,10 @@ module StateMachines
251
239
  method_str = method_name.to_s
252
240
 
253
241
  # Find the machine and event for this method
254
- self.class.state_machines.each do |name, machine|
242
+ self.class.state_machines.each do |_name, machine|
255
243
  machine.events.each do |event|
256
244
  qualified_name = event.qualified_name
257
- if qualified_name.to_s == method_str || "#{qualified_name}!".to_s == method_str
258
- return event.name
259
- end
245
+ return event.name if qualified_name.to_s == method_str || "#{qualified_name}!" == method_str
260
246
  end
261
247
  end
262
248
 
@@ -274,9 +260,7 @@ module StateMachines
274
260
  # results = vehicle.fire_events_concurrent(:ignite, :buy_insurance)
275
261
  # end
276
262
  def fire_events_concurrent(*event_names)
277
- unless defined?(::Async::Task) && ::Async::Task.current?
278
- raise RuntimeError, "fire_events_concurrent must be called within an Async context"
279
- end
263
+ raise 'fire_events_concurrent must be called within an Async context' unless defined?(::Async::Task) && ::Async::Task.current?
280
264
 
281
265
  tasks = async_fire_events(*event_names)
282
266
  tasks.map(&:wait)
@@ -24,13 +24,11 @@ module StateMachines
24
24
 
25
25
  # Fires an event asynchronously on the given object
26
26
  # Returns an Async::Task for concurrent execution
27
- def async_fire_event(object, event_name, *args)
28
- unless defined?(::Async::Task) && ::Async::Task.current?
29
- raise RuntimeError, "async_fire_event must be called within an Async context"
30
- end
27
+ def async_fire_event(object, event_name, *)
28
+ raise 'async_fire_event must be called within an Async context' unless defined?(::Async::Task) && ::Async::Task.current?
31
29
 
32
30
  Async do
33
- events[event_name].fire(object, *args)
31
+ events[event_name].fire(object, *)
34
32
  end
35
33
  end
36
34