Skip to content

Commit

Permalink
Simplify middleware stack lazy compares using named const references
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Jun 6, 2010
1 parent 9d0d6f7 commit 509f3d7
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions actionpack/lib/action_dispatch/middleware/stack.rb
Expand Up @@ -5,13 +5,13 @@ class MiddlewareStack < Array
class Middleware
attr_reader :args, :block

def initialize(klass, *args, &block)
@klass, @args, @block = klass, args, block
def initialize(klass_or_name, *args, &block)
@ref = ActiveSupport::Dependencies::Reference.new(klass_or_name)
@args, @block = args, block
end

def klass
return @klass if @klass.respond_to?(:new)
@klass = ActiveSupport::Inflector.constantize(@klass.to_s)
@ref.get
end

def ==(middleware)
Expand All @@ -21,11 +21,7 @@ def ==(middleware)
when Class
klass == middleware
else
if lazy_compare?(@klass) && lazy_compare?(middleware)
normalize(@klass) == normalize(middleware)
else
klass.name == normalize(middleware.to_s)
end
normalize(@ref.name) == normalize(middleware)
end
end

Expand All @@ -39,10 +35,6 @@ def build(app)

private

def lazy_compare?(object)
object.is_a?(String) || object.is_a?(Symbol)
end

def normalize(object)
object.to_s.strip.sub(/^::/, '')
end
Expand Down

0 comments on commit 509f3d7

Please sign in to comment.