Code Monkey home page Code Monkey logo

app_config's Introduction

Summary

Application level configuration.

Features

* simple YAML config files
* config files support ERB
* config files support inheritance
* access config information via convenient object member notation

Basic Usage

You simply write a configuration file in YAML. Notice you can use ERB.

config.yml

aws:
  access_key: 123ABC
  secret_key: ABC123
now: <%= Time.now %>
servers: [ {name: example1.com}, {name: example2.com} ]

Then somewhere in your code, you create a global constant from the config file. Then access the config data via object member notation.

code

::AppConfig = ApplicationConfiguration.new("config.yml")
AppConfig.aws.access_key  # => "123ABC"
AppConfig.aws.secret_key  # => "ABC123"
AppConfig.now             # => Tue May 05 21:55:15 -0500 2009
AppConfig.servers[0].name # => "example1.com"

Inheritance

You can have a second config file that is recursively merged with the first config file.

base.yml

app_name:  MyCoolApp
domain:  dev.mycoolapp.com

production.yml

domain:  www.mycoolapp.com

code

::AppConfig = ApplicationConfiguration.new("base.yml", "production.yml")
AppConfig.app_name # => "MyCoolApp"
AppConfig.domain   # => "www.mycoolapp.com"

Using in a Rails app

You just need to create an initializer that looks something like this.

require 'app_config'
::AppConfig = ApplicationConfiguration.new(RAILS_ROOT+"/config/app_config.yml",
                                           RAILS_ROOT+"/config/environments/#{RAILS_ENV}.yml")

If you installed this as a Rails plugin instead of a gem, that code is already run for you in the plugin’s init.rb.

Environments

Alternatively to splitting out your environments into separate files, you can just have a single file which defines the application configuration for all environments (much like how databases.yml works). Note if you do this, nested configurations will not be recursively merged. See example below.

app_config.yml

defaults: &defaults
  one: won
  two: too
  nested:
    foo: foo
    bar: bar

development:
  <<: *defaults
  two: new
  nested:
    foo: bar

code

RAILS_ENV # => "development"
::AppConfig = ApplicationConfiguration.new("app_config.yml")
AppConfig.use_environment!(RAILS_ENV)
AppConfig.one # => "won"
AppConfig.two # => "new"
AppConfig.nested.foo # => "bar"
AppConfig.nested.bar # raises NoMethodError because nested configurations are not recursively merged

Author

Christopher J. Bottaro

app_config's People

Contributors

cjbottaro avatar wanglian avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.