state_machines 0.301.2 → 0.303.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.
@@ -53,9 +53,9 @@ module StateMachines
53
53
  end
54
54
 
55
55
  actual = object.send(name_method)
56
- default_message = "Expected #{object.class}##{machine_name} to be #{expected_state}, but was #{actual}"
56
+ default_message = "Expected #{object.class}##{machine_name} to be #{expected_state.inspect}, but was #{actual.inspect}"
57
57
 
58
- _sm_assert_equal(expected_state.to_s, actual.to_s, message || default_message)
58
+ _sm_assert(_sm_same_state?(expected_state, actual), message || default_message)
59
59
  end
60
60
 
61
61
  # Assert that an object can transition via a specific event
@@ -119,70 +119,94 @@ module StateMachines
119
119
 
120
120
  # === Extended State Machine Assertions ===
121
121
 
122
+ # Assert the machine's states, in any order. Names resolve through the
123
+ # machine ('parked' is :parked). The nil state is only listed when expected,
124
+ # since a machine without an initial state has one implicitly.
122
125
  def assert_sm_states_list(machine, expected_states, message = nil)
123
- actual_states = machine.states.map(&:name).compact
124
- default_message = "Expected states #{expected_states} but got #{actual_states}"
126
+ expected = expected_states.map { |state| machine.states[state]&.name || state }
127
+ actual = machine.states.map(&:name)
128
+ actual.compact! unless expected_states.include?(nil)
129
+ default_message = "Expected states #{expected.inspect} but got #{actual.inspect}"
125
130
 
126
- _sm_assert_equal(expected_states.sort, actual_states.sort, message || default_message)
131
+ _sm_assert_equal(expected.sort_by(&:inspect), actual.sort_by(&:inspect), message || default_message)
127
132
  end
128
133
 
129
134
  def refute_sm_state_defined(machine, state, message = nil)
130
- state_exists = machine.states.any? { |s| s.name == state }
131
- default_message = "Expected state #{state} to not be defined in machine"
135
+ default_message = "Expected state #{state.inspect} to not be defined in machine"
132
136
 
133
- _sm_refute(state_exists, message || default_message)
137
+ _sm_refute(machine.states[state], message || default_message)
134
138
  end
135
139
  alias assert_sm_state_not_defined refute_sm_state_defined
136
140
 
141
+ # @raise [IndexError] if +expected_state+ is not a known state
137
142
  def assert_sm_initial_state(machine, expected_state, message = nil)
138
- state_obj = machine.state(expected_state)
139
- is_initial = state_obj&.initial?
140
- default_message = "Expected state #{expected_state} to be the initial state"
143
+ state = machine.states.fetch(expected_state)
144
+ default_message = "Expected state #{state.name.inspect} to be the initial state"
141
145
 
142
- _sm_assert(is_initial, message || default_message)
146
+ _sm_assert(state.initial?, message || default_message)
143
147
  end
144
148
 
149
+ # @raise [IndexError] if +state+ is not a known state
145
150
  def assert_sm_final_state(machine, state, message = nil)
146
- state_obj = machine.states[state]
147
- is_final = state_obj&.final?
148
- default_message = "Expected state #{state} to be final"
151
+ state = machine.states.fetch(state)
152
+ default_message = "Expected state #{state.name.inspect} to be final"
149
153
 
150
- _sm_assert(is_final, message || default_message)
154
+ _sm_assert(state.final?, message || default_message)
151
155
  end
152
156
 
157
+ # Assert the set of states the machine's events can lead to from a state,
158
+ # ignoring guards. Each branch contributes the target it would pick for
159
+ # +from+ (a loopback contributes +from+ itself); order does not matter.
160
+ #
161
+ # @param machine [StateMachines::Machine] The state machine
162
+ # @param from [Symbol, String, nil] The source state
163
+ # @param expected_to_states [Array] The expected target states
164
+ # @param message [String, nil] Custom failure message
165
+ # @raise [IndexError] if +from+ or an expected state is not a known state
153
166
  def assert_sm_possible_transitions(machine, from:, expected_to_states:, message: nil)
