konfig-yaml for Ruby
The loader of YAML configuration for each execution environments like settingslogic.
- Expand environment variables like bash (ex.
storage-${RUBY_ENV})- If an environment variable is not set, it is to be emtpy string.
- If
${DB_USER:-user}or${DB_USER:user}is defined,useris expanded unless DB_USER does not exists.
- Deep merge the environment settings and default settings (except array items)
- Support YAML Anchor
&something/ Reference<<: *something - Ruby version of konfig-yaml
Installation
Add this line to your application's Gemfile:
gem 'konfig-yaml'And then execute:
$ bundle
Or install it yourself as:
$ gem install konfig-yaml
Usage
Load a configuration instance
require 'konfig-yaml'
config = KonfigYaml.new([name], [opts]);-
namespecifys the name ofconfig/<name>.yml( defaultapp) -
opts-
:pathconfig directory path resolved from the process current one ( defaultconfig) -
:erbwhether expand ERB or not ( defaultfalse) -
:envExecution environment ( default RUBY_ENV value, RAILS_ENV value, RACK_ENV value, ordevelopment)
-
Load a configuration as Static class
require 'konfig-yaml'
KonfigYaml.setup do |config|
config.const_name = 'Config' # default is 'Settings'
end
p Config.log.levelAccess the values
by accessor method
port = config.port
log_lv = config.log.levelby symbol key
port = config[:port]
log_lv = config.log[:level]by string key
port = config['port']
log_lv = config['log'].levelDynamically change settings
Support only symbol or string key if adding new field
config[:port] = 80
config.log['level'] = 'fatal'
config[:backend] = { host: 'cms.example.com', "port" => 7080 }
p config.port # 80
p config.log.level # "fatal"
p config.backend.host # "cms.example.com"
p config.backend.port # 7080Example
Setup
config/app.yml
default:
port: 8080
log:
level: info
db:
name: ${BRAND:-normal}-app-${RUBY_ENV}
user: user
pass: pass
production:
port: 1080
log:
level: error
db:
pass: Password
main.rb
require 'konfig-yaml'
config = KonfigYaml.new
puts config.port
puts config.log.level
puts config.db.user
puts config[:db]['name']
puts config['db'].password
Run
In development
$ RUBY_ENV=development ruby main.rb
8080
info
normal-app-development
user
pass
In production
$ RUBY_ENV=production BRAND=awesome ruby main.rb
1080
error
awesome-app-production
user
Password
License
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/tilfin/konfig-yaml-rb.