LoginSignup
8

More than 5 years have passed since last update.

Settingslogicを使ってconfig.yml, config.local.yml運用してみる

Last updated at Posted at 2013-12-18

この記事は最終更新から1年以上経過しています。 気をつけてね。

Settingslogic は設定ファイルを一回ERbで処理してから使えるステキなRubygem。

CIやherokuなどはconfig.ymlを読んで環境変数を使いつつ、ローカル環境ではconfig.local.ymlで上書きしたいと思ったので試してみた。

ちなみに*.local.ymlを使うという運用方はtest-kitchenから拝借。

app.rb
require 'settingslogic'

class EnvSettings < Settingslogic
  source File.expand_path('../config.yml', __FILE__)
end

if File.exist?(File.expand_path('../config.local.yml', __FILE__))
  class EnvLocalSettings < Settingslogic
    source File.expand_path('../config.local.yml', __FILE__)
  end
  EnvSettings.merge!(EnvLocalSettings)
end

puts EnvSettings[:hoge]

多少冗長のような気もするが、既存のコードもifブロックを一個追加で同じ対応ができるしOK。

とりあえずconfig.ymlのみで実行。

config.yml
hoge: <%= ENV['HOGEHOGE'] %>
 $ HOGEHOGE=moge ruby ./app.rb 
moge

config.local.ymlを置いて実行。

config.local.yml
hoge: mogemoge
$ HOGEHOGE=moge ruby ./app.rb 
mogemoge

local側がENVを読んでもいいんだけど、使い勝手を考えるとこの順番かな。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8