154
- actual_transitions = machine.events.flat_map do |event|
155
- event.branches.select { |branch| branch.known_states.include?(from) }
156
- .map(&:to)
167
+ from = machine.states.fetch(from).name
168
+ names = machine.states.map(&:name)
169
+ expected = expected_to_states.map { |state| machine.states.fetch(state).name }.uniq
170
+ actual = machine.events.flat_map do |event|
171
+ event.branches.flat_map { |branch| _sm_branch_targets(branch, from, names) }
157
172
  end.uniq
158
- default_message = "Expected transitions from #{from} to #{expected_to_states} but got #{actual_transitions}"
173
+ by_definition = ->(name) { names.index(name) }
174
+ default_message = "Expected transitions from #{from.inspect} to #{expected.inspect} but got #{actual.inspect}"
159
175
 
160
- _sm_assert_equal(expected_to_states.sort, actual_transitions.sort, message || default_message)
176
+ _sm_assert_equal(expected.sort_by(&by_definition), actual.sort_by(&by_definition), message || default_message)
161
177
  end
162
178
 
179
+ # Refute that an event has a branch from +from+ to +to+, ignoring guards.
180
+ #
181
+ # @raise [IndexError] if +on+, +from+ or +to+ is unknown
163
182
  def refute_sm_transition_allowed(machine, from:, to:, on:, message: nil)
164
- event = machine.events[on]
165
- is_allowed = event&.branches&.any? { |branch| branch.known_states.include?(from) && branch.to == to }
166
- default_message = "Expected transition from #{from} to #{to} on #{on} to not be allowed"
183
+ event = machine.events.fetch(on)
184
+ from = machine.states.fetch(from).name
185
+ to = machine.states.fetch(to).name
186
+ names = machine.states.map(&:name)
187
+ is_allowed = event.branches.any? { |branch| _sm_branch_targets(branch, from, names) == [to] }
188
+ default_message = "Expected transition from #{from.inspect} to #{to.inspect} on #{event.name.inspect} to not be allowed"
167
189
 
168
190
  _sm_refute(is_allowed, message || default_message)
169
191
  end
170
192
  alias assert_sm_transition_not_allowed refute_sm_transition_allowed
171
193
 
172
194
  def assert_sm_event_triggers(object, event, machine_name = :state, message = nil)
173
- initial_state = object.send(machine_name)
195
+ machine = object.class.state_machine(machine_name)
196
+ initial_state = machine.read(object, :state)
174
197
  object.send("#{event}!")
175
- state_changed = initial_state != object.send(machine_name)
198
+ state_changed = initial_state != machine.read(object, :state)
176
199
  default_message = "Expected event #{event} to trigger state change on #{machine_name}"
177
200
 
178
201
  _sm_assert(state_changed, message || default_message)
179
202
  end
180
203
 
181
204
  def refute_sm_event_triggers(object, event, machine_name = :state, message = nil)
182
- initial_state = object.send(machine_name)
205
+ machine = object.class.state_machine(machine_name)
206
+ initial_state = machine.read(object, :state)
183
207
  begin
184
208
  object.send("#{event}!")
185
- state_unchanged = initial_state == object.send(machine_name)
209
+ state_unchanged = initial_state == machine.read(object, :state)
186
210
  default_message = "Expected event #{event} to not trigger state change on #{machine_name}"
187
211
 
188
212
  _sm_assert(state_unchanged, message || default_message)
@@ -271,15 +295,14 @@ module StateMachines
271
295
  # # Specific machine
272
296
  # assert_sm_triggers_event(vehicle, :disable, machine_name: :alarm) { vehicle.turn_off_alarm }
273
297
  def assert_sm_triggers_event(object, expected_events, machine_name: :state, message: nil)
274
- expected_events = Array(expected_events)
275
298
  triggered_events = []
276
299
 
277
300
  # Get the state machine
