Application preloader
spring
https://github.com/rails/spring
spork
https://github.com/sporkrb/spork
- 事前にバックグラウンドでライブラリをロードしておくことで、その待ち時間を短くする
- テストやサーバー起動時
- Process.forkを使っているのでWindows, JRuby環境はだめ
Install
gem 'spring', group: :development
bundle install
spring binstub --all
spring status
Add command
gem "spring-commands-rspec", group: :development
bundle install
spring stop
bundle exec spring binstub --all
rspec
# added command
# spring-commands-rspec
# spring-commands-cucumber
# spring-commands-spinach
# spring-commands-testunit
# spring-commands-teaspoon```
uninstall
bin/spring binstub --remove --all
# gem 'spring', group: :development
bundle install
計測
/usr/bin/time bin/rake about
/usr/bin/time bin/rspec spec/requests/user_pages_spec.rb
Rackとは
- RackはRubyでWebサーバーを立ち上げるためのインターフェースである
- 様々なミドルウェア、アプリケーションを組み合わせてWebサーバを立ち上げることができるrackupというコマンドを提供するライブラリ
rails(config.ru)
- Rails.applicationはRackアプリケーション仕様に準拠
- サーバと直接ではなくRackという抽象インターフェースを介してHTTP送受信処理を行う
- サーバの種類による違いは全てRackが吸収し、Rails側はどのサーバでも同じコードで記述できる
Rack::Server
- Rack::Server.start(ruckup)
- config.ruをRack::Builder DSLとして評価し、Rackアプリケーションを生成
- RACK_ENVに応じてRackアプリケーションにRackミドルウェアをラップする
- RackミドルウェアをラップしたRackアプリケーションをRackハンドラに渡す
Rack::Builder DSL
- rackup(rackup config)を叩くと指定したファイルをRack::Builder DSLとして読み込み、そのDSLによって設定されたWebサーバーが立ち上がる
Rackアプリケーション(RailsやSinatra)
- 引数を1つ取り3つの値を返すcallを呼び出すことができるオブジェクト
Rackミドルウェア(rack-のつくgem)
- Rackアプリケーションのcallの前後に行いたい処理を記述するクラス
- Rack::Builder DSL内で複数のRackミドルウェアをuseすることでミドルウェアスタックが作られる
Rackハンドラ(WEBrick, Thin, Puma)
- RackミドルウェアにラップされたRackアプリケーションと、portやhostなどのオプションを受け取って実際にWebサーバを立ち上げるクラス
- HTTPリクエストをパースしたりHTTPレスポンスを組み立てるの
Rails::Server
- opt_parserをオーバーライドしてrails s特有のオプションを定義
- デフォルトポートを3000
- RAILS_ENVに応じた処理が定義