Configa
Configa makes it easier to use multi environment YAML configs.
Inspired by prepor
Installation
Add this line to your application's Gemfile:
gem 'configa'
And then execute:
$ bundle
Or install it yourself as:
$ gem install configa
Usage
config_file_path = File.expand_path("../config/config.yml", __FILE__)
config = Configa.new(config_file_path)
config.mysql.host
config.development.mongodb.user
config.development.mongodb(:user, :password)
config.development.mongodb(:user, :password, hash: true)
config.production.root.password
config = Configa.new(config_file_path, env: :development)
config.mongodb.user
Why anyone needs configa?
You can specify in your config file many environments without duplication
mysql:
adapter: mysql
encoding: utf8
host: localhost
username: root
development:
mysql:
database: mysql_dev
test:
mysql:
database: mysql_test
production:
mysql:
username: admin
password: strongone
So, it will create mysql
"node" which will be shared between all of environments:
config.development.mysql.username
config.production.mysql.username
config.production.mysql(:username, :host)
Also you can share base templates between multiple nodes of one environment
mysql:
adapter: mysql
encoding: utf8
host: localhost
username: root
development:
databases:
users:
mysql:
database: users_dev
blogs:
mysql:
database: blogs_dev
config.development.databases.users.mysql.database
Sometimes configuration files grows
You can create development.yml
, staging.yml
or any other file, put it into the same folder as a base config file and Configa will automatically fetch and load it. Also you can use cascade templates. For example in following example you can define tarantool
namespace, wich will be inherited by tarantool
namespace in "development" env.
mysql:
adapter: mysql
encoding: utf8
host: localhost
username: root
tarantool:
host: localhost
port: 13013
type: :block
mysql:
database: my_database
tarantool:
type: :em
videos:
tarantool:
space: 1
users:
tarantool:
space: 2
config = Configa.new("config.yml")
config.development.mysql.database
config.development.mysql.username
config.videos.tarantool.space
config.videos.tarantool.type
All properties that defined higher in the tree will rewrite root properties:
database:
adapter: sqlite
user: root
development:
database:
db: dev_sqlite
staging:
database:
adapter: mysql
internal_servers:
database:
db: internal_db
external_servers:
database:
db: external_db
user: external_root
So you can see how for whole staging node we rewrite database adapter, then for each subnode we specify other properties.
Also you can define default environment:
dev = Configa.new(path, env: development)
all = Configa.new(path)
dev.mysql
all.development.mysql
dev.production.mysql
all.production.mysql
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
)
- Commit your changes (
git commit -am 'Added some feature'
)
- Push to the branch (
git push origin my-new-feature
)
- Create new Pull Request