278
301
  machine = object.class.state_machines[machine_name]
279
302
  raise ArgumentError, "No state machine found for #{machine_name}" unless machine
280
303
 
281
- # Save original callbacks to restore later
282
- machine.callbacks[:before].dup
304
+ # Resolve the names ('crash' is :crash); an unknown event raises IndexError
305
+ expected_events = Array(expected_events).map { |event| machine.events.fetch(event).name }
283
306
 
284
307
  # Add a temporary callback to track triggered events
285
308
  temp_callback = machine.before_transition do |_obj, transition|
@@ -346,72 +369,9 @@ module StateMachines
346
369
  _assert_transition_callback(:after, machine_or_class, options, message)
347
370
  end
348
371
 
349
- # === Sync Mode Assertions ===
372
+ # === Execution Assertions ===
350
373
 
351
- # Assert that a state machine is operating in synchronous mode
352
- #
353
- # @param object [Object] The object with state machines
354
- # @param machine_name [Symbol] The name of the state machine (defaults to :state)
355
- # @param message [String, nil] Custom failure message
356
- # @return [void]
357
- # @raise [AssertionError] If the machine has async mode enabled
358
- #
359
- # @example
360
- # user = User.new
361
- # assert_sm_sync_mode(user) # Uses default :state machine
362
- # assert_sm_sync_mode(user, :status) # Uses :status machine
363
- def assert_sm_sync_mode(object, machine_name = :state, message = nil)
364
- machine = object.class.state_machines[machine_name]
365
- raise ArgumentError, "No state machine '#{machine_name}' found" unless machine
366
-
367
- async_enabled = machine.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
368
- default_message = "Expected state machine '#{machine_name}' to be in sync mode, but async mode is enabled"
369
-
370
- _sm_refute(async_enabled, message || default_message)
371
- end
372
-
373
- # Assert that async methods are not available on a sync-only object
374
- #
375
- # @param object [Object] The object with state machines
376
- # @param message [String, nil] Custom failure message
377
- # @return [void]
378
- # @raise [AssertionError] If async methods are available
379
- #
380
- # @example
381
- # sync_only_car = Car.new # Car has no async: true machines
382
- # assert_sm_no_async_methods(sync_only_car)
383
- def assert_sm_no_async_methods(object, message = nil)
384
- async_methods = %i[fire_event_async fire_events_async fire_event_async! async_fire_event]
385
- available_async_methods = async_methods.select { |method| object.respond_to?(method) }
386
-
387
- default_message = "Expected no async methods to be available, but found: #{available_async_methods.inspect}"
388
-
389
- _sm_assert_empty(available_async_methods, message || default_message)
390
- end
391
-
392
- # Assert that an object has no async-enabled state machines
393
- #
394
- # @param object [Object] The object with state machines
395
- # @param message [String, nil] Custom failure message
396
- # @return [void]
397
- # @raise [AssertionError] If any machine has async mode enabled
398
- #
399
- # @example
400
- # sync_only_vehicle = Vehicle.new # All machines are sync-only
401
- # assert_sm_all_sync(sync_only_vehicle)
402
- def assert_sm_all_sync(object, message = nil)
403
- async_machines = []
404
-
405
- object.class.state_machines.each do |name, machine|
406
- async_machines << name if machine.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
407
- end
408
-
409
- default_message = "Expected all state machines to be sync-only, but these have async enabled: #{async_machines.inspect}"
410
-
411
- _sm_assert_empty(async_machines, message || default_message)
412
- end
413
-
414
- # Assert that synchronous event execution works correctly
374
+ # Assert that firing a bang event transitions to the expected state
415
375
  #
416
376
  # @param object [Object] The object with state machines
417
377
  # @param event [Symbol] The event to trigger
@@ -432,12 +392,12 @@ module StateMachines
432
392
  # Execute event synchronously
433
393
  result = object.send("#{event}!")
434
394
 
