Class: Makit::Commands::Middleware::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/commands/middleware/base.rb

Overview

Base class for all command execution middleware.

Middleware provides a way to add cross-cutting concerns like logging, caching, validation, and timing to command execution without modifying the core execution logic.

Examples:

Creating custom middleware

class CustomMiddleware < Base
  def call(request, &block)
    # Pre-execution logic
    puts "About to execute: #{request.command}"

    # Execute the command
    result = block.call(request)

    # Post-execution logic
    puts "Execution completed: #{result.success? ? 'SUCCESS' : 'FAILED'}"

    result
  end
end

Direct Known Subclasses

Cache, CommandLogger

Instance Method Summary collapse

Instance Method Details

#applicable?(_request) ⇒ Boolean

Check if this middleware should be applied to the given request.

Override this method to provide conditional middleware application based on request properties.

Parameters:

  • request (Request)

    the command request

Returns:

  • (Boolean)

    true if middleware should be applied



51
52
53
# File 'lib/makit/commands/middleware/base.rb', line 51

def applicable?(_request)
  true
end

#call(request) {|Request| ... } ⇒ Result

Execute middleware logic.

This method must be implemented by subclasses to provide the actual middleware functionality. The pattern is to perform any pre-execution logic, call the block to continue the middleware chain, then perform any post-execution logic.

Parameters:

  • request (Request)

    the command request to process

Yields:

  • (Request)

    yields the processed request to the next middleware

Yield Returns:

  • (Result)

    the execution result

Returns:

  • (Result)

    the final execution result

Raises:

  • (NotImplementedError)

    if not overridden by subclass



40
41
42
# File 'lib/makit/commands/middleware/base.rb', line 40

def call(request, &block)
  raise NotImplementedError, "#{self.class.name} must implement #call"
end

#configHash

Get middleware configuration.

Override this method to provide middleware-specific configuration.

Returns:

  • (Hash)

    middleware configuration



67
68
69
# File 'lib/makit/commands/middleware/base.rb', line 67

def config
  {}
end

#nameString

Get middleware name for logging and debugging.

Returns:

  • (String)

    middleware name



58
59
60
# File 'lib/makit/commands/middleware/base.rb', line 58

def name
  self.class.name.split("::").last
end