Similar to my tip #56 regarding environment variables. This method is becoming more popular for setting up application configurations for Ruby applications that depend on each other.

1) Create a ~/.client_name directory in your home directory. You can name it anything you want. I will choose .balboa_park/ for my example. Then create a globals.yml in this directory.

 
➜  .balboa_park  pwd
/Users/username/.balboa_park
➜  .balboa_park  ls
total 0
drwxr-xr-x    3 bweikulrich  staff   102B May  1 09:29 .
drwxr-xr-x+ 114 bweikulrich  staff   3.8K May  1 09:29 ..
-rw-r--r--    1 bweikulrich  staff     0B May  1 09:28 globals.yml

2) Now fill the YAML file with application names, ports and domain endpoints. My configuration is for managing a series of applications around a popular zoo and museum.

# ~/.balboa_path/globals.yml
users:
  public: 
    domain: "localhost:8011"
  internal: 
    domain: "localhost:8011"
zoo:
  web:
    hostname: "localhost"
    http_port: "8000"
    internal_hostname: "localhost:8000"
  domain: "localhost:8000"
  app:
    name: zoo
    port: 8000
  memcached:
  - 127.0.0.1:11211
  mail:
    method: sendmail
    sendmail:
      location: "/usr/sbin/sendmail"
    smtp:
      hostname: localhost
      port: 25
museum:
  web:
    hostname: "localhost"
    http_port: "9988"
    internal_hostname: "localhost:9988"
  domain: "localhost:9988"

3) Setup the YAML parser in your Rails config/application.rb file.

# config/application.rb
 
$env_globals = Hash.new
[ Rails.root.join('config', 'globals.yml'),
  "#{ENV['HOME']}/.balboa_park/globals.yml"
].each do |file| 
  $env_globals.merge!(YAML.load_file(file)) if File.exists? file
end 
abort "Refusing to continue without a global configuration" if $env_globals.empty?

4) Setup a CONFIG hash to easily read the variables through your application.

# config/application.rb
 
CONFIG = {}
CONFIG[:musuem_domain] = $env_globals['musuem']['domain']
CONFIG[:zoo_domain] = $env_globals['zoo']['domain']
CONFIG[:users_domain] = $env_globals['users']['domain']

Now throughout your application’s environments (ie… staging, production, development, test) you can now easily manage all your configuration from one file.

:)

Leave a Reply

Your email address will not be published. Required fields are marked *