demo_mode 3.7.1 → 3.8.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: 268e07b92704ae904b434f1c2865a8a21ece4adfc51775339aa2f5243a7e4624
4
- data.tar.gz: fe8974ba779ad030bf120811158e19f767c736c3c4faecf1c59a40dd019c7298
3
+ metadata.gz: b39607da6e06571e9a6b0d57b9efa1ab59f4109836e5c87776892fa30e239f95
4
+ data.tar.gz: 86c5e89834dff8123aa4b22f15d883d63eaa3209fc6d3236a261d625f98de2b6
5
5
  SHA512:
6
- metadata.gz: f0da8528e1c63ea16b3ca1f9c1e3a6fb1731ea4e5e406bb93e5512422e311139a4e27c1fe6fc38ec45dc9b9be10223625ad15455b73765025d31480a4b8f0fe4
7
- data.tar.gz: 75382c2d121e8308a9155280320ce94e3a7a04e0021161c3145a78ec6b6f8ec31815c15562197031cc55a757791cde01be774d4bc1a8a2312853dd6efa8d1bad
6
+ metadata.gz: 31b38ae765035a33cd107ea2340ce33c5875880102248c8bf269b55244aee007ca960c54c3ec79edd6a4a8b6e862811297b5ce4cd2c77c54bca07b620ad8ce41
7
+ data.tar.gz: f016a43e1afa88f64119ca71b548cd0a4c7bb4d07c5d2fb2bc57830508f26fcb6138fc13e4c7dddf9a3383a402457ed1cadc870b14e6b0055696ae91fb064290
@@ -4,12 +4,19 @@ module DemoMode
4
4
  class AccountGenerationJob < DemoMode.base_job_name.constantize
5
5
  def perform(session, **options)
6
6
  session.with_lock do
7
+ session.update!(status: 'processing') if session.failed?
7
8
  persona = session.persona
8
9
  raise "Unknown persona: #{session.persona_name}" if persona.blank?
9
10
 
10
11
  signinable = persona.generate!(variant: session.variant, password: session.signinable_password, options: options)
12
+ session.update!(signinable: signinable, persona_checksum: persona.file_checksum)
13
+
14
+ if session.claimed_at?
15
+ persona.effective_at_claim_callback(session.variant)&.call(signinable)
16
+ end
17
+
11
18
  new_status = session.claimed_at? ? 'in_use' : 'available'
12
- session.update!(signinable: signinable, status: new_status, persona_checksum: persona.file_checksum)
19
+ session.update!(status: new_status)
13
20
  end
14
21
  rescue StandardError => e
15
22
  session.update!(status: 'failed')
@@ -11,10 +11,10 @@ module DemoMode
11
11
  attr_accessor :pool_session
12
12
 
13
13
  steady_state :status do
14
- state 'processing', default: true
14
+ state 'processing', default: true, from: 'failed'
15
15
  state 'available', from: 'processing'
16
16
  state 'in_use', from: %w(processing available)
17
- state 'failed', from: 'processing'
17
+ state 'failed', from: %w(processing available)
18
18
  end
19
19
 
20
20
  scope :unclaimed, -> { where(claimed_at: nil) }
@@ -56,13 +56,15 @@ module DemoMode
56
56
  end
57
57
 
58
58
  def self.claim_for(persona_name:, variant: DEFAULT_VARIANT, **generation_opts)
59
+ persona = DemoMode.personas.find { |p| p.name.to_s == persona_name.to_s && p.variants.key?(variant) }
59
60
  pool_hit = false
60
61
  session = transaction do
61
62
  existing = available_for(persona_name, variant).lock.first
62
63
  pool_hit = existing.present?
63
- (existing || new(persona_name: persona_name, variant: variant)).tap do |s|
64
- s.claim!
65
- AccountGenerationJob.perform_later(s, **generation_opts) if s.signinable.blank?
64
+ if existing
65
+ claim_pool_session(existing, persona, variant)
66
+ else
67
+ new_claimed_session(persona_name, variant, generation_opts)
66
68
  end
67
69
  end
68
70
  ActiveSupport::Notifications.instrument('demo_mode.session.claimed',
@@ -70,6 +72,28 @@ module DemoMode
70
72
  session
71
73
  end
72
74
 
75
+ class << self
76
+ private
77
+
78
+ def claim_pool_session(session, persona, variant)
79
+ transaction(requires_new: true) do
80
+ persona&.effective_at_claim_callback(variant)&.call(session.signinable)
81
+ session.claim!
82
+ rescue StandardError
83
+ raise ActiveRecord::Rollback
84
+ end
85
+ session.update!(status: 'failed') unless session.claimed_at?
86
+ session
87
+ end
88
+
89
+ def new_claimed_session(persona_name, variant, generation_opts)
90
+ new(persona_name: persona_name, variant: variant).tap do |s|
91
+ s.claim!
92
+ AccountGenerationJob.perform_later(s, **generation_opts) if s.signinable.blank?
93
+ end
94
+ end
95
+ end
96
+
73
97
  def claim!
74
98
  if new_record?
75
99
  self.claimed_at = Time.zone.now
@@ -97,6 +97,16 @@ module DemoMode
97
97
  @enabled_condition ? @enabled_condition.call : true
98
98
  end
99
99
 
100
+ def at_claim(&block)
101
+ @at_claim_callback = block
102
+ end
103
+
104
+ attr_reader :at_claim_callback
105
+
106
+ def effective_at_claim_callback(variant_name)
107
+ variants[variant_name]&.at_claim_callback || @at_claim_callback
108
+ end
109
+
100
110
  def callout(callout = true) # rubocop:disable Style/OptionalBooleanParameter
101
111
  @callout = callout
102
112
  end
@@ -154,10 +164,15 @@ module DemoMode
154
164
  @enabled_condition ? @enabled_condition.call : true
155
165
  end
156
166
 
167
+ def at_claim(&block)
168
+ @at_claim_callback = block
169
+ end
170
+
157
171
  def title
158
172
  name.is_a?(Symbol) ? name.to_s.titleize : name.to_s
159
173
  end
160
174
 
175
+ attr_reader :at_claim_callback
161
176
  attr_reader :signinable_generator
162
177
  end
163
178
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DemoMode
4
- VERSION = '3.7.1'
4
+ VERSION = '3.8.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: demo_mode
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.1
4
+ version: 3.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Griffith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-17 00:00:00.000000000 Z
11
+ date: 2026-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack