Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add internal middleware stack to Dispatcher
config.middleware.use Rack::Cache
- Loading branch information
Showing
5 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module ActionController | ||
class MiddlewareStack < Array | ||
class Middleware | ||
attr_reader :klass, :args, :block | ||
|
||
def initialize(klass, *args, &block) | ||
@klass = klass.is_a?(Class) ? klass : klass.to_s.constantize | ||
@args = args | ||
@block = block | ||
end | ||
|
||
def ==(middleware) | ||
case middleware | ||
when Middleware | ||
klass == middleware.klass | ||
when Class | ||
klass == middleware | ||
else | ||
klass == middleware.to_s.constantize | ||
end | ||
end | ||
|
||
def inspect | ||
str = @klass.to_s | ||
@args.each { |arg| str += ", #{arg.inspect}" } | ||
str | ||
end | ||
|
||
def build(app) | ||
klass.new(app, *args, &block) | ||
end | ||
end | ||
|
||
def use(*args, &block) | ||
push(Middleware.new(*args, &block)) | ||
end | ||
|
||
def build(app) | ||
reverse.inject(app) { |a, e| e.build(a) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
desc 'Prints out your Rack middleware stack' | ||
task :middleware => :environment do | ||
ActionController::Dispatcher.middleware.each do |middleware| | ||
puts "use #{middleware.inspect}" | ||
end | ||
puts "run ActionController::Dispatcher.new" | ||
end |
06ed8e4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s funny that so many people are saying that you’re doing the “embrace & extend” thing with Rack, yet nobody noticed this commit (event though it was featured prominently in a recent “This week on Edge Rails” post" ).