Module: RactorRailsShim::StorageStrategy::Thread

Defined in:
lib/ractor_rails_shim/foundation/storage_strategy.rb

Overview

Thread-mode strategy. Mirrors the Ractor module's two entry points:

* `lookup` / `store`        — exact-key passthrough on
                           CLASS_ATTR_VALUES (the Thread backend),
                           for IESAccessor.
* `lookup_by_attr` / `store_by_attr` — per-receiver class_attribute
                           semantics (ancestor-walk on
                           CLASS_ATTR_VALUES) so each class keeps its
                           own value.

Class Method Summary collapse

Class Method Details

.lookup(owner, key, missing_default) ⇒ Object

Exact-key passthrough on CLASS_ATTR_VALUES (the Thread backend).



207
208
209
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 207

def lookup(owner, key, missing_default)
  RactorRailsShim::Registry.class_attr_values[key]
end

.lookup_by_attr(owner, attr_sym, missing_default) ⇒ Object

Per-receiver class_attribute lookup: derive the receiver's storage key (memoized via StorageStrategy.ca_key) and walk ancestors on CLASS_ATTR_VALUES.



219
220
221
222
223
224
225
226
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 219

def lookup_by_attr(owner, attr_sym, missing_default)
  klass = owner.is_a?(Module) ? owner : owner.class
  klass.ancestors.each do |anc|
    k = RactorRailsShim::StorageStrategy.ca_key(anc, attr_sym)
    return RactorRailsShim::Registry.class_attr_values[k] if RactorRailsShim::Registry.class_attr_values.key?(k)
  end
  missing_default
end

.lookup_resolved(owner, attr_sym, missing_default, resolved_key) ⇒ Object

Zero-allocation hot reader (see Ractor#lookup_resolved). Uses the Thread backend (CLASS_ATTR_VALUES) for the resolved cache.



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 236

def lookup_resolved(owner, attr_sym, missing_default, resolved_key)
  klass = owner.is_a?(Module) ? owner : owner.class
  oid = klass.object_id
  cache = RactorRailsShim::Registry.class_attr_values[resolved_key]
  return cache[oid] if cache && cache.key?(oid)
  v = lookup_by_attr(owner, attr_sym, missing_default)
  cache = RactorRailsShim::Registry.class_attr_values[resolved_key]
  cache ||= (RactorRailsShim::Registry.class_attr_values[resolved_key] = {})
  cache[oid] = v
  v
end

.replay_callbacks!(context, kind, &block) ⇒ Object

Thread mode always replays captured callbacks. Delegates to the shared replay_callbacks! implementation on the Ractor strategy (same logic, different trigger).



272
273
274
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 272

def replay_callbacks!(context, kind, &block)
  RactorRailsShim::StorageStrategy::Ractor.replay_callbacks!(context, kind, &block)
end

.replay_callbacks?(callbacks) ⇒ Boolean

Thread mode: the eager-load class_attribute leak corrupts __callbacks, so ALWAYS replay the captured symbolic filters (ignoring __callbacks entirely). The "on-empty" gate is unreachable (the "always" path runs first); provided for contract parity.

Returns:

  • (Boolean)


265
266
267
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 265

def replay_callbacks?(callbacks)
  false
end

.store(owner, key, value) ⇒ Object



211
212
213
214
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 211

def store(owner, key, value)
  RactorRailsShim::Registry.class_attr_values[key] = value
  value
end

.store_by_attr(owner, attr_sym, value) ⇒ Object



228
229
230
231
232
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 228

def store_by_attr(owner, attr_sym, value)
  klass = owner.is_a?(Module) ? owner : owner.class
  RactorRailsShim::Registry.class_attr_values[RactorRailsShim::StorageStrategy.ca_key(klass, attr_sym)] = value
  value
end

.store_resolved(owner, attr_sym, value, resolved_key) ⇒ Object

Zero-allocation hot writer (see Ractor#store_resolved).



249
250
251
252
253
254
255
256
257
258
# File 'lib/ractor_rails_shim/foundation/storage_strategy.rb', line 249

def store_resolved(owner, attr_sym, value, resolved_key)
  kkey = RactorRailsShim::StorageStrategy.ca_key(owner, attr_sym)
  RactorRailsShim::Registry.class_attr_values[kkey] = value
  klass = owner.is_a?(Module) ? owner : owner.class
  oid = klass.object_id
  cache = RactorRailsShim::Registry.class_attr_values[resolved_key]
  cache ||= (RactorRailsShim::Registry.class_attr_values[resolved_key] = {})
  cache[oid] = value
  value
end