mirror of
https://github.com/pry/pry-rails.git
synced 2022-11-09 12:36:03 -05:00
Merge remote-tracking branch 'sarenji/master'
Conflicts: lib/pry-rails/commands.rb
This commit is contained in:
commit
7c1065d56d
1 changed files with 65 additions and 0 deletions
|
@ -80,5 +80,70 @@ module PryRails
|
|||
output.puts models
|
||||
end
|
||||
end
|
||||
|
||||
create_command "show-middleware" do
|
||||
group "Rails"
|
||||
|
||||
def options(opt)
|
||||
opt.banner unindent <<-USAGE
|
||||
Usage: show-middleware [-G]
|
||||
|
||||
show-middleware shows the Rails app's middleware.
|
||||
|
||||
If this pry REPL is attached to a Rails server, the entire middleware
|
||||
stack is displayed. Otherwise, only the middleware Rails knows about is
|
||||
printed.
|
||||
USAGE
|
||||
|
||||
opt.on :G, "grep", "Filter output by regular expression", :argument => true
|
||||
end
|
||||
|
||||
def process
|
||||
# assumes there is only one Rack::Server instance
|
||||
server = nil
|
||||
ObjectSpace.each_object(Rack::Server) do |object|
|
||||
server = object
|
||||
end
|
||||
|
||||
middlewares = []
|
||||
|
||||
if server
|
||||
stack = server.instance_variable_get("@wrapped_app")
|
||||
middlewares << stack.class.to_s
|
||||
|
||||
while stack.instance_variable_defined?("@app") do
|
||||
stack = stack.instance_variable_get("@app")
|
||||
# Rails 3.0 uses the Application class rather than the application
|
||||
# instance itself, so we grab the instance.
|
||||
stack = Rails.application if stack == Rails.application.class
|
||||
middlewares << stack.class.to_s if stack != Rails.application
|
||||
end
|
||||
else
|
||||
middleware_names = Rails.application.middleware.map do |middleware|
|
||||
# After Rails 3.0, the middleware are wrapped in a special class
|
||||
# that responds to #name.
|
||||
if middleware.respond_to?(:name)
|
||||
middleware.name
|
||||
else
|
||||
middleware.inspect
|
||||
end
|
||||
end
|
||||
middlewares.concat middleware_names
|
||||
end
|
||||
middlewares << Rails.application.class.to_s
|
||||
print_middleware middlewares.grep(Regexp.new(opts[:G] || "."))
|
||||
end
|
||||
|
||||
def print_middleware(middlewares)
|
||||
middlewares.each do |middleware|
|
||||
string = if middleware == Rails.application.class.to_s
|
||||
"run #{middleware}.routes"
|
||||
else
|
||||
"use #{middleware}"
|
||||
end
|
||||
output.puts string
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue