Skip to content

Instantly share code, notes, and snippets.

@masone
Created August 8, 2012 11:36
Show Gist options
  • Save masone/3294426 to your computer and use it in GitHub Desktop.
Save masone/3294426 to your computer and use it in GitHub Desktop.
Global settings object with default and environment specific values
# Loads the file config/settings/shared.yml and merges it with config/settings/ENV['SILP_CLOUD'].yml.
# If ENV['SILP_CLOUD'] is not set, local.yml will be loaded as fallback.
class Settings
include Singleton
def self.init!(silp_cloud, rails_env)
rails_root = File.join(File.dirname(__FILE__), '..')
# Load shared configuration
shared_file = "#{rails_root}/config/settings/shared.yml"
shared = _yaml2hash(shared_file)
# Loading environment depending configuration
environment_file = "#{rails_root}/config/settings/#{silp_cloud || 'local' }.yml"
puts "### loading settings: #{environment_file} Rails Env: #{rails_env} ###"
environment = _yaml2hash(environment_file)
# Merge shared and environment depending config
merged = environment[rails_env].merge(shared)
# Load config hash
configatron.configure_from_hash(merged)
end
def self._yaml2hash(file)
YAML.load(ERB.new(File.read(file)).result).to_hash
end
def self.method_missing(method_sym, *arguments, &block)
configatron.send(method_sym)
end
end
Settings.instance # instantiate Settings class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment