Created
August 20, 2012 16:54
-
-
Save tekei/3405758 to your computer and use it in GitHub Desktop.
[Sinatra::ContribのConfigFile] RACK_ENVに依存しないパラメータを記述可能にする
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
# Sinatra::ContribのConfigFileに対して、RACK_ENV(settings.environment) | |
# に依存しないパラメータを記述可能にする | |
# | |
# 例: | |
# | |
# 通常ならば、以下の通り共通的な値「settings.aaa」も複数記述する必要がある | |
# ------ yaml の内容(before) ---------------------------- | |
#development: | |
# database: AAA | |
# aaa: info | |
#test: | |
# database: BBB | |
# aaa: info | |
#(以下省略) | |
# ------------------------------------------------------- | |
# | |
# これを以下の通り記載できる | |
# | |
# ------ yaml の内容(after) ---------------------------- | |
#development: | |
# database: AAA | |
#test: | |
# database: BBB | |
# | |
#aaa: info | |
#(以下省略) | |
# ------------------------------------------------------- | |
module Sinatra | |
module ConfigFile | |
def config_for_env(hash) | |
if hash.respond_to? :keys | |
k = hash.keys.stringify_keys | |
if environments.all? {|e| k.include? e } | |
c_hash = hash[environment.to_s] || hash[environment.to_sym] | |
(k - environments).each { |p| c_hash[p] = hash[p] } | |
hash = c_hash | |
end | |
end | |
if hash.respond_to? :to_hash | |
indifferent_hash = Hash.new {|hash,key| hash[key.to_s] if Symbol === key } | |
indifferent_hash.merge hash.to_hash | |
else | |
hash | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment