My solution is the following one:
I will pick figaro because of the following two reasons.
- figaro has a
require_keys
method to raise an error during initialization in case you didn't specify all neccessary configuration values.
- with figaro I keep more flexibilty to change the configuration implementation based on environment variables.
To keep the flexibility I will not use the Figaro.env proxy but pure ENV variables. This way I could change the implementation of environment variables more easily in the future without touching the codebase.
Additionally I benchmarked four solutions with the following results:
2.2.1 :016 > n = 50000
2.2.1 :017 > Benchmark.bm do |x|
2.2.1 :018 > x.report { n.times do ; ENV["mail_address"] ; end }
2.2.1 :019?> x.report { n.times do ; Rails.application.config_for(:app)["mail_address"]; end }
2.2.1 :020?> x.report { n.times do ; Figaro.env.mail_address ; end }
2.2.1 :021?> x.report { n.times do ; Rails.configuration.x.mail_address ; end }
2.2.1 :022?> end
user system total real
0.070000 0.010000 0.080000 ( 0.078491)
11.060000 1.260000 12.320000 ( 12.497704)
5.600000 0.070000 5.670000 ( 5.758400)
0.050000 0.000000 0.050000 ( 0.056211)
I made the following findings:
- Using environment variables directly is way faster than using the
Figaro.env
proxy.
- Using the
Figaro.env
proxy takes only have the time of using rails
config_for
- Using the Rails configuration namespace
x
is the fastest of them
all.
So my best solution would be:
Use figaro gem to set environment variables according to your environment.
Get them via ENV['your_key']
.
Use them in an initializer via Rails configuration namespace x
:
e.g.
# config/application.yml
custom_app_id: "2954"
custom_key: "7381a978f7dd7f9a1117"
custom_secret: "abdc3b896a0ffb85d373"
and
# config/initializer/custom_config.rb
App::Application.configure do
config.x.custom_app_id = ENV['custom_app_id']
config.x.custom_key = ENV['custom_key']
config.x.custom_secret = ENV['custom_secret']
end
In case you need dynamic values you could use Rails config_for, because you could put ERB inside the yml files.
Update
I did more benchmarking with the following results:
2.2.2 :015 > Benchmark.bm do |x|
2.2.2 :016 > x.report { n.times do ; ENV["logo_file"] ; end }
2.2.2 :017?> x.report { n.times do ; ENV["logo_file_login"] ; end }
2.2.2 :018?> x.report { n.times do ; ENV["company_name"] ; end }
2.2.2 :019?> x.report { n.times do ; Rails.configuration.x.logo_file ; end }
2.2.2 :020?> x.report { n.times do ; Rails.configuration.x.logo_file_login ; end }
2.2.2 :021?> x.report { n.times do ; Rails.configuration.x.company_name ; end }
2.2.2 :022?> end
user system total real
3.430000 0.030000 3.460000 ( 3.503262)
3.570000 0.030000 3.600000 ( 3.642426)
5.020000 0.040000 5.060000 ( 5.133344)
4.380000 0.040000 4.420000 ( 4.508829)
4.390000 0.030000 4.420000 ( 4.470256)
4.360000 0.040000 4.400000 ( 4.442839)
It seems to me, that there is not much difference between using the ENV variables and using the Rails configuration namespace x
. Therefore I will skip the step with the additional configuration file and will use plain ENV variables where I need the configuration values.