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.
- checksums.yaml +4 -4
- data/README.md +11 -115
- 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 +4 -2
- data/lib/state_machines/extensions.rb +5 -5
- data/lib/state_machines/machine/action_hooks.rb +13 -10
- data/lib/state_machines/machine/class_methods.rb +27 -8
- data/lib/state_machines/machine/configuration.rb +8 -6
- 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 +54 -3
- 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/test_helper.rb +128 -288
- data/lib/state_machines/transition_claim.rb +5 -4
- data/lib/state_machines/version.rb +1 -1
- metadata +1 -9
- data/lib/state_machines/async_mode/async_event_extensions.rb +0 -45
- data/lib/state_machines/async_mode/async_events.rb +0 -270
- data/lib/state_machines/async_mode/async_machine.rb +0 -58
- data/lib/state_machines/async_mode/async_transition_collection.rb +0 -64
- data/lib/state_machines/async_mode/thread_safe_state.rb +0 -51
- 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,270 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module StateMachines
|
|
4
|
-
module AsyncMode
|
|
5
|
-
# Async-aware event firing capabilities using the async gem
|
|
6
|
-
module AsyncEvents
|
|
7
|
-
# Fires an event asynchronously using Async
|
|
8
|
-
# Returns an Async::Task that can be awaited for the result
|
|
9
|
-
#
|
|
10
|
-
# @return [Async::Task] a task whose result is the performed
|
|
11
|
-
# {StateMachines::Transition}, or false if the event was refused
|
|
12
|
-
# @example
|
|
13
|
-
# Async do
|
|
14
|
-
# task = vehicle.async_fire_event(:ignite)
|
|
15
|
-
# result = task.wait # => #<StateMachines::Transition ...> or false
|
|
16
|
-
# end
|
|
17
|
-
def async_fire_event(event_name, *)
|
|
18
|
-
# Find the machine that has this event
|
|
19
|
-
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
20
|
-
|
|
21
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
22
|
-
|
|
23
|
-
# Must be called within an Async context
|
|
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?
|
|
25
|
-
|
|
26
|
-
Async do
|
|
27
|
-
machine.events[event_name].fire(self, *)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Fires multiple events asynchronously across different state machines
|
|
32
|
-
# Returns an array of Async::Tasks for concurrent execution
|
|
33
|
-
#
|
|
34
|
-
# @return [Array<Async::Task>] one task per event, each resolving to the
|
|
35
|
-
# performed {StateMachines::Transition} or false
|
|
36
|
-
# @example
|
|
37
|
-
# Async do
|
|
38
|
-
# tasks = vehicle.async_fire_events(:ignite, :buy_insurance)
|
|
39
|
-
# results = tasks.map(&:wait) # => [#<StateMachines::Transition ...>, #<StateMachines::Transition ...>]
|
|
40
|
-
# end
|
|
41
|
-
def async_fire_events(*event_names)
|
|
42
|
-
event_names.map { |event_name| async_fire_event(event_name) }
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
# Fires an event asynchronously and waits for completion
|
|
46
|
-
# This is a convenience method that creates and waits for the task
|
|
47
|
-
#
|
|
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, *)
|
|
53
|
-
raise NoMethodError, "undefined method `fire_event_async' for #{self}" unless has_async_machines?
|
|
54
|
-
|
|
55
|
-
# Find the machine that has this event
|
|
56
|
-
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
57
|
-
|
|
58
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
59
|
-
|
|
60
|
-
if defined?(::Async::Task) && ::Async::Task.current?
|
|
61
|
-
# Already in async context, just fire directly
|
|
62
|
-
machine.events[event_name].fire(self, *)
|
|
63
|
-
else
|
|
64
|
-
# Create async context and wait for result
|
|
65
|
-
Async do
|
|
66
|
-
machine.events[event_name].fire(self, *)
|
|
67
|
-
end.wait
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# Fires multiple events asynchronously and waits for all completions
|
|
72
|
-
# Returns results in the same order as the input events
|
|
73
|
-
#
|
|
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 ...>]
|
|
78
|
-
def fire_events_async(*event_names)
|
|
79
|
-
raise NoMethodError, "undefined method `fire_events_async' for #{self}" unless has_async_machines?
|
|
80
|
-
|
|
81
|
-
if defined?(::Async::Task) && ::Async::Task.current?
|
|
82
|
-
# Already in async context, run concurrently
|
|
83
|
-
tasks = event_names.map { |event_name| async_fire_event(event_name) }
|
|
84
|
-
tasks.map(&:wait)
|
|
85
|
-
else
|
|
86
|
-
# Create async context and run concurrently
|
|
87
|
-
Async do
|
|
88
|
-
tasks = event_names.map { |event_name| async_fire_event(event_name) }
|
|
89
|
-
tasks.map(&:wait)
|
|
90
|
-
end.wait
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# Fires an event asynchronously using Async and raises exception on failure
|
|
95
|
-
# Returns an Async::Task that raises StateMachines::InvalidTransition when awaited
|
|
96
|
-
#
|
|
97
|
-
# Example:
|
|
98
|
-
# Async do
|
|
99
|
-
# begin
|
|
100
|
-
# task = vehicle.async_fire_event!(:ignite)
|
|
101
|
-
# result = task.wait
|
|
102
|
-
# puts "Event fired successfully!"
|
|
103
|
-
# rescue StateMachines::InvalidTransition => e
|
|
104
|
-
# puts "Transition failed: #{e.message}"
|
|
105
|
-
# end
|
|
106
|
-
# end
|
|
107
|
-
def async_fire_event!(event_name, *)
|
|
108
|
-
# Find the machine that has this event
|
|
109
|
-
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
110
|
-
|
|
111
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
112
|
-
|
|
113
|
-
# Must be called within an Async context
|
|
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
|
-
|
|
116
|
-
Async do
|
|
117
|
-
fire_with_bang(machine, event_name, *)
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
# Fires an event asynchronously and waits for result, raising exceptions on failure
|
|
122
|
-
# This is a convenience method that creates and waits for the task
|
|
123
|
-
#
|
|
124
|
-
# Example:
|
|
125
|
-
# begin
|
|
126
|
-
# result = vehicle.fire_event_async!(:ignite)
|
|
127
|
-
# puts "Event fired successfully!"
|
|
128
|
-
# rescue StateMachines::InvalidTransition => e
|
|
129
|
-
# puts "Transition failed: #{e.message}"
|
|
130
|
-
# end
|
|
131
|
-
def fire_event_async!(event_name, *)
|
|
132
|
-
raise NoMethodError, "undefined method `fire_event_async!' for #{self}" unless has_async_machines?
|
|
133
|
-
|
|
134
|
-
# Find the machine that has this event
|
|
135
|
-
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
136
|
-
|
|
137
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
138
|
-
|
|
139
|
-
if defined?(::Async::Task) && ::Async::Task.current?
|
|
140
|
-
# Already in async context, just fire directly with bang behavior
|
|
141
|
-
fire_with_bang(machine, event_name, *)
|
|
142
|
-
else
|
|
143
|
-
# Create async context and wait for result (may raise exception)
|
|
144
|
-
Async do
|
|
145
|
-
fire_with_bang(machine, event_name, *)
|
|
146
|
-
end.wait
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
# Dynamically handle individual event async methods
|
|
151
|
-
# This provides launch_async, launch_async!, arm_weapons_async, etc.
|
|
152
|
-
def method_missing(method_name, *, **, &)
|
|
153
|
-
method_str = method_name.to_s
|
|
154
|
-
|
|
155
|
-
# Check if this is an async event method
|
|
156
|
-
if method_str.end_with?('_async!')
|
|
157
|
-
# Remove the _async! suffix to get the base event method
|
|
158
|
-
base_method = method_str.chomp('_async!').to_sym
|
|
159
|
-
|
|
160
|
-
# Check if the base method exists and this machine is async-enabled
|
|
161
|
-
return handle_individual_event_async_bang(base_method, *, **) if respond_to?(base_method) && async_method_for_event?(base_method)
|
|
162
|
-
elsif method_str.end_with?('_async')
|
|
163
|
-
# Remove the _async suffix to get the base event method
|
|
164
|
-
base_method = method_str.chomp('_async').to_sym
|
|
165
|
-
|
|
166
|
-
# Check if the base method exists and this machine is async-enabled
|
|
167
|
-
return handle_individual_event_async(base_method, *, **) if respond_to?(base_method) && async_method_for_event?(base_method)
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
# If not an async method, call the original method_missing
|
|
171
|
-
super
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
# Check if we should respond to async methods for this event
|
|
175
|
-
def respond_to_missing?(method_name, include_private = false)
|
|
176
|
-
# Only provide async methods if this object has async-enabled machines
|
|
177
|
-
return super unless has_async_machines?
|
|
178
|
-
|
|
179
|
-
method_str = method_name.to_s
|
|
180
|
-
|
|
181
|
-
if method_str.end_with?('_async!') || method_str.end_with?('_async')
|
|
182
|
-
base_method = method_str.chomp('_async!').chomp('_async').to_sym
|
|
183
|
-
return respond_to?(base_method) && async_method_for_event?(base_method)
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
super
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
# Check if this object has any async-enabled state machines
|
|
190
|
-
def has_async_machines?
|
|
191
|
-
self.class.state_machines.any? { |_name, machine| machine.async_mode_enabled? }
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
private
|
|
195
|
-
|
|
196
|
-
# Fires the event on the given machine, raising InvalidTransition on failure
|
|
197
|
-
def fire_with_bang(machine, event_name, *)
|
|
198
|
-
machine.events[event_name].fire(self, *) || raise(StateMachines::InvalidTransition.new(self, machine, event_name))
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
# Check if this event method should have async versions
|
|
202
|
-
def async_method_for_event?(event_method)
|
|
203
|
-
# Find which machine contains this event
|
|
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
|
|
211
|
-
end
|
|
212
|
-
end
|
|
213
|
-
false
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
# Handle individual event async methods (returns task)
|
|
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?
|
|
219
|
-
|
|
220
|
-
Async do
|
|
221
|
-
send(event_method, *, **)
|
|
222
|
-
end
|
|
223
|
-
end
|
|
224
|
-
|
|
225
|
-
# Handle individual event async bang methods (returns task, raises on failure)
|
|
226
|
-
def handle_individual_event_async_bang(event_method, *, **)
|
|
227
|
-
# Extract event name from method and use bang version
|
|
228
|
-
bang_method = :"#{event_method}!"
|
|
229
|
-
|
|
230
|
-
raise "#{event_method}_async! must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
|
|
231
|
-
|
|
232
|
-
Async do
|
|
233
|
-
send(bang_method, *, **)
|
|
234
|
-
end
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
# Extract event name from method name, handling namespaced events
|
|
238
|
-
def extract_event_name(method_name)
|
|
239
|
-
method_str = method_name.to_s
|
|
240
|
-
|
|
241
|
-
# Find the machine and event for this method
|
|
242
|
-
self.class.state_machines.each do |_name, machine|
|
|
243
|
-
machine.events.each do |event|
|
|
244
|
-
qualified_name = event.qualified_name
|
|
245
|
-
return event.name if qualified_name.to_s == method_str || "#{qualified_name}!" == method_str
|
|
246
|
-
end
|
|
247
|
-
end
|
|
248
|
-
|
|
249
|
-
# Fallback: assume the method name is the event name
|
|
250
|
-
method_str.chomp('!').to_sym
|
|
251
|
-
end
|
|
252
|
-
|
|
253
|
-
public
|
|
254
|
-
|
|
255
|
-
# Fires multiple events concurrently within an async context
|
|
256
|
-
# This method should be called from within an Async block
|
|
257
|
-
#
|
|
258
|
-
# Example:
|
|
259
|
-
# Async do
|
|
260
|
-
# results = vehicle.fire_events_concurrent(:ignite, :buy_insurance)
|
|
261
|
-
# end
|
|
262
|
-
def fire_events_concurrent(*event_names)
|
|
263
|
-
raise 'fire_events_concurrent must be called within an Async context' unless defined?(::Async::Task) && ::Async::Task.current?
|
|
264
|
-
|
|
265
|
-
tasks = async_fire_events(*event_names)
|
|
266
|
-
tasks.map(&:wait)
|
|
267
|
-
end
|
|
268
|
-
end
|
|
269
|
-
end
|
|
270
|
-
end
|
|
@@ -1,58 +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, *)
|
|
28
|
-
raise 'async_fire_event must be called within an Async context' unless defined?(::Async::Task) && ::Async::Task.current?
|
|
29
|
-
|
|
30
|
-
Async do
|
|
31
|
-
events[event_name].fire(object, *)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# Creates an async-aware transition collection
|
|
36
|
-
# Supports concurrent transition execution with proper synchronization
|
|
37
|
-
def create_async_transition_collection(transitions, options = {})
|
|
38
|
-
if defined?(AsyncTransitionCollection)
|
|
39
|
-
AsyncTransitionCollection.new(transitions, options)
|
|
40
|
-
else
|
|
41
|
-
# Fallback to regular collection if async collection isn't available
|
|
42
|
-
TransitionCollection.new(transitions, options)
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# Thread-safe callback execution for async operations
|
|
47
|
-
def run_callbacks_safely(type, object, context, transition)
|
|
48
|
-
if object.respond_to?(:state_machine_mutex)
|
|
49
|
-
object.state_machine_mutex.with_read_lock do
|
|
50
|
-
callbacks[type].each { |callback| callback.call(object, context, transition) }
|
|
51
|
-
end
|
|
52
|
-
else
|
|
53
|
-
callbacks[type].each { |callback| callback.call(object, context, transition) }
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
@@ -1,64 +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 the collection inside an Async task, entering a reactor when
|
|
20
|
-
# the caller is not already in one.
|
|
21
|
-
#
|
|
22
|
-
# The callbacks and the action belong to the collection, not to one
|
|
23
|
-
# transition, so they run once. What is asynchronous is the collection as
|
|
24
|
-
# a whole, which is what lets an I/O-bound action yield to its reactor.
|
|
25
|
-
def perform_async(&)
|
|
26
|
-
return perform(&) if defined?(::Async::Task) && ::Async::Task.current?
|
|
27
|
-
|
|
28
|
-
Async { perform(&) }.wait
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Performs the collection on another thread and waits for its result.
|
|
32
|
-
def perform_threaded(&)
|
|
33
|
-
Thread.new { perform(&) }.value
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
private
|
|
37
|
-
|
|
38
|
-
# Override run_actions to be thread-safe when needed
|
|
39
|
-
def run_actions(&)
|
|
40
|
-
catch_exceptions do
|
|
41
|
-
@success = if block_given?
|
|
42
|
-
result = yield
|
|
43
|
-
actions.each { |action| results[action] = result }
|
|
44
|
-
!!result
|
|
45
|
-
else
|
|
46
|
-
actions.compact.each do |action|
|
|
47
|
-
next if skip_actions
|
|
48
|
-
|
|
49
|
-
# Use thread-safe write for results
|
|
50
|
-
if object.respond_to?(:state_machine_mutex)
|
|
51
|
-
object.state_machine_mutex.with_write_lock do
|
|
52
|
-
results[action] = object.send(action)
|
|
53
|
-
end
|
|
54
|
-
else
|
|
55
|
-
results[action] = object.send(action)
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
results.values.all?
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|
|
@@ -1,51 +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
|
-
TRANSIENT_VARIABLES = StateMachines::InstanceMethods::TRANSIENT_VARIABLES
|
|
9
|
-
|
|
10
|
-
MUTEX_CREATION_LOCK = Thread::Mutex.new
|
|
11
|
-
|
|
12
|
-
# Gets or creates a reentrant mutex for thread-safe state operations on an object
|
|
13
|
-
# Each object gets its own mutex to avoid global locking
|
|
14
|
-
# Uses Concurrent::ReentrantReadWriteLock for better performance
|
|
15
|
-
def state_machine_mutex
|
|
16
|
-
@_state_machine_mutex || MUTEX_CREATION_LOCK.synchronize do
|
|
17
|
-
@_state_machine_mutex ||= Concurrent::ReentrantReadWriteLock.new
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# Thread-safe version of state reading
|
|
22
|
-
# Ensures atomic read operations across concurrent threads
|
|
23
|
-
def read_state_safely(machine, attribute, ivar = false)
|
|
24
|
-
state_machine_mutex.with_read_lock do
|
|
25
|
-
machine.read(self, attribute, ivar)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# Thread-safe version of state writing
|
|
30
|
-
# Ensures atomic write operations across concurrent threads
|
|
31
|
-
def write_state_safely(machine, attribute, value, ivar = false)
|
|
32
|
-
state_machine_mutex.with_write_lock do
|
|
33
|
-
machine.write(self, attribute, value, ivar)
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# Handle marshalling by excluding the mutex (will be recreated when needed)
|
|
38
|
-
def marshal_dump
|
|
39
|
-
vars = instance_variables - TRANSIENT_VARIABLES
|
|
40
|
-
vars.map { |var| [var, instance_variable_get(var)] }
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# Restore marshalled object, mutex will be lazily recreated when needed
|
|
44
|
-
def marshal_load(data)
|
|
45
|
-
data.each do |var, value|
|
|
46
|
-
instance_variable_set(var, value)
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
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
|