Class: TudlaContracts::Integrations::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/tudla_contracts/integrations/registry.rb

Overview

Shared class for registering and looking up integrations

Constant Summary collapse

VALID_TYPES =
%w[time_sheet].freeze

Class Method Summary collapse

Class Method Details

.all_of_type(type) ⇒ Object

Used by the Host App to look up all integrations of a specific type



69
70
71
# File 'lib/tudla_contracts/integrations/registry.rb', line 69

def all_of_type(type)
  @integrations.select { |_, v| v[:type] == type.to_s }.values
end

.available_providers ⇒ Object

Used to populate UI dropdowns



84
85
86
# File 'lib/tudla_contracts/integrations/registry.rb', line 84

def available_providers
  @integrations.keys
end

.config_class_for(name) ⇒ String?

Used by the Host App to find the config class for polymorphic associations

Parameters:

  • name (String, Symbol) —

    Integration name

Returns:

  • (String, nil) —

    Fully qualified config class name or nil if not found



76
77
78
79
80
81
# File 'lib/tudla_contracts/integrations/registry.rb', line 76

def config_class_for(name)
  entry = @integrations[name.to_s]
  return nil unless entry

  entry[:config_class]
end

.find(name) ⇒ String

Used by the Host App to look up a specific provider

Parameters:

  • name (String, Symbol) —

    Integration name

Returns:

  • (String) —

    Fully qualified provider class name

Raises:

  • (ArgumentError)


61
62
63
64
65
66
# File 'lib/tudla_contracts/integrations/registry.rb', line 61

def find(name)
  entry = @integrations[name.to_s]
  raise ArgumentError, "Unknown integration: #{name}" unless entry

  entry[:provider_class]
end

.register(name, type:, provider_class:, config_class:) ⇒ Object

Method called by gems to register themselves

Parameters:

  • name (String, Symbol) —

    Unique identifier for the integration

  • type (String, Symbol) —

    Integration type (e.g., :time_sheet)

  • provider_class (String) —

    Fully qualified class name of the provider

  • config_class (String) —

    Fully qualified class name of the config



20
21
22
23
24
25
26
# File 'lib/tudla_contracts/integrations/registry.rb', line 20

def register(name, type:, provider_class:, config_class:)
  @integrations[name.to_s] = {
    type: type.to_s,
    provider_class: provider_class.to_s,
    config_class: config_class.to_s
  }
end

.register_view_for_slot(slot, view_definition) ⇒ Object

Method called by gems to register view definitions for slots

Parameters:

  • slot (String, Symbol) —

    Slot name

  • view_definition (Object) —

    An instance of a class that inherits from ViewDefinitions::Base



37
38
39
40
41
42
43
44
# File 'lib/tudla_contracts/integrations/registry.rb', line 37

def register_view_for_slot(slot, view_definition)
  unless view_definition.class.ancestors.any? { |a| a.name&.end_with?("ViewDefinitions::Base") }
    raise ArgumentError, "view_definition must be an instance of a class that inherits from ViewDefinitions::Base"
  end

  @view_slots[slot.to_s] ||= []
  @view_slots[slot.to_s] << view_definition
end

.registered_slots ⇒ Object

Used by the Host App to look up all registered slots



54
55
56
# File 'lib/tudla_contracts/integrations/registry.rb', line 54

def registered_slots
  @view_slots.keys
end

.reset! ⇒ Object

Reset the registry



29
30
31
32
# File 'lib/tudla_contracts/integrations/registry.rb', line 29

def reset!
  @integrations = {}
  @view_slots = {}
end

.views_for_slot(slot) ⇒ Array<String>

Used by the Host App to look up view components for a specific slot

Parameters:

  • slot (String, Symbol) —

    Slot name

Returns:

  • (Array<String>) —

    Array of fully qualified view component class names



49
50
51
# File 'lib/tudla_contracts/integrations/registry.rb', line 49

def views_for_slot(slot)
  @view_slots[slot.to_s] || []
end