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,user
is 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
require 'konfig-yaml'
config = KonfigYaml.new([name], [opts]);
name
specifys the name ofconfig/<name>.yml
( defaultapp
)opts
:path
config directory path resolved from the process current one ( defaultconfig
):env
Execution environment ( default RUBY_ENV value, RAILS_ENV value, RACK_ENV value, ordevelopment
):use_cache
whether using cache ( defaulttrue
)
Access the values
by accessor method
port = config.port
log_lv = config.log.level
by symbol key
port = config[:port]
log_lv = config.log[:level]
by string key
port = config['port']
log_lv = config['log'].level
Dynamically change settings
Support only symbol or string
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 # 7080
Clear caches
For testing purpose
KonfigYaml.clear
Example
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.