state_machines 0.302.0 → 0.304.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 +61 -1
- data/lib/state_machines/branch.rb +21 -0
- data/lib/state_machines/callback.rb +26 -6
- data/lib/state_machines/error.rb +13 -2
- data/lib/state_machines/eval_helpers.rb +31 -18
- data/lib/state_machines/event.rb +60 -2
- data/lib/state_machines/machine/action_hooks.rb +13 -10
- data/lib/state_machines/machine/callbacks.rb +15 -0
- data/lib/state_machines/machine/configuration.rb +3 -0
- data/lib/state_machines/machine/helper_generators.rb +26 -13
- data/lib/state_machines/machine/state_methods.rb +4 -3
- data/lib/state_machines/machine/utilities.rb +24 -0
- data/lib/state_machines/machine.rb +0 -2
- data/lib/state_machines/node_collection.rb +26 -23
- data/lib/state_machines/state.rb +66 -31
- data/lib/state_machines/state_collection.rb +10 -2
- data/lib/state_machines/state_context.rb +6 -1
- data/lib/state_machines/test_helper.rb +132 -101
- data/lib/state_machines/transition.rb +108 -232
- data/lib/state_machines/transition_collection.rb +6 -14
- data/lib/state_machines/version.rb +1 -1
- metadata +1 -2
- data/lib/state_machines/machine/validation.rb +0 -39
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module StateMachines
|
|
4
|
-
# Module to extend Fiber instances for pausable state tracking
|
|
5
|
-
module PausableFiber
|
|
6
|
-
attr_accessor :state_machine_fiber_pausable
|
|
7
|
-
end
|
|
8
|
-
|
|
9
4
|
# A transition represents a state change for a specific attribute.
|
|
10
5
|
#
|
|
11
6
|
# Transitions consist of:
|
|
@@ -43,16 +38,17 @@ module StateMachines
|
|
|
43
38
|
# @see StateMachines::Event#refusal for the meaning of each reason
|
|
44
39
|
attr_accessor :refusal
|
|
45
40
|
|
|
41
|
+
# The event branch selected for this transition, whose before: / after: /
|
|
42
|
+
# around: callbacks it runs; nil for a transition not built by an event
|
|
43
|
+
attr_reader :branch
|
|
44
|
+
|
|
46
45
|
# Creates a new, specific transition
|
|
47
|
-
def initialize(object, machine, event, from_name, to_name, read_state = true) # :nodoc:
|
|
46
|
+
def initialize(object, machine, event, from_name, to_name, read_state = true, branch: nil) # :nodoc:
|
|
48
47
|
@object = object
|
|
49
48
|
@machine = machine
|
|
49
|
+
@branch = branch
|
|
50
50
|
@args = []
|
|
51
51
|
@transient = false
|
|
52
|
-
@paused_fiber = nil
|
|
53
|
-
@resuming = false
|
|
54
|
-
@continuation_block = nil
|
|
55
|
-
@fiber_thread_storage = nil
|
|
56
52
|
|
|
57
53
|
@event = machine.events.fetch(event)
|
|
58
54
|
@from_state = machine.states.fetch(from_name)
|
|
@@ -200,35 +196,35 @@ module StateMachines
|
|
|
200
196
|
#
|
|
201
197
|
# Configuration options:
|
|
202
198
|
# * +before+ - Whether to run before callbacks.
|
|
203
|
-
# * +after+ - Whether to run after callbacks. If false
|
|
204
|
-
#
|
|
199
|
+
# * +after+ - Whether to run after callbacks. If false (and +before+ is
|
|
200
|
+
# enabled), the run is a preview (e.g. validating before a save): every
|
|
201
|
+
# before callback runs, the around callbacks are skipped (not entered,
|
|
202
|
+
# their conditions not evaluated), then the block, and no after
|
|
203
|
+
# callbacks (failure callbacks if it halts or the block fails). The
|
|
204
|
+
# next run with +after+ enabled does not repeat those before callbacks;
|
|
205
|
+
# its around callbacks wrap only its action. So after a preview, a
|
|
206
|
+
# before callback declared after an around callback has run outside
|
|
207
|
+
# that around; without a preview, callbacks interleave as declared.
|
|
205
208
|
# Default is true.
|
|
206
209
|
#
|
|
207
210
|
# This will return true if all before callbacks gets executed. After
|
|
208
211
|
# callbacks will not have an effect on the result.
|
|
209
|
-
def run_callbacks(options = {}, &
|
|
212
|
+
def run_callbacks(options = {}, &)
|
|
210
213
|
options = { before: true, after: true }.merge(options)
|
|
214
|
+
return preview(&) if options[:before] && !options[:after]
|
|
211
215
|
|
|
212
|
-
|
|
213
|
-
# this is an idempotent call on an already-paused transition. Just return true.
|
|
214
|
-
return true if @paused_fiber&.alive? && !options[:after]
|
|
215
|
-
|
|
216
|
-
# Always use fibers for compatibility with existing pause/resume functionality
|
|
217
|
-
# The fiber argument can still be used to explicitly control fiber usage
|
|
218
|
-
pausable_options = options.key?(:fiber) ? { fiber: options[:fiber] } : {}
|
|
219
|
-
|
|
220
|
-
# Check if we're resuming from a pause
|
|
221
|
-
if @paused_fiber&.alive? && options[:after]
|
|
222
|
-
# Resume the paused fiber
|
|
223
|
-
# Don't reset @success when resuming - preserve the state from the pause
|
|
224
|
-
# Store the block for later execution
|
|
225
|
-
@continuation_block = block if block_given?
|
|
226
|
-
halted = pausable(pausable_options) { true }
|
|
227
|
-
else
|
|
216
|
+
if options[:before]
|
|
228
217
|
@success = false
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
218
|
+
begin
|
|
219
|
+
halted = !catch(:halt) do
|
|
220
|
+
before(&)
|
|
221
|
+
true
|
|
222
|
+
end
|
|
223
|
+
ensure
|
|
224
|
+
# However it ends, the full run consumed any preview: the
|
|
225
|
+
# transition is now like one never previewed after the same run
|
|
226
|
+
@previewed = nil
|
|
227
|
+
end
|
|
232
228
|
end
|
|
233
229
|
|
|
234
230
|
# After callbacks are only run if:
|
|
@@ -298,13 +294,8 @@ module StateMachines
|
|
|
298
294
|
# the state has already been persisted
|
|
299
295
|
def reset
|
|
300
296
|
@before_run = @persisted = @after_run = false
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
@paused_fiber.kill if RUBY_ENGINE == 'ruby' && @paused_fiber&.alive? && !@paused_fiber.equal?(Fiber.current)
|
|
304
|
-
@paused_fiber = nil
|
|
305
|
-
@resuming = false
|
|
306
|
-
@continuation_block = nil
|
|
307
|
-
@fiber_thread_storage = nil
|
|
297
|
+
@before_callbacks = nil
|
|
298
|
+
@previewed = nil
|
|
308
299
|
end
|
|
309
300
|
|
|
310
301
|
# Determines equality of transitions by testing whether the object, states,
|
|
@@ -328,45 +319,19 @@ module StateMachines
|
|
|
328
319
|
"#<#{self.class} #{%w[attribute event from from_name to to_name].map { |attr| "#{attr}=#{send(attr).inspect}" } * ' '}>"
|
|
329
320
|
end
|
|
330
321
|
|
|
331
|
-
#
|
|
332
|
-
#
|
|
333
|
-
def
|
|
334
|
-
|
|
335
|
-
end
|
|
336
|
-
|
|
337
|
-
# Checks whether this transition has a paused fiber that can be resumed.
|
|
338
|
-
# Returns true if there is a paused fiber, false otherwise.
|
|
339
|
-
#
|
|
340
|
-
# Note: The actual resuming happens automatically when run_callbacks is called
|
|
341
|
-
# again on a transition with a paused fiber.
|
|
342
|
-
def resumable?
|
|
343
|
-
paused?
|
|
344
|
-
end
|
|
345
|
-
|
|
346
|
-
# Manually resumes the execution of a previously paused callback.
|
|
347
|
-
# Returns true if the transition was successfully resumed and completed,
|
|
348
|
-
# false if there was no paused fiber, and raises an exception if the
|
|
349
|
-
# transition was halted.
|
|
350
|
-
def resume!(&block)
|
|
351
|
-
return false unless paused?
|
|
352
|
-
|
|
353
|
-
# Store continuation block if provided
|
|
354
|
-
@continuation_block = block if block_given?
|
|
355
|
-
|
|
356
|
-
# Run the pausable block which will resume the fiber
|
|
357
|
-
halted = pausable { true }
|
|
358
|
-
|
|
359
|
-
# Return whether the transition completed successfully
|
|
360
|
-
!halted
|
|
322
|
+
# Whether a preview run (run_callbacks with after: false) has run part of
|
|
323
|
+
# this transition's before chain, which the next full run continues
|
|
324
|
+
def previewed?
|
|
325
|
+
!@previewed.nil?
|
|
361
326
|
end
|
|
362
327
|
|
|
363
328
|
# Completes a transition whose before phase has already run by executing
|
|
364
|
-
# its after callbacks,
|
|
329
|
+
# its after callbacks, running any around callbacks its preview left first. Used
|
|
365
330
|
# to finish transitions that were stored for deferred completion (e.g.
|
|
366
331
|
# generated mid-action from an event attribute) once their action has
|
|
367
332
|
# succeeded.
|
|
368
333
|
def complete_deferred_after_callbacks
|
|
369
|
-
if
|
|
334
|
+
if previewed?
|
|
370
335
|
run_callbacks(after: true)
|
|
371
336
|
else
|
|
372
337
|
after
|
|
@@ -375,141 +340,49 @@ module StateMachines
|
|
|
375
340
|
|
|
376
341
|
private
|
|
377
342
|
|
|
378
|
-
#
|
|
379
|
-
#
|
|
380
|
-
#
|
|
343
|
+
# A preview run: every before callback not run by an earlier preview,
|
|
344
|
+
# skipping the around callbacks (not entered, nor their conditions
|
|
345
|
+
# evaluated: they wrap the action of the full run), then the block; on a
|
|
346
|
+
# halt or a failed block, the failure callbacks. Running all the befores
|
|
347
|
+
# lets a validation in the block see what the full run's befores set, so
|
|
348
|
+
# it agrees with the full run. Returns false if it halted, including a
|
|
349
|
+
# halt from the block.
|
|
381
350
|
#
|
|
382
|
-
#
|
|
383
|
-
#
|
|
384
|
-
#
|
|
385
|
-
#
|
|
386
|
-
#
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
end
|
|
396
|
-
|
|
397
|
-
if @paused_fiber
|
|
398
|
-
# Resume the paused fiber
|
|
399
|
-
@resuming = true
|
|
400
|
-
begin
|
|
401
|
-
result = @paused_fiber.resume
|
|
402
|
-
rescue StandardError => e
|
|
403
|
-
# Clean up on exception
|
|
404
|
-
@resuming = false
|
|
405
|
-
@paused_fiber = nil
|
|
406
|
-
raise e
|
|
351
|
+
# Preview bookkeeping (which befores ran) exists only from a successful
|
|
352
|
+
# preview to the end of its full run: it is built aside and kept only if
|
|
353
|
+
# this preview succeeds, so a preview that halts, fails or raises leaves
|
|
354
|
+
# the transition as it found it, minus any earlier preview.
|
|
355
|
+
def preview # rubocop:disable Naming/PredicateMethod -- returns whether the befores ran, as run_callbacks does
|
|
356
|
+
previewed = @previewed ? @previewed.dup : {}.compare_by_identity
|
|
357
|
+
@previewed = nil
|
|
358
|
+
halted = !catch(:halt) do
|
|
359
|
+
before_callbacks.each do |callback|
|
|
360
|
+
next if callback.type == :around || previewed.key?(callback)
|
|
361
|
+
|
|
362
|
+
previewed[callback] = true
|
|
363
|
+
callback.call(object, context, self)
|
|
407
364
|
end
|
|
408
|
-
@resuming = false
|
|
409
365
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
#
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
# Fiber completed
|
|
418
|
-
@paused_fiber = nil
|
|
419
|
-
result_value == :halted
|
|
420
|
-
end
|
|
421
|
-
else
|
|
422
|
-
# Capture current fiber's Thread.current storage to preserve object identity
|
|
423
|
-
# This is needed for compatibility but has limitations with dynamic assignments
|
|
424
|
-
parent_fiber_locals = capture_thread_locals
|
|
425
|
-
|
|
426
|
-
# Create a new fiber to run the block
|
|
427
|
-
fiber = Fiber.new do
|
|
428
|
-
# Restore parent's Thread.current storage with exact same object references
|
|
429
|
-
parent_fiber_locals.each do |key, value|
|
|
430
|
-
Thread.current[key] = value
|
|
431
|
-
end
|
|
432
|
-
|
|
433
|
-
# Mark that we're inside a pausable fiber
|
|
434
|
-
Fiber.current.extend(StateMachines::PausableFiber)
|
|
435
|
-
Fiber.current.state_machine_fiber_pausable = true
|
|
436
|
-
begin
|
|
437
|
-
halted = !catch(:halt) do
|
|
438
|
-
yield
|
|
439
|
-
true
|
|
440
|
-
end
|
|
441
|
-
|
|
442
|
-
# Export the final thread storage state along with the result
|
|
443
|
-
[halted ? :halted : :completed, capture_thread_locals]
|
|
444
|
-
rescue StandardError => e
|
|
445
|
-
# Store the exception for re-raising
|
|
446
|
-
[:error, e]
|
|
447
|
-
ensure
|
|
448
|
-
# Clean up the flag
|
|
449
|
-
Fiber.current.state_machine_fiber_pausable = false
|
|
450
|
-
end
|
|
451
|
-
end
|
|
452
|
-
|
|
453
|
-
# Run the fiber
|
|
454
|
-
result = fiber.resume
|
|
455
|
-
|
|
456
|
-
result_value = unwrap_fiber_result(result)
|
|
457
|
-
|
|
458
|
-
# Save if paused
|
|
459
|
-
if fiber.alive?
|
|
460
|
-
@paused_fiber = fiber
|
|
461
|
-
# Return true to indicate paused (treated as halted for flow control)
|
|
462
|
-
true
|
|
463
|
-
else
|
|
464
|
-
# Fiber completed, return whether it was halted
|
|
465
|
-
result_value == :halted
|
|
466
|
-
end
|
|
467
|
-
end
|
|
468
|
-
end
|
|
469
|
-
|
|
470
|
-
# Snapshots the current fiber's Thread.current storage, preserving object
|
|
471
|
-
# identity of the stored values
|
|
472
|
-
def capture_thread_locals
|
|
473
|
-
Thread.current.keys.each_with_object({}) do |key, storage|
|
|
474
|
-
storage[key] = Thread.current[key]
|
|
366
|
+
# Inside the catch, as in a full run: the block (e.g. the collection
|
|
367
|
+
# running the next machine's transition) may halt, which fails this
|
|
368
|
+
# transition too
|
|
369
|
+
action = block_given? ? { success: true }.merge(yield) : { success: true }
|
|
370
|
+
@result = action[:result]
|
|
371
|
+
@success = action[:success]
|
|
372
|
+
true
|
|
475
373
|
end
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
# Unwraps a result returned from a pausable fiber, re-raising any exception
|
|
479
|
-
# captured inside the fiber and importing its exported thread storage
|
|
480
|
-
def unwrap_fiber_result(result)
|
|
481
|
-
return result unless result.is_a?(Array)
|
|
374
|
+
@success = false if halted
|
|
482
375
|
|
|
483
|
-
if
|
|
484
|
-
@
|
|
485
|
-
|
|
376
|
+
if @success
|
|
377
|
+
@previewed = previewed
|
|
378
|
+
else
|
|
379
|
+
# A preview that halts or fails is a failed transition: its failure
|
|
380
|
+
# callbacks run, as in a full run, without marking the after phase
|
|
381
|
+
# as done (the full run that may follow runs its own)
|
|
382
|
+
after
|
|
383
|
+
@after_run = false
|
|
486
384
|
end
|
|
487
|
-
|
|
488
|
-
@fiber_thread_storage = result[1] if result.length == 2 && result[1].is_a?(Hash)
|
|
489
|
-
result[0]
|
|
490
|
-
end
|
|
491
|
-
|
|
492
|
-
# Pauses the current callback execution. This should only occur within
|
|
493
|
-
# around callbacks when the remainder of the callback will be executed at
|
|
494
|
-
# a later point in time.
|
|
495
|
-
def pause
|
|
496
|
-
# Don't pause if we're in the middle of resuming
|
|
497
|
-
return if @resuming
|
|
498
|
-
|
|
499
|
-
# Only yield if we're actually inside a fiber created by pausable
|
|
500
|
-
# We use a module extension to track this
|
|
501
|
-
current_fiber = Fiber.current
|
|
502
|
-
return unless current_fiber.respond_to?(:state_machine_fiber_pausable) && current_fiber.state_machine_fiber_pausable
|
|
503
|
-
|
|
504
|
-
Fiber.yield
|
|
505
|
-
|
|
506
|
-
# When we resume from the pause, execute the continuation block if present
|
|
507
|
-
return unless @continuation_block && !@result
|
|
508
|
-
|
|
509
|
-
action = { success: true }.merge(@continuation_block.call)
|
|
510
|
-
@result = action[:result]
|
|
511
|
-
@success = action[:success]
|
|
512
|
-
@continuation_block = nil
|
|
385
|
+
!halted
|
|
513
386
|
end
|
|
514
387
|
|
|
515
388
|
# Runs the machine's +before+ callbacks for this transition. Only
|
|
@@ -518,38 +391,28 @@ module StateMachines
|
|
|
518
391
|
#
|
|
519
392
|
# Once the callbacks are run, they cannot be run again until this transition
|
|
520
393
|
# is reset.
|
|
521
|
-
def before(
|
|
394
|
+
def before(index = 0, &)
|
|
522
395
|
return if @before_run
|
|
523
396
|
|
|
524
|
-
callback =
|
|
397
|
+
callback = before_callbacks[index]
|
|
525
398
|
|
|
526
399
|
if callback
|
|
527
|
-
#
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
#
|
|
535
|
-
callback
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
pause if @success && !complete
|
|
539
|
-
|
|
540
|
-
# If the block failed (success is false), we should halt
|
|
541
|
-
# the around callback from continuing
|
|
542
|
-
throw :halt unless @success
|
|
543
|
-
end
|
|
544
|
-
else
|
|
545
|
-
# Normal before callback
|
|
546
|
-
callback.call(object, context, self)
|
|
547
|
-
# Continue with next callback
|
|
548
|
-
before(complete, index + 1, &)
|
|
400
|
+
# Callback#call evaluates the callback's conditions (once) and answers
|
|
401
|
+
# whether it applied
|
|
402
|
+
if callback.type == :around
|
|
403
|
+
# The rest of the chain, and the action, run inside the around
|
|
404
|
+
applied = callback.call(object, context, self) do
|
|
405
|
+
before(index + 1, &)
|
|
406
|
+
|
|
407
|
+
# If the block failed (success is false), we should halt
|
|
408
|
+
# the around callback from continuing
|
|
409
|
+
throw :halt unless @success
|
|
549
410
|
end
|
|
411
|
+
before(index + 1, &) unless applied
|
|
550
412
|
else
|
|
551
|
-
#
|
|
552
|
-
|
|
413
|
+
# A preview already ran this before callback
|
|
414
|
+
callback.call(object, context, self) unless @previewed&.key?(callback)
|
|
415
|
+
before(index + 1, &)
|
|
553
416
|
end
|
|
554
417
|
else
|
|
555
418
|
# No more callbacks, execute the action block if at the end
|
|
@@ -582,13 +445,11 @@ module StateMachines
|
|
|
582
445
|
def after
|
|
583
446
|
return if @after_run
|
|
584
447
|
|
|
585
|
-
# Restore the fiber's thread storage to ensure consistency
|
|
586
|
-
# This preserves Thread.current state from before/around callbacks to after callbacks
|
|
587
|
-
@fiber_thread_storage.each { |key, value| Thread.current[key] = value } if @fiber_thread_storage
|
|
588
|
-
|
|
589
448
|
catch(:halt) do
|
|
590
|
-
|
|
591
|
-
|
|
449
|
+
# A branch's afters run before the machine's, closest to the change;
|
|
450
|
+
# they run on success only (a branch has no after_failure)
|
|
451
|
+
callbacks = @success ? branch_callbacks(:after) + machine.callbacks[:after] : machine.callbacks[:failure]
|
|
452
|
+
callbacks.each { |callback| callback.call(object, context, self) }
|
|
592
453
|
end
|
|
593
454
|
|
|
594
455
|
@after_run = true
|
|
@@ -605,5 +466,20 @@ module StateMachines
|
|
|
605
466
|
def context
|
|
606
467
|
@context ||= { on: event, from: from_name, to: to_name }
|
|
607
468
|
end
|
|
469
|
+
|
|
470
|
+
# The before chain: the machine's before / around callbacks, then the
|
|
471
|
+
# selected branch's, so branch callbacks wrap closest to the state change.
|
|
472
|
+
# It is taken once per run: a callback registered while the chain runs
|
|
473
|
+
# (e.g. from inside a before_transition) applies from the next transition,
|
|
474
|
+
# not to the one in progress, whose chain cannot change under it.
|
|
475
|
+
def before_callbacks
|
|
476
|
+
@before_callbacks ||= machine.callbacks[:before] + branch_callbacks(:before, :around)
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
def branch_callbacks(*types)
|
|
480
|
+
return [] unless branch
|
|
481
|
+
|
|
482
|
+
branch.callbacks.select { |callback| types.include?(callback.type) }
|
|
483
|
+
end
|
|
608
484
|
end
|
|
609
485
|
end
|
|
@@ -34,17 +34,17 @@ module StateMachines
|
|
|
34
34
|
attributes = map(&:attribute).uniq
|
|
35
35
|
raise ArgumentError, 'Cannot perform multiple transitions in parallel for the same state machine attribute' if attributes.length != length
|
|
36
36
|
|
|
37
|
-
StateMachines::OptionsValidator.assert_valid_keys!(options, :actions, :after, :use_transactions
|
|
37
|
+
StateMachines::OptionsValidator.assert_valid_keys!(options, :actions, :after, :use_transactions)
|
|
38
38
|
options = { actions: true, after: true, use_transactions: true }.merge(options)
|
|
39
39
|
@skip_actions = !options[:actions]
|
|
40
40
|
@skip_after = !options[:after]
|
|
41
41
|
@use_transactions = options[:use_transactions]
|
|
42
42
|
@options = options
|
|
43
43
|
|
|
44
|
-
# Reset transitions when creating a new collection
|
|
45
|
-
#
|
|
44
|
+
# Reset transitions when creating a new collection, except those a
|
|
45
|
+
# preview run started, which this collection continues
|
|
46
46
|
each do |transition|
|
|
47
|
-
transition.reset unless transition.
|
|
47
|
+
transition.reset unless transition.previewed?
|
|
48
48
|
end
|
|
49
49
|
end
|
|
50
50
|
|
|
@@ -106,7 +106,7 @@ module StateMachines
|
|
|
106
106
|
end
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
-
# The fiber
|
|
109
|
+
# The stack (Thread.current, so per fiber) of collections whose actions are running
|
|
110
110
|
RUNNING_ACTIONS_KEY = :state_machines_running_actions
|
|
111
111
|
|
|
112
112
|
# The reason an event attribute performed inside this collection's action
|
|
@@ -228,19 +228,11 @@ module StateMachines
|
|
|
228
228
|
# If any transition fails to run its callbacks, :halt will be thrown.
|
|
229
229
|
def run_callbacks(index = 0, &)
|
|
230
230
|
if (transition = self[index])
|
|
231
|
-
|
|
232
|
-
callback_options = { after: !skip_after }
|
|
233
|
-
callback_options[:fiber] = options[:fiber] if options.key?(:fiber)
|
|
234
|
-
|
|
235
|
-
callback_result = transition.run_callbacks(callback_options) do
|
|
231
|
+
callback_result = transition.run_callbacks(after: !skip_after) do
|
|
236
232
|
run_callbacks(index + 1, &)
|
|
237
233
|
{ result: results[transition.action], success: success? }
|
|
238
234
|
end
|
|
239
235
|
|
|
240
|
-
# If we're skipping after callbacks and the transition is paused,
|
|
241
|
-
# consider it successful (the pause was intentional)
|
|
242
|
-
@success = true if skip_after && transition.paused?
|
|
243
|
-
|
|
244
236
|
unless callback_result
|
|
245
237
|
# The innermost transition to halt is the one that caused it; the
|
|
246
238
|
# ones wrapping it halt as a consequence
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: state_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.304.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -88,7 +88,6 @@ files:
|
|
|
88
88
|
- lib/state_machines/machine/scoping.rb
|
|
89
89
|
- lib/state_machines/machine/state_methods.rb
|
|
90
90
|
- lib/state_machines/machine/utilities.rb
|
|
91
|
-
- lib/state_machines/machine/validation.rb
|
|
92
91
|
- lib/state_machines/machine_collection.rb
|
|
93
92
|
- lib/state_machines/macro_methods.rb
|
|
94
93
|
- lib/state_machines/matcher.rb
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module StateMachines
|
|
4
|
-
class Machine
|
|
5
|
-
module Validation
|
|
6
|
-
# Frozen constant to avoid repeated array allocations
|
|
7
|
-
DANGEROUS_PATTERNS = [
|
|
8
|
-
/`.*`/, # Backticks (shell execution)
|
|
9
|
-
/system\s*\(/, # System calls
|
|
10
|
-
/exec\s*\(/, # Exec calls
|
|
11
|
-
/eval\s*\(/, # Nested eval
|
|
12
|
-
/require\s+['"]/, # Require statements
|
|
13
|
-
/load\s+['"]/, # Load statements
|
|
14
|
-
/File\./, # File operations
|
|
15
|
-
/IO\./, # IO operations
|
|
16
|
-
/Dir\./, # Directory operations
|
|
17
|
-
/Kernel\./ # Kernel operations
|
|
18
|
-
].freeze
|
|
19
|
-
|
|
20
|
-
private
|
|
21
|
-
|
|
22
|
-
# Validates string input before eval to prevent code injection
|
|
23
|
-
# This is a basic safety check - not foolproof security
|
|
24
|
-
def validate_eval_string(method_string)
|
|
25
|
-
# Check for obviously dangerous patterns
|
|
26
|
-
DANGEROUS_PATTERNS.each do |pattern|
|
|
27
|
-
raise SecurityError, "Potentially dangerous code detected in eval string: #{method_string.inspect}" if method_string.match?(pattern)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Basic syntax validation (cross-platform)
|
|
31
|
-
begin
|
|
32
|
-
SyntaxValidator.validate!(method_string, '(eval)')
|
|
33
|
-
rescue SyntaxError => e
|
|
34
|
-
raise ArgumentError, "Invalid Ruby syntax in eval string: #{e.message}"
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|