Class: Cabriolet::Commands::CommandDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/cli/command_dispatcher.rb

Overview

Unified command dispatcher that routes commands to format-specific handlers

The dispatcher is responsible for:

  1. Detecting the format of input files (or using manual override)

  2. Selecting the appropriate format handler from the registry

  3. Delegating command execution to the handler

This class implements the Strategy pattern, where the format handler is the strategy selected based on the detected format.

Examples:

Using the dispatcher

dispatcher = CLI::CommandDispatcher.new(format: :cab, verbose: true)
dispatcher.dispatch(:list, "archive.cab")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandDispatcher

Initialize the command dispatcher

Options Hash (options):

  • :format (String, Symbol)

    Manual format override

  • :verbose (Boolean)

    Enable verbose output



29
30
31
32
# File 'lib/cabriolet/cli/command_dispatcher.rb', line 29

def initialize(options = {})
  @format_override = parse_format_option(options[:format])
  @verbose = options[:verbose] || false
end

Class Method Details

.format_supported?(format) ⇒ Boolean

Check if a format is supported



56
57
58
# File 'lib/cabriolet/cli/command_dispatcher.rb', line 56

def self.format_supported?(format)
  CommandRegistry.format_registered?(format)
end

.supported_formatsArray<Symbol>

Get list of supported formats



63
64
65
# File 'lib/cabriolet/cli/command_dispatcher.rb', line 63

def self.supported_formats
  CommandRegistry.registered_formats
end

Instance Method Details

#dispatch(command, file, *args, **options) ⇒ void

This method returns an undefined value.

Dispatch a command to the appropriate format handler

This method detects the format (if not manually specified), retrieves the appropriate handler, and delegates the command execution.

Raises:



45
46
47
48
49
50
# File 'lib/cabriolet/cli/command_dispatcher.rb', line 45

def dispatch(command, file, *args, **options)
  format = detect_format(file)
  handler = get_handler_for(format)

  execute_command(handler, command, file, args, options)
end