原創|其它|編輯:郝浩|2009-02-09 11:11:30.000|閱讀 1704 次
概述:在程序開發中將一些參數配置文件放在外部YAML文件里,使應用程序代碼不會被污染,有利于提高開發效率、增強代碼的可讀性。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在程序開發中將一些參數配置文件放在外部YAML文件里,使應用程序代碼不會被污染,有利于提高開發效率、增強代碼的可讀性,相關Java代碼如下:
# config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
# application.rb
def authenticate
if APP_CONFIG['perform_authentication']
authenticate_or_request_with_http_basic do |username, password|
username == APP_CONFIG['username'] && password == APP_CONFIG['password']
end
end
end
# config/config.yml
development:
perform_authentication: false
test:
perform_authentication: false
production:
perform_authentication: true
username: admin
password: secret
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都科技