Skip to main content

Decorator Middleware

A decorator middleware is a kind of middleware that always calls the next middleware in a chain.

HasCallbacks is a good example.

module ConvenientService
module Common
module Plugins
module HasCallbacks
class Middleware < Core::MethodChainMiddleware
# ...
def next(...)
entity.callbacks.for([:before, method]).each { |callback| callback.call_in_context(entity) }

original_value = chain.next(...)

entity.callbacks.for([:after, method]).reverse_each { |callback| callback.call_in_context(entity, original_value) }

original_value
end
# ...
end
end
end
end
end

It triggers before callbacks, then chain.next(...), and later after callbacks.

There are no conditionals that may skip chain.next(...) invocation.

That is why this middleware is considered decorator middleware.

In contrast, proxy middlewares may not call chain.next if needed.