今回は以下の使い方を順に解説していきます。
- Hirb
- Pry
- show-models
- show-model
- show-routes
- find-route
- recognize-path
インストール
まずはgemをインストール。以下の3つを追加します。
group :development do gem 'pry-rails' gem 'hirb' gem 'hirb-unicode' end
pryをRailsコンソールで使用するには、pry-railsをインストールします。
gemをインストールすれば、コンソールを開いた時にpryで起動するようになります。
Hirb単体では日本語などマルチバイト文字が入った場所に出力結果が乱れてしまうのでhirb-unicodeを一緒にインストールします。
Hirb (データを表形式で表示)
ActiveRecordのモデルデータを表形式で表示します。
[1] pry(main)> Book.all Book Load (21.8ms) SELECT "books".* FROM "books" +----+--------+-------+-------------+--------------+ | id | name | price | created_at | updated_at | +----+--------+-------+-------------+--------------+ | 1 | Book 1 | 1000 | 2017-07-... | 2017-07-1... | | 2 | Book 2 | 2000 | 2017-07-... | 2017-07-1... | | 3 | Book 3 | 3000 | 2017-07-... | 2017-07-1... | | 4 | Book 4 | 4000 | 2017-07-... | 2017-07-1... | | 5 | Book 5 | 5000 | 2017-07-... | 2017-07-1... | +----+--------+-------+-------------+--------------+ 5 rows in set
selectでカラムを選択して表示することもできます。
[2] pry(main)> Book.all.select(:id, :name) Book Load (0.2ms) SELECT "books"."id", "books"."name" FROM "books" +----+--------+ | id | name | +----+--------+ | 1 | Book 1 | | 2 | Book 2 | | 3 | Book 3 | | 4 | Book 4 | | 5 | Book 5 | +----+--------+ 5 rows in set
pluckなどでArrayにしてしまうと表形式になりません。
[3] pry(main)> Book.all.pluck(:id, :name) (0.2ms) SELECT "books"."id", "books"."name" FROM "books" => [[1, "Book 1"], [2, "Book 2"], [3, "Book 3"], [4, "Book 4"], [5, "Book 5"]]
Pry(便利なユーティリティ)
Pryにはモデルやroutesの一覧などを表示する便利なコマンドがありますので、順に紹介します。
show-models
全モデルのモデルごとのカラムとリレーションを一覧で表示します。
[1] pry(main)> show-models Beer id: integer name: string type: string rating: integer ibu: integer abv: integer created_at: datetime updated_at: datetime belongs_to hacker Hacker id: integer social_ability: integer created_at: datetime updated_at: datetime has_many pokemons has_many beers Pokemon id: integer name: string caught: binary species: string abilities: string created_at: datetime updated_at: datetime belongs_to hacker has_many beers through hacker
--grep
オプションで絞り込むこともできます。
[1] pry(main)> show-models --grep Pokemon Pokemon id: integer name: string caught: binary species: string abilities: string created_at: datetime updated_at: datetime belongs_to hacker has_many beers through hacker
show-model
特定モデルを引数に取り、カラムのとリレーションを表示します。
[1] pry(main)> show-model Pokemon Pokemon id: integer name: string caught: binary species: string abilities: string created_at: datetime updated_at: datetime belongs_to hacker has_many beers through hacker
show-routes
rake routes
と同じ出力。
[1] pry(main)> show-routes pokemon POST /pokemon(.:format) pokemons#create new_pokemon GET /pokemon/new(.:format) pokemons#new edit_pokemon GET /pokemon/edit(.:format) pokemons#edit GET /pokemon(.:format) pokemons#show PUT /pokemon(.:format) pokemons#update DELETE /pokemon(.:format) pokemons#destroy beer POST /beer(.:format) beers#create new_beer GET /beer/new(.:format) beers#new edit_beer GET /beer/edit(.:format) beers#edit GET /beer(.:format) beers#show PUT /beer(.:format) beers#update DELETE /beer(.:format) beers#destroy
こちらも--grep
オプションで絞り込むこともできます。
[2] pry(main)> show-routes --grep beer beer POST /beer(.:format) beers#create new_beer GET /beer/new(.:format) beers#new edit_beer GET /beer/edit(.:format) beers#edit GET /beer(.:format) beers#show PUT /beer(.:format) beers#update DELETE /beer(.:format) beers#destroy
find-route
指定したコントローラ、アクションのRoutes一覧を表示します。
[1] pry(main)> find-route Book Routes for BooksController -- index GET /books(.:format) [books] create POST /books(.:format) new GET /books/new(.:format) [new_book] edit GET /books/:id/edit(.:format) [edit_book] show GET /books/:id(.:format) [book] update PATCH /books/:id(.:format) update PUT /books/:id(.:format) destroy DELETE /books/:id(.:format)
recognize-path
指定したパスにマッチするコントローラとアクションを表示します。
[1] pry(main)> recognize-path '/books/10' {:action=>"show", :controller=>"books", :id=>"10"}
まとめ
今回紹介した2つのgem HirbとPryを使えばデータ検索やスキーマの確認など容易に行うことができます。
Pryといえばbyebugのイメージでしたが、コンソールで使える便利なコマンドを知ることが出来ました。
Pryの使い方はまだまだありますので、次回はデバッグ等々での使い方を紹介します。