Flexible application configuration in Ruby on Rails

Oct 14
2006 06:27 (Development, Ruby on Rails) · Русский (10,137 views)

In my current project in Ruby on Rails I need to store application specific configuration. There are several approaches I’ve found: AppConfig plugin, several methods described on HowtoAddYourOwnConfigInfo wiki page, but neither one looks “config-like”. Me with my friend, Alexey Kovyrin, discovered all of them and decided to use YAML-file. Ideal configuration, I think, file looks like following:

common:
  support_email: admin@myhost.com
  root_url: myhost.com
  photos_max_number: 6

production:
  email_exceptions: true

development:
  root_url: localhost:3000
  photos_max_number: 10

In this example you can see three sections: common will be used as base configuration for all environments, production and development - environment specific options. Possible sections are production, development, and testing, or any other custom environment name. I’ve placed this file in config/config.yml and added following code to config/environment.rb:

require 'ostruct'
require 'yaml'

config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
env_config = config.send(RAILS_ENV)
config.common.update(env_config) unless env_config.nil?
::AppConfig = OpenStruct.new(config.common)

Now I’m able to use constructions like AppConfig.support_email and AppConfig.root_url. Looks like I’ve kept all my configs as DRY as possible :-)

Zend Framework: Thoughts about Zend_Config

Mar 10
2006 10:48 (Development, PHP) · Русский (12,252 views)

In Zend Framework’s mailing list discussion about Zend_Config class is in full swing. I have my own ideas about this class and I will try to explain their here. I need something that can load and save configuration into different storages (for example, XML, database or plain text like INI-files), it’s necessary to have ability to change every parameter of storage (for example, file name, database tables or even database structure), it will be able if I can extend storage system with my own storage strategies.

Read the rest of entry »

 
Copyright © 2005 - 2008, Dmytro Shteflyuk