435
- # Verify immediate state change (no async delay)
395
+ # Verify the state changed by the time the event returned
436
396
  final_state = object.send(machine_name)
437
397
 
438
398
  # Check that transition succeeded
439
399
  state_changed = initial_state != final_state
440
- correct_final_state = final_state.to_s == expected_state.to_s
400
+ correct_final_state = _sm_same_state?(expected_state, final_state)
441
401
 
442
402
  default_message = "Expected sync execution of '#{event}' to change #{machine_name} from '#{initial_state}' to '#{expected_state}', but got '#{final_state}'"
443
403
 
@@ -447,16 +407,16 @@ module StateMachines
447
407
  end
448
408
 
449
409
  # Assert that event execution is immediate: the state has changed when the
450
- # bang event returns, and it returned no async task to wait on. Decided by
451
- # what the call did, not by how long it took, so a slow runner (a cold
452
- # JIT) cannot fail it.
410
+ # bang event returns, and it returned the performed transition rather than
411
+ # something to wait on. Decided by what the call did, not by how long it
412
+ # took, so a slow runner (a cold JIT) cannot fail it.
453
413
  #
454
414
  # @param object [Object] The object with state machines
455
415
  # @param event [Symbol] The event to trigger
456
416
  # @param machine_name [Symbol] The name of the state machine (defaults to :state)
457
417
  # @param message [String, nil] Custom failure message
458
418
  # @return [void]
459
- # @raise [AssertionError] If the event did not complete synchronously
419
+ # @raise [AssertionError] If the event did not complete before returning
460
420
  #
461
421
  # @example
462
422
  # car = Car.new
@@ -466,129 +426,10 @@ module StateMachines
466
426
  result = object.send("#{event}!")
467
427
  final_state = object.send(machine_name)
468
428
 
469
- deferred = defined?(::Async::Task) && result.is_a?(::Async::Task)
470
- default_message = "Expected immediate sync execution of '#{event}', but it returned #{result.class} (async)"
429
+ default_message = "Expected immediate execution of '#{event}' to return the performed transition, but it returned #{result.class}"
471
430
 
472
431
  _sm_assert(initial_state != final_state, message || "Event #{event} should change #{machine_name} before returning")
