From 2d632cb7499792806e2549518f6dc688f52e658f Mon Sep 17 00:00:00 2001 From: "ar.shestopal" Date: Tue, 21 Mar 2017 11:11:59 +0200 Subject: [PATCH] Change show-routes command to allow multiple grep options --- lib/pry-rails/commands/show_routes.rb | 28 +++++++++++++++++++++++++-- spec/show_routes_spec.rb | 12 ++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/pry-rails/commands/show_routes.rb b/lib/pry-rails/commands/show_routes.rb index 360947d..db2c9d1 100644 --- a/lib/pry-rails/commands/show_routes.rb +++ b/lib/pry-rails/commands/show_routes.rb @@ -8,7 +8,9 @@ class PryRails::ShowRoutes < Pry::ClassCommand BANNER def options(opt) - opt.on :G, "grep", "Filter output by regular expression", :argument => true + opt.on :G, "grep", "Filter output by regular expression", + :argument => true, + :as => Array end def process @@ -24,7 +26,29 @@ def process process_rails_3_0_and_3_1(all_routes) end - output.puts formatted.grep(Regexp.new(opts[:G] || ".")).join("\n") + output.puts grep_routes(formatted).join("\n") + end + + # This method allows multiple grep conditions, like a pipe operator in unix + # Input show-routes -G admin -G post + # Output admin_post GET /admin/posts/:id(.:format) admin/posts#show + + # Params + + # formatted + # [" Prefix Verb URI Pattern Controller#Action", + # " root GET / blog/posts#index", + # " post GET /post(.:format) blog/posts#post", + # " admin_post GET /admin/posts/:id(.:format) admin/posts#show + # ] + + def grep_routes(formatted) + return formatted unless opts[:G] + grep_opts = opts[:G] + + grep_opts.reduce(formatted) do |grep_opt| + formatted.grep(Regexp.new(grep_opt)) + end end # Cribbed from https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake diff --git a/spec/show_routes_spec.rb b/spec/show_routes_spec.rb index e1b2000..3ac30ea 100644 --- a/spec/show_routes_spec.rb +++ b/spec/show_routes_spec.rb @@ -12,4 +12,16 @@ output.must_match %r{^edit_pokemon GET /pokemon/edit} end + + it "should print a list of routes which include grep option" do + output = mock_pry('show-routes -G edit', 'exit-all') + + output.must_match %r{^edit_pokemon GET /pokemon/edit} + end + + it "should print a list of routes which include grep option" do + output = mock_pry('show-routes -G edit -G pokemon', 'exit-all') + + output.must_match %r{^edit_pokemon GET /pokemon/edit} + end end