Created
June 12, 2012 09:02
-
-
Save jhjguxin/2916329 to your computer and use it in GitHub Desktop.
load_config_from_yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#In summary: | |
# config/initializers/load_config.rb | |
#APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV] | |
# application.rb | |
#if APP_CONFIG['perform_authentication'] | |
# Do stuff | |
#end | |
require 'delegate' | |
module BBTangCMS | |
class Config < SimpleDelegator | |
def initialize(file_name) | |
super(symbolize_keys( YAML.load_file(file_name)[Rails.env] || YAML.load_file(file_name))) | |
end | |
def [](*path) | |
path.inject(__getobj__()) {|config, item| | |
config[item] | |
} | |
end | |
#def author_open_ids | |
# [self[:author, :open_id]].flatten.map {|uri| URI.parse(uri)} | |
#end | |
def self.default | |
BBTangCMS::Config.new(default_location) | |
end | |
def self.default_location | |
"#{Rails.root}/config/bbtangcms.yml" | |
end | |
def self.sina | |
BBTangCMS::Config.new(sina_location) | |
end | |
def self.sina_location | |
"#{Rails.root}/config/oauth/sina.yml" | |
end | |
def self.qq | |
BBTangCMS::Config.new(qq_location) | |
end | |
def self.qq_location | |
"#{Rails.root}/config/oauth/qq.yml" | |
end | |
def self.tag | |
BBTangCMS::Config.new(tag_location) | |
end | |
def self.tag_location | |
"#{Rails.root}/config/tag.yml" | |
end | |
private | |
def symbolize_keys(hash) | |
hash.inject({}) do |options, (key, value)| | |
options[(key.to_sym rescue key) || key] = value.is_a?(Hash) ? symbolize_keys(value) : value | |
options | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment