2012年4月19日木曜日

Sinatraの環境切り替え

config.ymlといった定義ファイルをdevelopmentモードとproductionモードといったモードで切り替えを行う方法です。

http://www.sinatrarb.com/contrib/config_file.html


下記2行を追加すれば、railsのようにproduction/developmentの記載できます。
require "sinatra/config_file"
config_file 'path/to/config.yml
例えば、こんなconfig.ymlがあったとします。
production:
  server: sinatra_production
  port: 5555
development:
  server: sinatra_dev
  port: 5555
test:
  server: sinatra_test
  port: 5555
settings.serversettings.port でそれぞれのモードに応じた値が取得できます。
settingsはお決まりの用語です。

なお、上記のymlでも下記のymlでも同じ結果になります。
server:
  production: sinatra_production
  development: sinatra_dev
  test: sinatra_test

port: 5555

どちらの記載が良いのかは、定義ファイルの書く内容次第ですね。

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。