Replies: 3 comments 3 replies
-
A quick idea that comes into my mind is to instantiate an instance right where the class is defined: class MyConfig
# ...
require :x, :foo
end.new #=> here we try to initialize an instance right away |
Beta Was this translation helpful? Give feedback.
-
That syntax looks like it should work but was not letting me call # Eager instantiate configs to fail on startup if misconfigured
Dir["config/configs/*.rb"].each do |f|
require_dependency("#{Dir.pwd}/#{f}")
end
ApplicationConfig.subclasses.each(&:new) I'd really like to use the value assigned to |
Beta Was this translation helpful? Give feedback.
-
I think, We can try to incorporate something like this into a library to take into account all possible situations when we don't need to call this validation. For Rails application, I'd suggest putting this into a server start callback: # application.rb
class MyApp < Rails::Application
# ...
server do
ApplicationConfig.subclasses.each(&:new)
end
end |
Beta Was this translation helpful? Give feedback.
-
Ideally, i'd like my application to fail to start/deploy if required configuration is not in place. The
required
keyword andon_load
blocks have gotten me quite far, but only fail on instantiation or when the config is referenced. Most of the configs are referenced in initializers, but not in all cases so I had a couple slip by.Short of doing iterating and instantiating every class in
config/configs
, is there a better way to do this?Beta Was this translation helpful? Give feedback.
All reactions