state_machines 0.300.0 → 0.301.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/lib/state_machines/async_mode/thread_safe_state.rb +1 -2
- data/lib/state_machines/extensions.rb +10 -1
- data/lib/state_machines/machine/class_methods.rb +15 -3
- data/lib/state_machines/machine/configuration.rb +1 -0
- data/lib/state_machines/machine.rb +8 -0
- data/lib/state_machines/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3b1301e729cdaeef68048c873e588c6f9dedbe0700a7e8113635abaef9acc72e
|
|
4
|
+
data.tar.gz: ecb7a1ff168729164b711fb7897143d052780a96e31fb1e1328ed1af82b34061
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dc66215fe339e29b063c19dcadb4c8e682a6fc101e950e82721ebe433759adb4609193c30985303334cf114b03e7ae26beeeb61083b9561a782682be75db7e04
|
|
7
|
+
data.tar.gz: 72df6b1193ec0d46357499404a6925a9bede47dbfc0e95c3559284c03a4e8b0af24b6be4d0d636d5a04075d6b325266cf4d56480d196a49758b67096f0ca835b
|
|
@@ -5,8 +5,7 @@ module StateMachines
|
|
|
5
5
|
# Thread-safe state operations for async-enabled state machines
|
|
6
6
|
# Uses concurrent-ruby for enterprise-grade thread safety
|
|
7
7
|
module ThreadSafeState
|
|
8
|
-
|
|
9
|
-
TRANSIENT_VARIABLES = [:@_state_machine_mutex, StateMachines::TransitionClaim::CLAIMS_VARIABLE].freeze
|
|
8
|
+
TRANSIENT_VARIABLES = StateMachines::InstanceMethods::TRANSIENT_VARIABLES
|
|
10
9
|
|
|
11
10
|
MUTEX_CREATION_LOCK = Thread::Mutex.new
|
|
12
11
|
|
|
@@ -22,6 +22,10 @@ module StateMachines
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
module InstanceMethods
|
|
25
|
+
# Per-object concurrency state, created on demand: the lock and the
|
|
26
|
+
# transition claims. Never copied nor marshalled.
|
|
27
|
+
TRANSIENT_VARIABLES = %i[@_state_machine_mutex @_state_machine_transition_claims].freeze
|
|
28
|
+
|
|
25
29
|
# Runs one or more events in parallel. All events will run through the
|
|
26
30
|
# following steps:
|
|
27
31
|
# * Before callbacks
|
|
@@ -142,7 +146,9 @@ module StateMachines
|
|
|
142
146
|
end
|
|
143
147
|
|
|
144
148
|
# A copy (+dup+ / +clone+) has made no attempt to fire an event, so it
|
|
145
|
-
# starts with no refusal reasons instead of sharing the original's.
|
|
149
|
+
# starts with no refusal reasons instead of sharing the original's. It
|
|
150
|
+
# also gets its own lock and transition claims (created on demand)
|
|
151
|
+
# instead of sharing the original's.
|
|
146
152
|
#
|
|
147
153
|
# A class defining its own +initialize_copy+ must call +super+.
|
|
148
154
|
#
|
|
@@ -152,6 +158,9 @@ module StateMachines
|
|
|
152
158
|
def initialize_copy(orig)
|
|
153
159
|
super
|
|
154
160
|
StateMachines::Event.clear_refusals(self)
|
|
161
|
+
TRANSIENT_VARIABLES.each do |ivar|
|
|
162
|
+
remove_instance_variable(ivar) if instance_variable_defined?(ivar)
|
|
163
|
+
end
|
|
155
164
|
end
|
|
156
165
|
|
|
157
166
|
protected
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
module StateMachines
|
|
4
4
|
class Machine
|
|
5
5
|
module ClassMethods
|
|
6
|
+
# Options a reopened machine accepts; the others only apply when it is
|
|
7
|
+
# created.
|
|
8
|
+
REOPEN_OPTIONS = %i[initial async].freeze
|
|
9
|
+
|
|
6
10
|
# Attempts to find or create a state machine for the given class. For
|
|
7
11
|
# example,
|
|
8
12
|
#
|
|
@@ -14,6 +18,9 @@ module StateMachines
|
|
|
14
18
|
# If a machine of the given name already exists in one of the class's
|
|
15
19
|
# superclasses, then a copy of that machine will be created and stored
|
|
16
20
|
# in the new owner class (the original will remain unchanged).
|
|
21
|
+
#
|
|
22
|
+
# Reopening an existing machine applies +:initial+ and +:async+;
|
|
23
|
+
# any other option raises ArgumentError instead of being ignored.
|
|
17
24
|
def find_or_create(owner_class, *args, &)
|
|
18
25
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
19
26
|
name = args.first || :state
|
|
@@ -24,15 +31,20 @@ module StateMachines
|
|
|
24
31
|
owner_class.state_machines.values.first))) || nil
|
|
25
32
|
|
|
26
33
|
if machine
|
|
34
|
+
unsupported = options.keys - REOPEN_OPTIONS
|
|
35
|
+
unless unsupported.empty?
|
|
36
|
+
raise ArgumentError, "Cannot change #{unsupported.map(&:inspect).join(', ')} of existing state machine " \
|
|
37
|
+
"#{name.inspect} on #{owner_class}; reopening it accepts only :initial and :async"
|
|
38
|
+
end
|
|
39
|
+
|
|
27
40
|
# Only create a new copy if changes are being made to the machine in
|
|
28
41
|
# a subclass
|
|
29
42
|
if machine.owner_class != owner_class && (options.any? || block_given?)
|
|
30
43
|
machine = machine.clone
|
|
31
|
-
machine.initial_state = options[:initial] if options.include?(:initial)
|
|
32
44
|
machine.owner_class = owner_class
|
|
33
|
-
# Configure async mode if requested in options
|
|
34
|
-
machine.configure_async_mode!(options[:async]) if options.include?(:async)
|
|
35
45
|
end
|
|
46
|
+
machine.initial_state = options[:initial] if options.include?(:initial)
|
|
47
|
+
machine.configure_async_mode!(options[:async]) if options.include?(:async)
|
|
36
48
|
|
|
37
49
|
# Evaluate DSL
|
|
38
50
|
machine.instance_eval(&) if block_given?
|
|
@@ -40,6 +40,7 @@ module StateMachines
|
|
|
40
40
|
@initialize_state = options[:initialize]
|
|
41
41
|
@action_hook_defined = false
|
|
42
42
|
@async_requested = options[:async]
|
|
43
|
+
@integration_options = @integration ? options.slice(*@integration.integration_options).freeze : {}.freeze
|
|
43
44
|
|
|
44
45
|
self.owner_class = owner_class
|
|
45
46
|
|
|
@@ -476,6 +476,14 @@ module StateMachines
|
|
|
476
476
|
# Whether the machine will use transactions when firing events
|
|
477
477
|
attr_reader :use_transactions
|
|
478
478
|
|
|
479
|
+
# The options declared by the machine's integration (see
|
|
480
|
+
# StateMachines::Integrations::Base::ClassMethods#integration_options)
|
|
481
|
+
# that were passed when creating the machine. Available from
|
|
482
|
+
# +owner_class=+ onward.
|
|
483
|
+
#
|
|
484
|
+
# @return [Hash{Symbol => Object}] frozen option name => value
|
|
485
|
+
attr_reader :integration_options
|
|
486
|
+
|
|
479
487
|
# Defines a new helper method in an instance or class scope with the given
|
|
480
488
|
# name. If the method is already defined in the scope, then this will not
|
|
481
489
|
# override it.
|