Class: SourceMonitor::Configuration::DeprecationRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/configuration/deprecation_registry.rb

Overview

Registry for deprecated configuration options.

Engine developers register deprecations at boot time via the DSL:

SourceMonitor::Configuration::DeprecationRegistry.register(
  "http.old_proxy_url",
  removed_in: "0.5.0",
  replacement: "http.proxy",
  severity: :warning,
  message: "Use config.http.proxy instead"
)

When a host app’s initializer accesses a deprecated option, the trapping method fires automatically:

- :warning  -- logs via Rails.logger.warn and forwards to replacement
- :error    -- raises SourceMonitor::DeprecatedOptionError

Constant Summary collapse

SETTINGS_CLASSES =

Maps settings accessor names (as used on Configuration) to their classes.

{
  "http" => "HTTPSettings",
  "fetching" => "FetchingSettings",
  "health" => "HealthSettings",
  "scraping" => "ScrapingSettings",
  "retention" => "RetentionSettings",
  "realtime" => "RealtimeSettings",
  "authentication" => "AuthenticationSettings",
  "images" => "ImagesSettings",
  "scrapers" => "ScraperRegistry",
  "events" => "Events",
  "models" => "Models"
}.freeze

Class Method Summary collapse

Class Method Details

.check_defaults!(_config) ⇒ Object

No-op hook for future “default changed” checks. Called by Configuration#check_deprecations! after the configure block.



105
106
107
108
# File 'lib/source_monitor/configuration/deprecation_registry.rb', line 105

def check_defaults!(_config)
  # Reserved for future use. Phases may add checks like:
  # "option X changed its default from A to B in version Y"
end

.clear!Object

Remove all registered deprecation traps and clear state. Essential for test isolation.



82
83
84
85
86
87
88
89
90
91
# File 'lib/source_monitor/configuration/deprecation_registry.rb', line 82

def clear!
  defined_methods.each do |target_class, method_name|
    target_class.remove_method(method_name) if target_class.method_defined?(method_name)
  rescue NameError
    # Method was already removed or never defined; ignore.
  end

  @entries = {}
  @defined_methods = []
end

.entriesObject

Returns a duplicate of the entries hash for inspection.



94
95
96
# File 'lib/source_monitor/configuration/deprecation_registry.rb', line 94

def entries
  @entries ||= {}
end

.register(path, removed_in:, replacement: nil, severity: :warning, message: nil) ⇒ Object

Register a deprecated configuration option.

Parameters:

  • path (String)

    dot-notation path, e.g. “http.old_proxy_url” or “old_queue_prefix”

  • removed_in (String)

    version in which the option was deprecated

  • replacement (String, nil) (defaults to: nil)

    dot-notation path to the replacement option

  • severity (:warning, :error) (defaults to: :warning)

    :warning logs + forwards, :error raises

  • message (String, nil) (defaults to: nil)

    additional migration guidance



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/source_monitor/configuration/deprecation_registry.rb', line 48

def register(path, removed_in:, replacement: nil, severity: :warning, message: nil)
  segments = path.split(".")
  source_prefix = nil
  if segments.length == 1
    target_class = Configuration
    option_name = segments.first
  else
    source_prefix = segments.first
    option_name = segments.last
    class_name = SETTINGS_CLASSES[source_prefix]
    raise ArgumentError, "Unknown settings accessor: #{source_prefix}" unless class_name

    target_class = Configuration.const_get(class_name)
  end

  deprecation_message = build_message(path, removed_in, replacement, message)

  if target_class.method_defined?(:"#{option_name}=") || target_class.method_defined?(option_name.to_sym)
    warn "[SourceMonitor] DeprecationRegistry: '#{path}' already exists on #{target_class.name}. " \
         "Skipping trap definition -- the option is not yet removed/renamed."
    entries[path] = { path: path, removed_in: removed_in, replacement: replacement,
                      severity: severity, message: deprecation_message, skipped: true }
    return
  end

  define_trap_methods(target_class, option_name, deprecation_message, severity, replacement,
                      source_prefix: source_prefix)

  entries[path] = { path: path, removed_in: removed_in, replacement: replacement,
                    severity: severity, message: deprecation_message, skipped: false }
end

.registered?(path) ⇒ Boolean

Check if a path is registered.

Returns:

  • (Boolean)


99
100
101
# File 'lib/source_monitor/configuration/deprecation_registry.rb', line 99

def registered?(path)
  entries.key?(path)
end