lib/acfs/service/middleware.rb in acfs-1.0.0.dev.1.b305 vs lib/acfs/service/middleware.rb in acfs-1.0.0

- old
+ new

@@ -1,8 +1,9 @@ +require 'acfs/service/middleware/stack' + module Acfs class Service - # Module providing all function to register middlewares # on services and process queued request through the # middleware stack. # module Middleware @@ -14,53 +15,42 @@ def prepare(request) self.class.middleware.call request end module ClassMethods - - # @api public + # @!method use(klass, *args, &block) + # @api public # - # Register a new middleware to be used for this service. + # Register a new middleware to be used for this service. # - # @example - # class MyService < Acfs::Service - # self.base_url = 'http://my.srv' - # use Acfs::Middleware::JsonDecoder - # end + # @example + # class MyService < Acfs::Service + # self.base_url = 'http://my.srv' + # use Acfs::Middleware::JSON + # end # - # @param [Class] klass Middleware class to instantiate and append to middleware stack. - # @param [Hash, Object] options Options to delegate to middleware class initializer. - # @return [undefined] + # @param [Class] klass Middleware class to append + # @param [Array<Object>] args Arguments passed to klass initialize + # @param [Proc] block Block passed to klass initialize + # @return [undefined] # - def use(klass, options = {}) - @middlewares ||= [] - - return false if @middlewares.include? klass - - @middlewares << klass - @middleware = klass.new(middleware, options) + def use(klass, *args, &block) + # Backward compatible behavior + middleware.insert(0, klass, *args, &block) end # @api private # # Return top most middleware. # # @return [#call] # def middleware - @middleware ||= proc { |request| request} + @middleware ||= Stack.new end - # @api public - # - # Clear all registered middlewares. - # - # @return [undefined] - # - def clear - @middleware = nil - @middlewares = [] - end + # @deprecated + delegate :clear, to: :middleware end end end end