Environment Configurable

During our last big rails project, Bloomfire, we found ourselves integrating with all kinds of external services. Because of this we had a diverse set of environment dependent configuration variables. A consistent pattern started to arise where we would extract our configuration variables into a YAML file and then wrap the configuration using a small class wrapper. This eventually gave rise to Environment Configurable, a library that makes environment dependent configuration easy in rails.

Installation

  • Add config.gem “environment_configurable” to your environment.rb file
  • rake gems:install
  • rake gems:unpack

Usage

Code:

1
2
3
4
5
6
7
8
9
10
11
 class S3Helper
  include EnvironmentConfigurable
  configure_with "config/s3.yml"
  
  def self.connect!
     AWS::S3::Base.establish_connection!(
        :access_key_id     => config.access_key_id,
        :secret_access_key => config.secret_access_key
      )
  end
end

YAML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
production: &DEFAULTS
  access_key_id: ACCESS_KEY_ID
  secret_access_key: SECRET_ACCESS_KEY
  bucket: the-prod-bucket

staging:
  <<: *DEFAULTS
  bucket: the-staging-bucket

development:
  <<: *DEFAULTS
  bucket: the-development-bucket

test:
  <<: *DEFAULTS
  bucket: the-test-bucket
Filed in: Technologies, Tools


Leave a Reply

  

  

  

Stay Connected