mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Simplify middleware stack lazy compares using named const references
This commit is contained in:
parent
9d0d6f7d26
commit
509f3d7d2f
1 changed files with 5 additions and 13 deletions
|
@ -5,13 +5,13 @@ module ActionDispatch
|
||||||
class Middleware
|
class Middleware
|
||||||
attr_reader :args, :block
|
attr_reader :args, :block
|
||||||
|
|
||||||
def initialize(klass, *args, &block)
|
def initialize(klass_or_name, *args, &block)
|
||||||
@klass, @args, @block = klass, args, block
|
@ref = ActiveSupport::Dependencies::Reference.new(klass_or_name)
|
||||||
|
@args, @block = args, block
|
||||||
end
|
end
|
||||||
|
|
||||||
def klass
|
def klass
|
||||||
return @klass if @klass.respond_to?(:new)
|
@ref.get
|
||||||
@klass = ActiveSupport::Inflector.constantize(@klass.to_s)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(middleware)
|
def ==(middleware)
|
||||||
|
@ -21,11 +21,7 @@ module ActionDispatch
|
||||||
when Class
|
when Class
|
||||||
klass == middleware
|
klass == middleware
|
||||||
else
|
else
|
||||||
if lazy_compare?(@klass) && lazy_compare?(middleware)
|
normalize(@ref.name) == normalize(middleware)
|
||||||
normalize(@klass) == normalize(middleware)
|
|
||||||
else
|
|
||||||
klass.name == normalize(middleware.to_s)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,10 +35,6 @@ module ActionDispatch
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def lazy_compare?(object)
|
|
||||||
object.is_a?(String) || object.is_a?(Symbol)
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize(object)
|
def normalize(object)
|
||||||
object.to_s.strip.sub(/^::/, '')
|
object.to_s.strip.sub(/^::/, '')
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue