Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[Sinatra::ContribのConfigFile] RACK_ENVに依存しないパラメータを記述可能にする
# 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