Skip to content

Commit

Permalink
Merge pull request #92 from ar-shestopal/Alex-Shestopal-Show-Routes-M…
Browse files Browse the repository at this point in the history
…ultiple-Grep-options

Change show-routes command to allow multiple grep options
  • Loading branch information
rf- committed Mar 25, 2017
2 parents f964db1 + 2d632cb commit bac26a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/pry-rails/commands/show_routes.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/show_routes_spec.rb
Expand Up @@ -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

0 comments on commit bac26a5

Please sign in to comment.