473
- _sm_assert(!deferred, message || default_message)
474
- end
475
-
476
- # === Async Mode Assertions ===
477
-
478
- # Assert that a state machine is operating in asynchronous mode
479
- #
480
- # @param object [Object] The object with state machines
481
- # @param machine_name [Symbol] The name of the state machine (defaults to :state)
482
- # @param message [String, nil] Custom failure message
483
- # @return [void]
484
- # @raise [AssertionError] If the machine doesn't have async mode enabled
485
- #
486
- # @example
487
- # drone = AutonomousDrone.new
488
- # assert_sm_async_mode(drone) # Uses default :state machine
489
- # assert_sm_async_mode(drone, :teleporter_status) # Uses :teleporter_status machine
490
- def assert_sm_async_mode(object, machine_name = :state, message = nil)
491
- machine = object.class.state_machines[machine_name]
492
- raise ArgumentError, "No state machine '#{machine_name}' found" unless machine
493
-
494
- async_enabled = machine.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
495
- default_message = "Expected state machine '#{machine_name}' to have async mode enabled, but it's in sync mode"
496
-
497
- _sm_assert(async_enabled, message || default_message)
498
- end
499
-
500
- # Assert that async methods are available on an async-enabled object
501
- #
502
- # @param object [Object] The object with state machines
503
- # @param message [String, nil] Custom failure message
504
- # @return [void]
505
- # @raise [AssertionError] If async methods are not available
506
- #
507
- # @example
508
- # drone = AutonomousDrone.new # Has async: true machines
509
- # assert_sm_async_methods(drone)
510
- def assert_sm_async_methods(object, message = nil)
511
- async_methods = %i[fire_event_async fire_events_async fire_event_async! async_fire_event]
512
- available_async_methods = async_methods.select { |method| object.respond_to?(method) }
513
-
514
- default_message = 'Expected async methods to be available, but found none'
515
-
516
- _sm_refute_empty(available_async_methods, message || default_message)
517
- end
518
-
519
- # Assert that an object has async-enabled state machines
520
- #
521
- # @param object [Object] The object with state machines
522
- # @param machine_names [Array<Symbol>] Expected async machine names
523
- # @param message [String, nil] Custom failure message
524
- # @return [void]
525
- # @raise [AssertionError] If expected machines don't have async mode
526
- #
527
- # @example
528
- # drone = AutonomousDrone.new
529
- # assert_sm_has_async(drone, [:status, :teleporter_status, :shields])
530
- def assert_sm_has_async(object, machine_names = nil, message = nil)
531
- if machine_names
532
- # Check specific machines
533
- non_async_machines = machine_names.reject do |name|
534
- machine = object.class.state_machines[name]
535
- machine&.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
536
- end
537
-
538
- default_message = "Expected machines #{machine_names.inspect} to have async enabled, but these don't: #{non_async_machines.inspect}"
539
-
540
- _sm_assert_empty(non_async_machines, message || default_message)
541
- else
542
- # Check that at least one machine has async
543
- async_machines = object.class.state_machines.select do |_name, machine|
544
- machine.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
545
- end
546
-
547
- default_message = 'Expected at least one state machine to have async enabled, but none found'
548
-
549
- _sm_refute_empty(async_machines, message || default_message)
550
- end
551
- end
552
-
553
- # Assert that individual async event methods are available
554
- #
555
- # @param object [Object] The object with state machines
556
- # @param event [Symbol] The event name
557
- # @param message [String, nil] Custom failure message
558
- # @return [void]
559
- # @raise [AssertionError] If async event methods are not available
560
- #
561
- # @example
562
- # drone = AutonomousDrone.new
563
- # assert_sm_async_event_methods(drone, :launch) # Checks launch_async and launch_async!
564
- def assert_sm_async_event_methods(object, event, message = nil)
565
- async_method = :"#{event}_async"
566
- async_bang_method = :"#{event}_async!"
567
-
568
- has_async = object.respond_to?(async_method)
569
- has_async_bang = object.respond_to?(async_bang_method)
570
-
571
- _sm_assert(has_async, message || "Missing #{async_method} method")
572
- _sm_assert(has_async_bang, message || "Missing #{async_bang_method} method")
573
- end
574
-
575
- # Assert that an object has thread-safe state methods when async is enabled
576
- #
577
- # @param object [Object] The object with state machines
578
- # @param message [String, nil] Custom failure message
579
- # @return [void]
580
- # @raise [AssertionError] If thread-safe methods are not available
581
- #
582
- # @example
583
- # drone = AutonomousDrone.new
584
- # assert_sm_thread_safe_methods(drone)
585
- def assert_sm_thread_safe_methods(object, message = nil)
586
- thread_safe_methods = %i[state_machine_mutex read_state_safely write_state_safely]
587
- missing_methods = thread_safe_methods.reject { |method| object.respond_to?(method) }
588
-
589
- default_message = "Expected thread-safe methods to be available, but missing: #{missing_methods.inspect}"
590
-
591
- _sm_assert_empty(missing_methods, message || default_message)
432
+ _sm_assert(result.is_a?(StateMachines::Transition), message || default_message)
592
433
  end
593
434
 
594
435
  # === Refusal Reason Assertions ===
@@ -762,6 +603,13 @@ module StateMachines
762
603
 
763
604
  private
764
605
 
606
+ # Compares a state name or value with an expected state, treating a
607
+ # String and its Symbol as equal. nil only equals nil: comparing by to_s
608
+ # alone would make the nil state and a state named :"" indistinguishable.
609
+ def _sm_same_state?(expected, actual)
610
+ expected.nil? || actual.nil? ? expected.nil? && actual.nil? : expected.to_s == actual.to_s
611
+ end
612
+
765
613
  # Finds the machine and its event, accepting the plain or the
