Module: Cuprum::Middleware
- Defined in:
- lib/cuprum/middleware.rb
Overview
Implements a wrapper around another command.
A middleware command wraps the execution of another command, allowing the developer to compose functionality without an explicit wrapper command. Because the middleware is responsible for calling the wrapped command, it has control over when that command is called, with what parameters, and how the command result is handled.
To use middleware, start by defining a middleware command. This can either be a class that includes Cuprum::Middleware, or a command instance that extends Cuprum::Middleware. Each middleware command’s #process method takes as its first argument the wrapped command. By convention, any additional arguments and any keywords or a block are passed to the wrapped command, but some middleware will override ths behavior.
When defining #process, make sure to either call super or call the wrapped command directly, unless the middleware is specifically intended not to call the wrapped command under those circumstances.
Middleware is powerful because it allows the developer to manipulate the parameters passed to a command, add handling to a result, or even intercept or override the command execution. These are some of the possible use cases for middleware:
-
Injecting code before or after a command.
-
Changing the parameters passed to a command.
-
Adding behavior based on the command result.
-
Overriding the command behavior based on the parameters.
Middleware is loosely coupled, meaning that one middleware command can wrap any number of other commands. One example would be logging middleware, which could record when a command is called and with what parameters. For a more involved example, consider authorization in a web application. If individual actions are defined as commands, then a single authorization middleware class could wrap each individual action, reducing both the testing burden and the amount of code that must be maintained.
Class Method Summary collapse
-
.apply(command:, middleware:) ⇒ Cuprum::Command
Helper method for wrapping a command with middleware.
Instance Method Summary collapse
-
#call(next_command, *arguments, **keywords) { ... } ⇒ Cuprum::Result
Calls the next command with the given arguments, keywords, and block.
Class Method Details
.apply(command:, middleware:) ⇒ Cuprum::Command
Helper method for wrapping a command with middleware.
This method takes the given command and middleware and returns a command that will call the middleware in order, followed by the given command. This is done via partial application: the last item in the middleware is partially applied with the given command as the middleware’s next command parameter. The next to last middleware is then partially applied with the last middleware as the next command and so on. This ensures that the middleware commands will be called in the given order, and that each middleware command wraps the next, down to the given command at the root.
197 198 199 200 201 202 203 204 205 |
# File 'lib/cuprum/middleware.rb', line 197 def self.apply(command:, middleware:) middleware = Array(middleware) return command if middleware.empty? middleware.reverse_each.reduce(command) do |next_command, cmd| cmd.curry(next_command) end end |
Instance Method Details
#call(next_command, *arguments, **keywords) { ... } ⇒ Cuprum::Result
Calls the next command with the given arguments, keywords, and block.
Subclasses can call super to easily call the next command with the given parameters, or pass explicit parameters into super to call the next command with those parameters.
|
# File 'lib/cuprum/middleware.rb', line 164
|