Class: Rigor::Plugin::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/plugin/loader.rb

Overview

Resolves the project’s ‘.rigor.yml` `plugins:` entries into instantiated plugin instances, paired with a service container. Internal slice-1 implementation; the public surface is Loader.load returning a Registry.

Steps per entry (in order):

  1. Normalise the entry into ‘{ gem:, id:, config: }`.

  2. ‘require` the gem (failures surface as a LoadError).

  3. Look up the registered plugin class by id (or by gem name if the entry omitted an explicit id).

  4. Validate the user’s config against the manifest’s ‘config_schema`.

  5. Instantiate the plugin and call ‘init(services)`.

Loading is deterministic: configuration order, with plugin id alphabetical as the tie-breaker for entries that resolve to the same gem. Failures do not abort the run; the loader collects them on the Registry so the runner can convert each one into a ‘:plugin_loader` diagnostic.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services:, requirer: ->(name) { require name }) ⇒ Loader

Returns a new instance of Loader.

Parameters:

  • services (Rigor::Plugin::Services)
  • requirer (#call) (defaults to: ->(name) { require name })

    takes a gem name and returns truthy on successful require. Defaulted to ‘Kernel.require` via a lambda; the spec injects a fake to avoid touching the real load path.



36
37
38
39
# File 'lib/rigor/plugin/loader.rb', line 36

def initialize(services:, requirer: ->(name) { require name })
  @services = services
  @requirer = requirer
end

Instance Attribute Details

#requirerObject (readonly)

rubocop:disable Metrics/ClassLength



29
30
31
# File 'lib/rigor/plugin/loader.rb', line 29

def requirer
  @requirer
end

#servicesObject (readonly)

rubocop:disable Metrics/ClassLength



29
30
31
# File 'lib/rigor/plugin/loader.rb', line 29

def services
  @services
end

Class Method Details

.load(configuration:, services:, requirer: ->(name) { require name }) ⇒ Object



41
42
43
# File 'lib/rigor/plugin/loader.rb', line 41

def self.load(configuration:, services:, requirer: ->(name) { require name })
  new(services: services, requirer: requirer).load(configuration.plugins)
end

Instance Method Details

#load(entries) ⇒ Registry

Parameters:

  • entries (Array<String, Hash>)

    the raw ‘plugins:` list from the configuration.

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rigor/plugin/loader.rb', line 48

def load(entries)
  plugins = []
  load_errors = []
  seen_ids = {}

  Array(entries).each_with_index do |raw, index|
    entry = normalise_entry(raw, index)
  rescue LoadError => e
    load_errors << e
  else
    begin
      plugin = resolve_and_instantiate(entry, seen_ids)
      plugins << plugin if plugin
    rescue LoadError => e
      load_errors << e
    end
  end

  Registry.new(plugins: plugins, load_errors: load_errors)
end