766
614
  # namespace-qualified event name, and raising when either is missing
767
615
  def _sm_find_event(object, event, machine_name)
@@ -880,6 +728,40 @@ module StateMachines
880
728
  assert false, message
881
729
  end
882
730
 
731
+ # The target a branch would pick from +from+ (as Event#transition_for
732
+ # does, ignoring guards), as [] or [target]. first(1), not first: a
733
+ # transition into the nil state must not read as none.
734
+ def _sm_branch_targets(branch, from, names)
735
+ requirement = branch.state_requirements.detect { |candidate| candidate[:from].matches?(from) }
736
+ return [] unless requirement
737
+
738
+ requirement[:to].is_a?(LoopbackMatcher) ? [from] : requirement[:to].filter([from] | names).first(1)
739
+ end
740
+
741
+ # Whether a callback's from/to requirement admits the expected states. A
742
+ # loopback (same) lands where it left, so without an expected source the
743
+ # source is the target itself.
744
+ def _sm_state_requirement_matches?(requirement, states)
745
+ loopback = requirement[:to].is_a?(LoopbackMatcher)
746
+ from = states.key?(:from) ? states[:from] : states[:to]
747
+ return false if (states.key?(:from) || loopback) && !requirement[:from].matches?(from)
748
+
749
+ !states.key?(:to) || requirement[:to].matches?(states[:to], from: from)
750
+ end
751
+
752
+ # Whether every expected callable (one or an Array) is among the defined
753
+ # ones. Method names compare as String/Symbol alike; anything else, such as
754
+ # a lambda, only matches itself.
755
+ def _sm_includes_callables?(defined, expected)
756
+ defined = Array(defined)
757
+ name = ->(value) { value.is_a?(Symbol) || value.is_a?(String) }
758
+ Array(expected).all? do |wanted|
759
+ defined.any? do |candidate|
760
+ name.call(candidate) && name.call(wanted) ? candidate.to_s == wanted.to_s : candidate.equal?(wanted)
761
+ end
762
+ end
763
+ end
764
+
883
765
  # Internal helper for checking transition callbacks
884
766
  def _assert_transition_callback(callback_type, machine_or_class, options, message)
885
767
  # Get the machine
@@ -888,72 +770,30 @@ module StateMachines
888
770
 
889
771
  callbacks = machine.callbacks[callback_type] || []
890
772
 
891
- # Extract expected conditions
892
- expected_event = options[:on]
893
- expected_from = options[:from]
894
- expected_to = options[:to]
773
+ # Extract expected conditions. States and events resolve to their
774
+ # canonical names first, since the branch matchers compare those: a raw
775
+ # 'parked' would never equal :parked, and would pass any all - :parked.
776
+ # An unknown name raises IndexError.
777
+ expected_event = machine.events.fetch(options[:on]).name if options.key?(:on)
778
+ states = options.slice(:from, :to).transform_values { |state| machine.states.fetch(state).name }
779
+ expected_from = states[:from]
780
+ expected_to = states[:to]
895
781
  expected_method = options[:do]
896
782
  expected_if = options[:if]
897
783
  expected_unless = options[:unless]
898
784
 
899
- # Find matching callback
785
+ # Find matching callback. Requirements are checked with the matchers'
786
+ # own matches?, so all / all - [...] / same behave as they do at runtime,
787
+ # and an expected nil state is an expectation, not an omission.
900
788
  matching_callback = callbacks.find do |callback|
901
789
  branch = callback.branch
902
790
 
