config_yml

Build Status Code Climate

Simplify your app configuration.

Description

config_yml provides a very simple way of configure your ruby applications through yaml files.

Installation

If you are using Bundler, add the following code to your Gemfile:

gem "config_yml", :require => "configuration"

and run:

$ bundle install

If you are not using Bundler:

$ gem install config_yml

then in your Ruby code:

require "configuration"

Usage

Create any yaml file inside config/ directory and it will became a hash accessible through the class method with the same name of the file.

# config/redis.yml
password: foo
host: localhost
port: 6379
database: 1
if redis = Configuration.redis
  password = redis[:password] && ":#{redis[:password]}@"
  ENV["REDIS_URL"] = %(redis://#{password}#{redis[:host]}:#{redis[:port]}/#{redis[:database]})
end

ENV["REDIS_URL"] # => "redis://:foo@localhost:6379/1"

You can also use the shorthand:

redis = Conf.redis # => { :password => "foo", :host => "localhost", :port => 6379, :database => 1 }

To see all available yaml files:

Configuration.files # => ["config/redis.yml"]

Obs.: File names must match Ruby method name restrictions. E.g. don't use config/my-app.yml, use config/my_app.yml instead.

Environment based:

# config/mysql.yml
development:
  adapter: mysql2
  encoding: utf8
  database: myapp_development
  username: foo
  password:
  socket: /tmp/mysql.sock

test:
  adapter: mysql2
  encoding: utf8
  database: myapp_test
  username: foo
  password:
  socket: /tmp/mysql.sock

Rack

ENV["RACK_ENV"] # => "development"
Configuration.mysql[:database] # => "myapp_development"

Rails

Rails.env # => "test"
Configuration.mysql[:database] # => "myapp_test"

Nested:

# config/matryoshka.yml
foo:
  bar:
    baz:
      array:
        - :red
        - :green
        - :blue
Configuration.matryoshka[:foo][:bar][:baz][:array] # => [:red, :green, :blue]

Manage yaml files

Avoid maintain .yml files in your repository since they very ofter cointain sensitive information. You can use .yml.sample or .yml.example files with fake data instead.

To transform sample files in yaml files, you can use the following task:

$ rake config_yml:generate

If you are not using Rails, first add this to your Rakefile:

require "configuration/tasks"

License

Copyright 2013 Vitor Kiyoshi Arimitsu - http://vitork.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.