diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware')
| -rw-r--r-- | actionpack/lib/action_dispatch/middleware/stack.rb | 58 | ||||
| -rw-r--r-- | actionpack/lib/action_dispatch/middleware/static.rb | 8 | 
2 files changed, 50 insertions, 16 deletions
| diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index e3cd779756..a4308f528c 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -2,17 +2,26 @@ require "active_support/inflector/methods"  require "active_support/dependencies"  module ActionDispatch -  class MiddlewareStack < Array +  class MiddlewareStack      class Middleware -      attr_reader :args, :block +      attr_reader :args, :block, :name, :classcache        def initialize(klass_or_name, *args, &block) -        @ref = ActiveSupport::Dependencies::Reference.new(klass_or_name) +        @klass = nil + +        if klass_or_name.respond_to?(:name) +          @klass = klass_or_name +          @name  = @klass.name +        else +          @name  = klass_or_name.to_s +        end + +        @classcache = ActiveSupport::Dependencies::Reference          @args, @block = args, block        end        def klass -        @ref.get +        @klass || classcache[@name]        end        def ==(middleware) @@ -22,7 +31,7 @@ module ActionDispatch          when Class            klass == middleware          else -          normalize(@ref.name) == normalize(middleware) +          normalize(@name) == normalize(middleware)          end        end @@ -41,18 +50,39 @@ module ActionDispatch        end      end -    # Use this instead of super to work around a warning. -    alias :array_initialize :initialize +    include Enumerable + +    attr_accessor :middlewares      def initialize(*args) -      array_initialize(*args) +      @middlewares = []        yield(self) if block_given?      end +    def each +      @middlewares.each { |x| yield x } +    end + +    def size +      middlewares.size +    end + +    def last +      middlewares.last +    end + +    def [](i) +      middlewares[i] +    end + +    def initialize_copy(other) +      self.middlewares = other.middlewares.dup +    end +      def insert(index, *args, &block)        index = assert_index(index, :before)        middleware = self.class::Middleware.new(*args, &block) -      super(index, middleware) +      middlewares.insert(index, middleware)      end      alias_method :insert_before, :insert @@ -67,21 +97,25 @@ module ActionDispatch        delete(target)      end +    def delete(target) +      middlewares.delete target +    end +      def use(*args, &block)        middleware = self.class::Middleware.new(*args, &block) -      push(middleware) +      middlewares.push(middleware)      end      def build(app = nil, &block)        app ||= block        raise "MiddlewareStack#build requires an app" unless app -      reverse.inject(app) { |a, e| e.build(a) } +      middlewares.reverse.inject(app) { |a, e| e.build(a) }      end    protected      def assert_index(index, where) -      i = index.is_a?(Integer) ? index : self.index(index) +      i = index.is_a?(Integer) ? index : middlewares.index(index)        raise "No such middleware to insert #{where}: #{index.inspect}" unless i        i      end diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 913b899e20..c57f694c4d 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -3,10 +3,10 @@ require 'rack/utils'  module ActionDispatch    class FileHandler      def initialize(at, root) -      @at, @root = at.chomp('/'), root.chomp('/') -      @compiled_at = (Regexp.compile(/^#{Regexp.escape(at)}/) unless @at.blank?) -      @compiled_root = Regexp.compile(/^#{Regexp.escape(root)}/) -      @file_server = ::Rack::File.new(@root) +      @at, @root     = at.chomp('/'), root.chomp('/') +      @compiled_at   = @at.blank? ? nil : /^#{Regexp.escape(at)}/ +      @compiled_root = /^#{Regexp.escape(root)}/ +      @file_server   = ::Rack::File.new(@root)      end      def match?(path) | 
