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
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module StateMachines
|
|
4
|
-
module AsyncMode
|
|
5
|
-
# Enhanced machine class with async capabilities
|
|
6
|
-
module AsyncMachine
|
|
7
|
-
# Thread-safe state reading for machines
|
|
8
|
-
def read_safely(object, attribute, ivar = false)
|
|
9
|
-
if object.respond_to?(:read_state_safely)
|
|
10
|
-
object.read_state_safely(self, attribute, ivar)
|
|
11
|
-
else
|
|
12
|
-
read(object, attribute, ivar)
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
# Thread-safe state writing for machines
|
|
17
|
-
def write_safely(object, attribute, value, ivar = false)
|
|
18
|
-
if object.respond_to?(:write_state_safely)
|
|
19
|
-
object.write_state_safely(self, attribute, value, ivar)
|
|
20
|
-
else
|
|
21
|
-
write(object, attribute, value, ivar)
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# Fires an event asynchronously on the given object
|
|
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
|
|
31
|
-
|
|
32
|
-
Async do
|
|
33
|
-
events[event_name].fire(object, *args)
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# Creates an async-aware transition collection
|
|
38
|
-
# Supports concurrent transition execution with proper synchronization
|
|
39
|
-
def create_async_transition_collection(transitions, options = {})
|
|
40
|
-
if defined?(AsyncTransitionCollection)
|
|
41
|
-
AsyncTransitionCollection.new(transitions, options)
|
|
42
|
-
else
|
|
43
|
-
# Fallback to regular collection if async collection isn't available
|
|
44
|
-
TransitionCollection.new(transitions, options)
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# Thread-safe callback execution for async operations
|
|
49
|
-
def run_callbacks_safely(type, object, context, transition)
|
|
50
|
-
if object.respond_to?(:state_machine_mutex)
|
|
51
|
-
object.state_machine_mutex.with_read_lock do
|
|
52
|
-
callbacks[type].each { |callback| callback.call(object, context, transition) }
|
|
53
|
-
end
|
|
54
|
-
else
|
|
55
|
-
callbacks[type].each { |callback| callback.call(object, context, transition) }
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module StateMachines
|
|
4
|
-
module AsyncMode
|
|
5
|
-
# Error class for async-specific transition failures
|
|
6
|
-
class AsyncTransitionError < StateMachines::Error
|
|
7
|
-
def initialize(object, machines, failed_events)
|
|
8
|
-
@object = object
|
|
9
|
-
@machines = machines
|
|
10
|
-
@failed_events = failed_events
|
|
11
|
-
super("Failed to perform async transitions: #{failed_events.join(', ')}")
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
attr_reader :object, :machines, :failed_events
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
# Async-aware transition collection that can execute transitions concurrently
|
|
18
|
-
class AsyncTransitionCollection < TransitionCollection
|
|
19
|
-
# Performs transitions asynchronously using Async
|
|
20
|
-
# Provides better concurrency for I/O-bound operations
|
|
21
|
-
def perform_async(&block)
|
|
22
|
-
reset
|
|
23
|
-
|
|
24
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
25
|
-
return Async do
|
|
26
|
-
perform_async(&block)
|
|
27
|
-
end.wait
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
if valid?
|
|
31
|
-
# Create async tasks for each transition
|
|
32
|
-
tasks = map do |transition|
|
|
33
|
-
Async do
|
|
34
|
-
execute_transition(transition, &block)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Wait for all tasks to complete
|
|
39
|
-
completed_transitions = []
|
|
40
|
-
tasks.each do |task|
|
|
41
|
-
begin
|
|
42
|
-
result = task.wait
|
|
43
|
-
completed_transitions << result if result
|
|
44
|
-
rescue StandardError => e
|
|
45
|
-
# Handle individual transition failures
|
|
46
|
-
rollback
|
|
47
|
-
raise AsyncTransitionError.new(object, map(&:machine), [e.message])
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
# Check if all transitions succeeded
|
|
52
|
-
@success = completed_transitions.length == length
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
success?
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
# Performs transitions concurrently using threads
|
|
59
|
-
# Better for CPU-bound operations but requires more careful synchronization
|
|
60
|
-
def perform_threaded(&block)
|
|
61
|
-
reset
|
|
62
|
-
|
|
63
|
-
if valid?
|
|
64
|
-
# Use basic thread approach
|
|
65
|
-
threads = []
|
|
66
|
-
results = []
|
|
67
|
-
results_mutex = Concurrent::ReentrantReadWriteLock.new
|
|
68
|
-
|
|
69
|
-
each do |transition|
|
|
70
|
-
threads << Thread.new do
|
|
71
|
-
begin
|
|
72
|
-
result = execute_transition(transition, &block)
|
|
73
|
-
|
|
74
|
-
results_mutex.with_write_lock { results << result }
|
|
75
|
-
rescue StandardError => e
|
|
76
|
-
# Handle individual transition failures
|
|
77
|
-
rollback
|
|
78
|
-
raise AsyncTransitionError.new(object, [transition.machine], [e.message])
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# Wait for all threads to complete
|
|
84
|
-
threads.each(&:join)
|
|
85
|
-
@success = results.length == length
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
success?
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
private
|
|
92
|
-
|
|
93
|
-
# Runs a single transition either via event attributes or the standard
|
|
94
|
-
# callback/transaction flow, returning the transition on completion
|
|
95
|
-
def execute_transition(transition, &block)
|
|
96
|
-
if use_event_attributes? && !block_given?
|
|
97
|
-
transition.transient = true
|
|
98
|
-
transition.machine.write_safely(object, :event_transition, transition)
|
|
99
|
-
run_actions
|
|
100
|
-
else
|
|
101
|
-
within_transaction do
|
|
102
|
-
catch(:halt) { run_callbacks(&block) }
|
|
103
|
-
rollback unless success?
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
transition
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
# Override run_actions to be thread-safe when needed
|
|
111
|
-
def run_actions(&block)
|
|
112
|
-
catch_exceptions do
|
|
113
|
-
@success = if block_given?
|
|
114
|
-
result = yield
|
|
115
|
-
actions.each { |action| results[action] = result }
|
|
116
|
-
!!result
|
|
117
|
-
else
|
|
118
|
-
actions.compact.each do |action|
|
|
119
|
-
next if skip_actions
|
|
120
|
-
|
|
121
|
-
# Use thread-safe write for results
|
|
122
|
-
if object.respond_to?(:state_machine_mutex)
|
|
123
|
-
object.state_machine_mutex.with_write_lock do
|
|
124
|
-
results[action] = object.send(action)
|
|
125
|
-
end
|
|
126
|
-
else
|
|
127
|
-
results[action] = object.send(action)
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
results.values.all?
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
end
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module StateMachines
|
|
4
|
-
module AsyncMode
|
|
5
|
-
# Thread-safe state operations for async-enabled state machines
|
|
6
|
-
# Uses concurrent-ruby for enterprise-grade thread safety
|
|
7
|
-
module ThreadSafeState
|
|
8
|
-
# Gets or creates a reentrant mutex for thread-safe state operations on an object
|
|
9
|
-
# Each object gets its own mutex to avoid global locking
|
|
10
|
-
# Uses Concurrent::ReentrantReadWriteLock for better performance
|
|
11
|
-
def state_machine_mutex
|
|
12
|
-
@_state_machine_mutex ||= Concurrent::ReentrantReadWriteLock.new
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
# Thread-safe version of state reading
|
|
16
|
-
# Ensures atomic read operations across concurrent threads
|
|
17
|
-
def read_state_safely(machine, attribute, ivar = false)
|
|
18
|
-
state_machine_mutex.with_read_lock do
|
|
19
|
-
machine.read(self, attribute, ivar)
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Thread-safe version of state writing
|
|
24
|
-
# Ensures atomic write operations across concurrent threads
|
|
25
|
-
def write_state_safely(machine, attribute, value, ivar = false)
|
|
26
|
-
state_machine_mutex.with_write_lock do
|
|
27
|
-
machine.write(self, attribute, value, ivar)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Handle marshalling by excluding the mutex (will be recreated when needed)
|
|
32
|
-
def marshal_dump
|
|
33
|
-
# Get instance variables excluding the mutex
|
|
34
|
-
vars = instance_variables.reject { |var| var == :@_state_machine_mutex }
|
|
35
|
-
vars.map { |var| [var, instance_variable_get(var)] }
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Restore marshalled object, mutex will be lazily recreated when needed
|
|
39
|
-
def marshal_load(data)
|
|
40
|
-
data.each do |var, value|
|
|
41
|
-
instance_variable_set(var, value)
|
|
42
|
-
end
|
|
43
|
-
# Don't set @_state_machine_mutex - let it be lazily created
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
end
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# Ruby Engine Compatibility Check
|
|
4
|
-
# The async gem requires native extensions and Fiber scheduler support
|
|
5
|
-
# which are not available on JRuby or TruffleRuby
|
|
6
|
-
if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'truffleruby'
|
|
7
|
-
raise LoadError, <<~ERROR
|
|
8
|
-
StateMachines::AsyncMode is not available on #{RUBY_ENGINE}.
|
|
9
|
-
|
|
10
|
-
The async gem requires native extensions (io-event) and Fiber scheduler support
|
|
11
|
-
which are not implemented in #{RUBY_ENGINE}. AsyncMode is only supported on:
|
|
12
|
-
|
|
13
|
-
• MRI Ruby (CRuby) 3.2+
|
|
14
|
-
• Other Ruby engines with full Fiber scheduler support
|
|
15
|
-
|
|
16
|
-
If you need async support on #{RUBY_ENGINE}, consider using:
|
|
17
|
-
• java.util.concurrent classes (JRuby)
|
|
18
|
-
• Native threading libraries for your platform
|
|
19
|
-
• Or stick with synchronous state machines
|
|
20
|
-
ERROR
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Load required gems with version constraints
|
|
24
|
-
gem 'async', '>= 2.25.0'
|
|
25
|
-
gem 'concurrent-ruby', '>= 1.3.5' # Security is not negotiable - enterprise-grade thread safety required
|
|
26
|
-
|
|
27
|
-
require 'async'
|
|
28
|
-
require 'concurrent-ruby'
|
|
29
|
-
|
|
30
|
-
# Load all async mode components
|
|
31
|
-
require_relative 'async_mode/thread_safe_state'
|
|
32
|
-
require_relative 'async_mode/async_events'
|
|
33
|
-
require_relative 'async_mode/async_event_extensions'
|
|
34
|
-
require_relative 'async_mode/async_machine'
|
|
35
|
-
require_relative 'async_mode/async_transition_collection'
|
|
36
|
-
|
|
37
|
-
module StateMachines
|
|
38
|
-
# AsyncMode provides asynchronous state machine capabilities using the async gem
|
|
39
|
-
# This module enables concurrent, thread-safe state operations for high-performance applications
|
|
40
|
-
#
|
|
41
|
-
# @example Basic usage
|
|
42
|
-
# class AutonomousDrone < StarfleetShip
|
|
43
|
-
# state_machine :teleporter_status, async: true do
|
|
44
|
-
# event :power_up do
|
|
45
|
-
# transition offline: :charging
|
|
46
|
-
# end
|
|
47
|
-
# end
|
|
48
|
-
# end
|
|
49
|
-
#
|
|
50
|
-
# drone = AutonomousDrone.new
|
|
51
|
-
# Async do
|
|
52
|
-
# result = drone.fire_event_async(:power_up) # => true
|
|
53
|
-
# task = drone.power_up_async! # => Async::Task
|
|
54
|
-
# end
|
|
55
|
-
#
|
|
56
|
-
module AsyncMode
|
|
57
|
-
# All components are loaded from separate files:
|
|
58
|
-
# - ThreadSafeState: Mutex-based thread safety
|
|
59
|
-
# - AsyncEvents: Async event firing methods
|
|
60
|
-
# - AsyncEventExtensions: Event method generation
|
|
61
|
-
# - AsyncMachine: Machine-level async capabilities
|
|
62
|
-
# - AsyncTransitionCollection: Concurrent transition execution
|
|
63
|
-
end
|
|
64
|
-
end
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# This file provides optional async extensions for the Machine class.
|
|
4
|
-
# It should only be loaded when async functionality is explicitly requested.
|
|
5
|
-
|
|
6
|
-
module StateMachines
|
|
7
|
-
class Machine
|
|
8
|
-
# AsyncMode extensions for the Machine class
|
|
9
|
-
# Provides async-aware methods while maintaining backward compatibility
|
|
10
|
-
module AsyncExtensions
|
|
11
|
-
# Instance methods added to Machine for async support
|
|
12
|
-
|
|
13
|
-
# Configure this specific machine instance for async mode
|
|
14
|
-
#
|
|
15
|
-
# Example:
|
|
16
|
-
# class Vehicle
|
|
17
|
-
# state_machine initial: :parked do
|
|
18
|
-
# configure_async_mode! # Enable async for this machine
|
|
19
|
-
#
|
|
20
|
-
# event :ignite do
|
|
21
|
-
# transition parked: :idling
|
|
22
|
-
# end
|
|
23
|
-
# end
|
|
24
|
-
# end
|
|
25
|
-
def configure_async_mode!(enabled = true)
|
|
26
|
-
if enabled
|
|
27
|
-
begin
|
|
28
|
-
require 'state_machines/async_mode'
|
|
29
|
-
@async_mode_enabled = true
|
|
30
|
-
|
|
31
|
-
owner_class.include(StateMachines::AsyncMode::ThreadSafeState)
|
|
32
|
-
owner_class.include(StateMachines::AsyncMode::AsyncEvents)
|
|
33
|
-
extend(StateMachines::AsyncMode::AsyncMachine)
|
|
34
|
-
|
|
35
|
-
# Extend events to generate async versions
|
|
36
|
-
events.each do |event|
|
|
37
|
-
event.extend(StateMachines::AsyncMode::AsyncEventExtensions)
|
|
38
|
-
end
|
|
39
|
-
rescue LoadError => e
|
|
40
|
-
# Fallback to sync mode with warning (only once per class)
|
|
41
|
-
unless owner_class.instance_variable_get(:@async_fallback_warned)
|
|
42
|
-
warn <<~WARNING
|
|
43
|
-
⚠️ #{owner_class.name}: Async mode requested but not available on #{RUBY_ENGINE}.
|
|
44
|
-
|
|
45
|
-
#{e.message}
|
|
46
|
-
|
|
47
|
-
⚠️ Falling back to synchronous mode. Results may be unpredictable due to engine limitations.
|
|
48
|
-
For production async support, use MRI Ruby (CRuby) 3.2+
|
|
49
|
-
WARNING
|
|
50
|
-
owner_class.instance_variable_set(:@async_fallback_warned, true)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
@async_mode_enabled = false
|
|
54
|
-
end
|
|
55
|
-
else
|
|
56
|
-
@async_mode_enabled = false
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
self
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# Check if this specific machine instance has async mode enabled
|
|
63
|
-
def async_mode_enabled?
|
|
64
|
-
@async_mode_enabled || false
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
# Thread-safe version of state reading
|
|
68
|
-
def read_safely(object, attribute, ivar = false)
|
|
69
|
-
object.read_state_safely(self, attribute, ivar)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
# Thread-safe version of state writing
|
|
73
|
-
def write_safely(object, attribute, value, ivar = false)
|
|
74
|
-
object.write_state_safely(self, attribute, value, ivar)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# Thread-safe callback execution for async operations
|
|
78
|
-
def run_callbacks_safely(type, object, context, transition)
|
|
79
|
-
object.state_machine_mutex.with_write_lock do
|
|
80
|
-
callbacks[type].each { |callback| callback.call(object, context, transition) }
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
# Include async extensions by default (but only load AsyncMode when requested)
|
|
86
|
-
include AsyncExtensions
|
|
87
|
-
end
|
|
88
|
-
end
|
|
@@ -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
|