NxtConfig
This is a very simple tool to load YAML files into strict configuration structs, accessible through global constants. This is inspired by the famous config gem. The core features are:
- Load the content of a YAML file as a configuration object
- Strict attribute accessors
- Infinite amount of YAML files/configuration objects loadable (not just one)
- Configuration objects can be registered in a given namespace (especially useful when in use in ruby gems loaded by other applications) by calling
NxtConfig::load
in the namespace where the constant lives that its config struct is assigned to.
Installation
Add this line to your application's Gemfile:
gem 'nxt_config'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install nxt_config
Usage
You can create a configuration object using NxtConfig.load
from YAML files or directly from a hash.
If you are in a rails application, you can do this in an initializer (e.g. config/initializers/nxt_config.rb
).
module MyRailsApp
ExternalApiConfig = NxtConfig.load Rails.root.join('config', 'external_api.yml.erb')
end
Of course you can also load configuration structs everywhere else in the application.
Depending on where you assign it to a constant, you can have many configuration structs available via constants namespaced all over your application,
scoped to the context where you need them.
MyRailsApp::ExternalApiConfig.http.headers.user_agent
MyRailsApp::ExternalApiConfig.non_existent_key
MyRailsApp::ExternalApiConfig.fetch(:http, :headers, :user_agent)
MyRailsApp::ExternalApiConfig.fetch("http", "headers", "user_agent")
MyRailsApp::ExternalApiConfig.fetch(:http, :oh_no, :user_agent, &Proc.new { 'Hy!' })
MyRailsApp::ExternalApiConfig.http
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run bin/rspec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Run bin/guard
for guard to start watching for file changes and running the corresponding specs accordingly.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/nxt-insurance/nxt_config.
License
The gem is available as open source under the terms of the MIT License.