We're hiring!

We're actively seeking developers & designers for our new Detroit location. Learn more

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
This entry was posted in Design & Development, Tools and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">