Module: RactorRailsShim::Freezers::GlobalConstantFreezer
- Extended by:
- RoleDefaults
- Defined in:
- lib/ractor_rails_shim/roles/freezers.rb
Constant Summary
collapse
- TARGETS =
Mutable target list — downstream apps can register their own
DATE_FORMATS-style constants via add_target without monkey-patching.
Read at boot time (main Ractor only) before workers spawn.
%w[Time Date DateTime]
- CONSTANT_NAME =
:DATE_FORMATS
Class Method Summary
collapse
default_funnel, default_reassign_shareable_const, default_safe_const_get
Class Method Details
.add_target(class_name) ⇒ Object
182
183
184
|
# File 'lib/ractor_rails_shim/roles/freezers.rb', line 182
def self.add_target(class_name)
TARGETS << class_name unless TARGETS.include?(class_name)
end
|
.call ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/ractor_rails_shim/roles/freezers.rb', line 186
def self.call
constants = TARGETS.filter_map do |n|
mod = safe_const_get.call(n)
mod.is_a?(Module) ? [mod, CONSTANT_NAME] : nil
end
constants.each do |mod, name|
next unless mod.const_defined?(name, false)
val = mod.const_get(name, false)
next if Ractor.shareable?(val)
shareable = if val.is_a?(Hash)
h = {}
val.each { |k, v| h[k] = v if Ractor.shareable?(v) }
h.freeze
elsif val.is_a?(Array)
val.select { |v| Ractor.shareable?(v) }.freeze
else
val
end
begin
mod.send(:remove_const, name) if mod.const_defined?(name, false)
mod.const_set(name, shareable)
rescue StandardError => e
funnel.call("freeze global constant #{mod}::#{name}") { raise e }
end
end
true
end
|
158
159
160
161
|
# File 'lib/ractor_rails_shim/roles/freezers.rb', line 158
def self.configure(safe_const_get: nil, funnel: nil)
@safe_const_get = safe_const_get
@funnel = funnel
end
|
.funnel ⇒ Object
172
173
174
|
# File 'lib/ractor_rails_shim/roles/freezers.rb', line 172
def self.funnel
@funnel || default_funnel
end
|
.reset_configuration ⇒ Object
163
164
165
166
|
# File 'lib/ractor_rails_shim/roles/freezers.rb', line 163
def self.reset_configuration
@safe_const_get = nil
@funnel = nil
end
|
.safe_const_get ⇒ Object
168
169
170
|
# File 'lib/ractor_rails_shim/roles/freezers.rb', line 168
def self.safe_const_get
@safe_const_get || default_safe_const_get
end
|