Module: Newshound::Warnings

Defined in:
lib/newshound/warnings.rb,
lib/newshound/warnings/base.rb

Defined Under Namespace

Classes: Base

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registryHash (readonly)

Access the registry of registered adapters

Returns:

  • (Hash)

    The registry hash mapping names to adapter classes



15
16
17
# File 'lib/newshound/warnings.rb', line 15

def registry
  @registry
end

Class Method Details

.clear_registry!Object

Clear the registry (primarily for testing)



51
52
53
# File 'lib/newshound/warnings.rb', line 51

def clear_registry!
  @registry = {}
end

.register(name, adapter_class) ⇒ Object

Register a warning adapter class with a symbolic name

Examples:

Newshound::Warnings.register(:unprocessable_events, MyApp::UnprocessableEventsWarning)

Parameters:

  • name (Symbol, String)

    The name to register the adapter under

  • adapter_class (Class)

    The adapter class (must inherit from Warnings::Base)



23
24
25
# File 'lib/newshound/warnings.rb', line 23

def register(name, adapter_class)
  @registry[name.to_sym] = adapter_class
end

.source(source) ⇒ Warnings::Base

Get a warning source adapter instance

Examples:

Newshound::Warnings.source(:unprocessable_events)
Newshound::Warnings.source(MyAdapter.new)

Parameters:

  • source (Symbol, Object)

    Either a symbolic name to look up, or an adapter instance

Returns:

Raises:

  • (RuntimeError)

    If the source symbol cannot be resolved



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/newshound/warnings.rb', line 35

def source(source)
  return source unless source.is_a?(Symbol)

  # First check the registry
  if @registry.key?(source)
    return @registry[source].new
  end

  # Fall back to constant lookup (like Exceptions module)
  constant = constants.find { |c| c.to_s.gsub(/(?<!^)([A-Z])/, "_\\1").downcase == source.to_s }
  raise "Invalid warning source: #{source}" unless constant

  const_get(constant).new
end