903
- # Check event requirement
904
- if expected_event
905
- event_requirement = branch.event_requirement
906
- event_matches = if event_requirement && event_requirement.respond_to?(:values)
907
- event_requirement.values.include?(expected_event)
908
- else
909
- false
910
- end
911
- next false unless event_matches
912
- end
913
-
914
- # Check state requirements (from/to)
915
- if expected_from || expected_to
916
- state_matches = false
917
- branch.state_requirements.each do |req|
918
- from_matches = !expected_from || (req[:from] && req[:from].respond_to?(:values) && req[:from].values.include?(expected_from))
919
- to_matches = !expected_to || (req[:to] && req[:to].respond_to?(:values) && req[:to].values.include?(expected_to))
920
-
921
- if from_matches && to_matches
922
- state_matches = true
923
- break
924
- end
925
- end
926
- next false unless state_matches
927
- end
928
-
929
- # Check method requirement
930
- if expected_method
931
- methods = callback.instance_variable_get(:@methods) || []
932
- method_matches = methods.any? do |method|
933
- (method.is_a?(Symbol) && method == expected_method) ||
934
- (method.is_a?(String) && method.to_sym == expected_method) ||
935
- (method.respond_to?(:call) && method.respond_to?(:source_location))
936
- end
937
- next false unless method_matches
938
- end
939
-
940
- # Check if condition
941
- if expected_if
942
- if_condition = branch.if_condition
943
- if_matches = (if_condition.is_a?(Symbol) && if_condition == expected_if) ||
944
- (if_condition.is_a?(String) && if_condition.to_sym == expected_if) ||
945
- if_condition.respond_to?(:call)
946
- next false unless if_matches
947
- end
948
-
949
- # Check unless condition
950
- if expected_unless
951
- unless_condition = branch.unless_condition
952
- unless_matches = (unless_condition.is_a?(Symbol) && unless_condition == expected_unless) ||
953
- (unless_condition.is_a?(String) && unless_condition.to_sym == expected_unless) ||
954
- unless_condition.respond_to?(:call)
955
- next false unless unless_matches
956
- end
791
+ next false if options.key?(:on) && !branch.event_requirement.matches?(expected_event)
792
+ next false if states.any? &&
793
+ branch.state_requirements.none? { |requirement| _sm_state_requirement_matches?(requirement, states) }
794
+ next false if options.key?(:do) && !_sm_includes_callables?(callback.callables, expected_method)
795
+ next false if options.key?(:if) && !_sm_includes_callables?(branch.if_condition, expected_if)
796
+ next false if options.key?(:unless) && !_sm_includes_callables?(branch.unless_condition, expected_unless)
957
797
 
958
798
  true
959
799
  end
@@ -961,12 +801,12 @@ module StateMachines
961
801
  return if matching_callback
962
802
 
963
803
  expected_parts = []
964
- expected_parts << "on: #{expected_event.inspect}" if expected_event
965
- expected_parts << "from: #{expected_from.inspect}" if expected_from
966
- expected_parts << "to: #{expected_to.inspect}" if expected_to
967
- expected_parts << "do: #{expected_method.inspect}" if expected_method
968
- expected_parts << "if: #{expected_if.inspect}" if expected_if
969
- expected_parts << "unless: #{expected_unless.inspect}" if expected_unless
804
+ expected_parts << "on: #{expected_event.inspect}" if options.key?(:on)
805
+ expected_parts << "from: #{expected_from.inspect}" if options.key?(:from)
806
+ expected_parts << "to: #{expected_to.inspect}" if options.key?(:to)
807
+ expected_parts << "do: #{expected_method.inspect}" if options.key?(:do)
808
+ expected_parts << "if: #{expected_if.inspect}" if options.key?(:if)
809
+ expected_parts << "unless: #{expected_unless.inspect}" if options.key?(:unless)
970
810
 
971
811
  default_message = "Expected #{callback_type}_transition callback with #{expected_parts.join(', ')} to be defined, but it was not found"
972
812
 
@@ -26,9 +26,10 @@ module StateMachines
26
26
  LOCK = Thread::Mutex.new
27
27
 
28
28
  class << self
29
- # Claims an attribute for every given transition whose machine runs in
30
- # async mode, ordered by attribute so that collections over the same
31
- # attributes claim them in the same order.
29
+ # Claims an attribute for every given transition whose machine claims
30
+ # transitions (Machine#claims_transitions?), ordered by attribute so that
31
+ # collections over the same attributes claim them in the same order. A
32
+ # collection without such a transition is granted an empty set.
32
33
  #
