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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdbf8052e6f7ea2c8b05a007f232c67d3a41f4183bd0b0686e2fd6843105185c
4
- data.tar.gz: 994775869a61cd7f5f55f63ce0e79b85566c7015395f33d4db3f92097532e182
3
+ metadata.gz: 3b1301e729cdaeef68048c873e588c6f9dedbe0700a7e8113635abaef9acc72e
4
+ data.tar.gz: ecb7a1ff168729164b711fb7897143d052780a96e31fb1e1328ed1af82b34061
5
5
  SHA512:
6
- metadata.gz: 8d5d0747286937d1b336d3e9628700afbd06c39d05d57123737733421d5b02889caa45884f6bc62c49a08b08c34fbb3816d4d10b4e3fd1684fcb3118afdb4d38
7
- data.tar.gz: 11018072c682344a1bb6c7592c54d2d79a78dc06f78b97018edbdb1a214a6d5d6d6b6a522224519b1503364cf529a91b5a4a7e15004b049de85769022df7b500
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
- # Lazily created state that is rebuilt on demand rather than marshalled
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StateMachines
4
- VERSION = '0.300.0'
4
+ VERSION = '0.301.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.300.0
4
+ version: 0.301.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih