This gem provides ruby applications with a global Settings
object loaded
from YAML files.
# Gemfile
gem 'settings', github: 'roomink/settings'
Settings
is a nested Hashie::Mash
populated with values from these files
in your app's root:
config/settings.yml
config/settings/#{Rails.env}.yml
config/settings.local.yml
config/settings/#{Rails.env}.local.yml
The files are loaded in that exact order so environment-specific settings overwrite global settings, and local settings override them both. Missing files are silently skipped.
When the Rails
constant isn't defined Settings
will look after
ENV['RAILS_ENV']
and ENV['RACK_ENV']
. If they're also not found
the default value of :development
will be used.
Then you can call methods corresponding to keys in your YAML files:
Settings.redis.host # => localhost
Settings.redis.db # => 0
Usually Settings
works with data for your current environment determined by
the process described above. But sometimes you may need your settings
for another environment. Here's how you can do that:
Settings.for(:test).redis.db # => 1
There is also a Settings.map
method which yields setting sets for
all configurations and returns them as a hash keyed by the environments:
Settings.map(&:redis)
# => { development: { host: 'localhost', port: 6379, db: 0 }, test: ... }
Settings are automatically filled with data from YAML files on the first call and cached for subsequent calls (this applies to settings for all environments). If you need to reload this data without restarting the app you can do this:
Settings.reload!
This call just flushes the cache - the files will be loaded next time you access some value.
By default settings are loaded for development
, test
and production
environments. If you need to load settings for some custom environment you need
to configure settings
to use it:
Settings::Utils.environments << :staging
Important: this must be done before the first call to settings data! Changing the environments list after the data was loaded will have no effect (read the "Reloading data" section above for details on how the data is being loaded).
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request