Class: Canon::Options::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/options/registry.rb

Overview

Centralized registry for all Canon options This is the SINGLE SOURCE OF TRUTH for option definitions All interfaces (CLI, Ruby API, RSpec) auto-generate from this registry

Class Method Summary collapse

Class Method Details

.all_optionsObject

Get all option definitions



13
14
15
16
17
18
19
20
21
22
# File 'lib/canon/options/registry.rb', line 13

def all_options
  @all_options ||= [
    preprocessing_option,
    diff_algorithm_option,
    diff_mode_option,
    *match_dimension_options,
    match_profile_option,
    *diff_formatting_options,
  ].freeze
end

.cli_flag_for(option_name) ⇒ Object

Get CLI flag name for an option



42
43
44
45
# File 'lib/canon/options/registry.rb', line 42

def cli_flag_for(option_name)
  opt = all_options.find { |o| o[:name] == option_name }
  opt&.dig(:cli_flag)
end

.default_for(option_name, format = nil) ⇒ Object

Get default value for an option



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/canon/options/registry.rb', line 48

def default_for(option_name, format = nil)
  opt = all_options.find { |o| o[:name] == option_name }
  return nil unless opt

  # Check for format-specific default
  if format && opt[:format_defaults]&.key?(format)
    opt[:format_defaults][format]
  else
    opt[:default]
  end
end

.options_for_format(format) ⇒ Object

Get options applicable to a specific format



25
26
27
28
29
# File 'lib/canon/options/registry.rb', line 25

def options_for_format(format)
  all_options.select do |opt|
    opt[:applies_to].nil? || opt[:applies_to].include?(format)
  end
end

.validate_options!(opts, format) ⇒ Object

Validate options hash against registry

Raises:



32
33
34
35
36
37
38
39
# File 'lib/canon/options/registry.rb', line 32

def validate_options!(opts, format)
  valid_option_names = options_for_format(format).map { |o| o[:name] }
  invalid = opts.keys - valid_option_names
  return if invalid.empty?

  raise Canon::Error,
        "Invalid options for #{format}: #{invalid.join(', ')}"
end