0.0
No commit activity in last 3 years
No release in over 3 years
The loader of YAML configuration for each execution environments.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0

Runtime

~> 0.2
 Project Readme

konfig-yaml for Ruby

Gem Version document Build Status Code Climate Test Coverage

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 instance

require 'konfig-yaml'

config = KonfigYaml.new([name], [opts]);
  • name specifys the name of config/<name>.yml ( default app )
  • opts
    • :path config directory path resolved from the process current one ( default config )
    • :erb whether expand ERB or not ( default false )
    • :env Execution environment ( default RUBY_ENV value, RAILS_ENV value, RACK_ENV value, or development )

Load a configuration as Static class

require 'konfig-yaml'

KonfigYaml.setup do |config|
  config.const_name = 'Config' # default is 'Settings'
end

p Config.log.level

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 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 # 7080

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

MIT

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tilfin/konfig-yaml-rb.