Skip to main content

How to call a method skipping its middlewares?

warning

The functionality described on this page is available from v0.20.

For some rare cases, it may be useful to have a way to call a method skipping all its middlewares.

Consider the following example.

class Service
include ConvenientService::Standard::Config

def result
success
end
end

To make it more illustrative, let's add a simple middleware that just prints some text before and after calling the original result.

class Middleware < ConvenientService::MethodChainMiddleware
def next(...)
puts "Before calling `#{method}`..."

value = chain.next(...)

puts "After calling `#{method}`..."

value
end
end

This is how it can be registered.

class Service
include ConvenientService::Standard::Config

middlewares :result, scope: :class do
use Middleware
end

def result
success
end
end

So now, when we invoke Service.result, additional logs are printed.

result = Service.result
# Before calling result...
# After calling result...
# => <Service::Result status: :success>

But there is still a possibility to call it without middlewares like so:

result = Service.result_without_middlewares
# => <Service::Result status: :success>
note

When the original method ends with ?, like success?, the corresponding skipping method is success_without_middlewares?.

The same rule is applicable for methods with trailing !.

bar! -> bar_without_middlewares!.

danger

Keep in mind that the ability to skip middlewares is present just for debugging purposes.

There are NO common use cases for the production business code with it.