33
34
  # Returns the granted claims, or nil when an attribute is held by another
34
35
  # active owner or has left the transition's source state. Every exit
@@ -41,7 +42,7 @@ module StateMachines
41
42
  # attribute could not be claimed, when the claim is refused
42
43
  # @return [Array<StateMachines::TransitionClaim>, nil]
43
44
  def claim(transitions)
44
- claimable = transitions.select { |transition| transition.machine.async_mode_enabled? }
45
+ claimable = transitions.select { |transition| transition.machine.claims_transitions? }
45
46
  granted = []
46
47
  admitted = false
47
48
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StateMachines
4
- VERSION = '0.301.2'
4
+ VERSION = '0.303.0'
5
5
  end
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.301.2
4
+ version: 0.303.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -62,12 +62,6 @@ files:
62
62
  - LICENSE.txt
63
63
  - README.md
64
64
  - lib/state_machines.rb
65
- - lib/state_machines/async_mode.rb
66
- - lib/state_machines/async_mode/async_event_extensions.rb
67
- - lib/state_machines/async_mode/async_events.rb
68
- - lib/state_machines/async_mode/async_machine.rb
69
- - lib/state_machines/async_mode/async_transition_collection.rb
70
- - lib/state_machines/async_mode/thread_safe_state.rb
71
65
  - lib/state_machines/branch.rb
72
66
  - lib/state_machines/callback.rb
73
67
  - lib/state_machines/core.rb
@@ -83,7 +77,6 @@ files:
83
77
  - lib/state_machines/integrations/base.rb
84
78
  - lib/state_machines/machine.rb
85
79
  - lib/state_machines/machine/action_hooks.rb
86
- - lib/state_machines/machine/async_extensions.rb
87
80
  - lib/state_machines/machine/callbacks.rb
88
81
  - lib/state_machines/machine/class_methods.rb
89
82
  - lib/state_machines/machine/configuration.rb
@@ -95,7 +88,6 @@ files:
95
88
  - lib/state_machines/machine/scoping.rb
96
89
  - lib/state_machines/machine/state_methods.rb
97
90
  - lib/state_machines/machine/utilities.rb
98
- - lib/state_machines/machine/validation.rb
99
91
  - lib/state_machines/machine_collection.rb
100
92
  - lib/state_machines/macro_methods.rb
101
93
  - lib/state_machines/matcher.rb
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module StateMachines
4
- module AsyncMode
5
- # Extensions to Event class for async bang methods
6
- module AsyncEventExtensions
7
- # Generate async bang methods for events when async mode is enabled
8
- def define_helper(scope, method, *args, &)
9
- result = super
10
-
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?
13
- qualified_name = method.to_s
14
-
15
- # Create async version that returns a task
16
- machine.define_helper(scope, "#{qualified_name}_async") do |_machine, object, *method_args, **kwargs|
17
- # Find the machine that has this event
18
- target_machine = object.class.state_machines.values.find { |m| m.events[name] }
19
-
20
- raise "#{qualified_name}_async must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
21
-
22
- Async do
23
- target_machine.events[name].fire(object, *method_args, **kwargs)
24
- end
25
- end
26
-
27
- # Create async bang version that raises exceptions when awaited
28
- machine.define_helper(scope, "#{qualified_name}_async!") do |_machine, object, *method_args, **kwargs|
29
- # Find the machine that has this event
30
- target_machine = object.class.state_machines.values.find { |m| m.events[name] }
31
-
32
- raise "#{qualified_name}_async! must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
33
-
34
- Async do
35
- # Use fire method which will raise exceptions on invalid transitions
36
- target_machine.events[name].fire(object, *method_args, **kwargs) || raise(StateMachines::InvalidTransition.new(object, target_machine, name))
37
- end
38
- end
39
- end
40
-
41
- result
42
- end
43
- end
44
- end
45
- end