lib/action_dispatch/middleware/stack.rb in actionpack-3.0.0.beta3 vs lib/action_dispatch/middleware/stack.rb in actionpack-3.0.0.beta4
- old
+ new
@@ -1,104 +1,55 @@
require "active_support/inflector/methods"
module ActionDispatch
class MiddlewareStack < Array
class Middleware
- def self.new(klass, *args, &block)
- if klass.is_a?(self)
- klass
- else
- super
- end
- end
-
attr_reader :args, :block
- def initialize(klass, *args, &block)
- @klass = klass
-
- options = args.extract_options!
- if options.has_key?(:if)
- @conditional = options.delete(:if)
- else
- @conditional = true
- end
- args << options unless options.empty?
-
- @args = args
- @block = block
+ def initialize(klass_or_name, *args, &block)
+ @ref = ActiveSupport::Dependencies::Reference.new(klass_or_name)
+ @args, @block = args, block
end
def klass
- if @klass.respond_to?(:new)
- @klass
- elsif @klass.respond_to?(:call)
- @klass.call
- else
- ActiveSupport::Inflector.constantize(@klass.to_s)
- end
+ @ref.get
end
- def active?
- return false unless klass
-
- if @conditional.respond_to?(:call)
- @conditional.call
- else
- @conditional
- end
- end
-
def ==(middleware)
case middleware
when Middleware
klass == middleware.klass
when Class
klass == middleware
else
- if lazy_compare?(@klass) && lazy_compare?(middleware)
- normalize(@klass) == normalize(middleware)
- else
- klass.name == middleware.to_s
- end
+ normalize(@ref.name) == normalize(middleware)
end
end
def inspect
klass.to_s
end
def build(app)
- if block
- klass.new(app, *build_args, &block)
- else
- klass.new(app, *build_args)
- end
+ klass.new(app, *args, &block)
end
- private
- def lazy_compare?(object)
- object.is_a?(String) || object.is_a?(Symbol)
- end
+ private
- def normalize(object)
- object.to_s.strip.sub(/^::/, '')
- end
-
- def build_args
- Array(args).map { |arg| arg.respond_to?(:call) ? arg.call : arg }
- end
+ def normalize(object)
+ object.to_s.strip.sub(/^::/, '')
+ end
end
def initialize(*args, &block)
super(*args)
block.call(self) if block_given?
end
def insert(index, *args, &block)
index = self.index(index) unless index.is_a?(Integer)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
super(index, middleware)
end
alias_method :insert_before, :insert
@@ -112,22 +63,21 @@
insert_before(target, *args, &block)
delete(target)
end
def use(*args, &block)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
push(middleware)
end
def active
- find_all { |middleware| middleware.active? }
+ ActiveSupport::Deprecation.warn "All middlewares in the chaing are active since the laziness " <<
+ "was removed from the middleware stack", caller
end
- def build(app = nil, &blk)
- app ||= blk
-
+ def build(app = nil, &block)
+ app ||= block
raise "MiddlewareStack#build requires an app" unless app
-
- active.reverse.inject(app) { |a, e| e.build(a) }
+ reverse.inject(app) { |a, e| e.build(a) }
end
end
end