Skip to content

Instantly share code, notes, and snippets.

@peterc
Created December 27, 2008 11:31
Show Gist options
  • Save peterc/40239 to your computer and use it in GitHub Desktop.
Save peterc/40239 to your computer and use it in GitHub Desktop.
Configuration loading library for Sinatra apps
# NAME: configurator
# VERSION: 1.0
# AUTHOR: Peter Cooper [ http://www.rubyinside.com/ github:peterc twitter:peterc ]
# DESCRIPTION: Sinatra library to load app-wide configurations
# COMPATIBILITY: All, in theory - tested on Hoboken
# LICENSE: Use for what you want
#
# INSTRUCTIONS:
# 1. Ensure _this_ file is lib/initializer.rb within your app's directory structure
# 2. Add require 'lib/configurator' to your Sinatra app
# 3. Place YAML configuration files in /config (e.g. general.yaml, development.yaml, etc.)
# 4. Use CONFIG[:whatever] from within your Sinatra app
# Establish base directory names
APP_ROOT = File.dirname(File.expand_path($0)) unless defined?(APP_ROOT)
CONFIG_DIR = File.join(APP_ROOT, "config") unless defined?(CONFIG_DIR)
# Only load configurations if there's a config directory in the first place..
if File.directory?(CONFIG_DIR)
# Load general configuration
CONFIG = YAML.load(File.read(File.join(CONFIG_DIR, "general.yaml"))) rescue {}
# Merge in configurations based on environments
[:development, :production, :test].each do |e|
configure(e) { CONFIG.merge!(YAML.load(File.read(File.join(CONFIG_DIR, "development.yaml")))) rescue {} }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment