Class: Dry::System::ProviderRegistrar

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/system/provider_registrar.rb

Overview

Default provider registrar implementation

This is currently configured by default for every Dry::System::Container. The provider registrar is responsible for loading provider files and exposing an API for running the provider lifecycle steps.

Since:

  • 1.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ ProviderRegistrar

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ProviderRegistrar.

Since:

  • 1.1.0



34
35
36
37
# File 'lib/dry/system/provider_registrar.rb', line 34

def initialize(container)
  @providers = {}
  @container = container
end

Instance Attribute Details

#containerObject (readonly) Also known as: target_container

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



23
24
25
# File 'lib/dry/system/provider_registrar.rb', line 23

def container
  @container
end

#providersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



20
21
22
# File 'lib/dry/system/provider_registrar.rb', line 20

def providers
  @providers
end

Instance Method Details

#[](provider_name) ⇒ Dry::System::Provider? Also known as: find_and_load_provider

Returns a provider if it can be found or loaded, otherwise nil

Returns:

Since:

  • 1.1.0



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dry/system/provider_registrar.rb', line 92

def [](provider_name)
  provider_name = provider_name.to_sym

  if (provider = providers[provider_name])
    return provider
  end

  return if finalized?

  require_provider_file(provider_name)

  providers[provider_name]
end

#finalize!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



158
159
160
161
162
163
164
165
166
# File 'lib/dry/system/provider_registrar.rb', line 158

def finalize!
  provider_files.each do |path|
    load_provider(path)
  end

  providers.each_value(&:start)

  freeze
end

#freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



40
41
42
43
# File 'lib/dry/system/provider_registrar.rb', line 40

def freeze
  providers.freeze
  super
end

#key?(provider_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 1.1.0



110
111
112
# File 'lib/dry/system/provider_registrar.rb', line 110

def key?(provider_name)
  providers.key?(provider_name)
end

#prepare(provider_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



182
183
184
185
# File 'lib/dry/system/provider_registrar.rb', line 182

def prepare(provider_name)
  with_provider(provider_name, &:prepare)
  self
end

#provider_filesArray<Pathname>

Returns all provider files within the configured provider_paths.

Searches for files in the order of the configured provider_paths. In the case of multiple identically-named boot files within different provider_paths, the file found first will be returned, and other matching files will be discarded.

This method is public to allow other tools extending dry-system (like dry-rails) to access a canonical list of real, in-use provider files.

Returns:

  • (Array<Pathname>)

See Also:

  • Container.provider_paths

Since:

  • 1.1.0



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dry/system/provider_registrar.rb', line 127

def provider_files
  @provider_files ||= provider_paths.each_with_object([[], []]) { |path, (provider_files, loaded)| # rubocop:disable Layout/LineLength
    files = Dir["#{path}/#{RB_GLOB}"].sort

    files.each do |file|
      basename = File.basename(file)

      unless loaded.include?(basename)
        provider_files << Pathname(file)
        loaded << basename
      end
    end
  }.first
end

#provider_source_classObject

Extension point for subclasses to customize their provider source superclass. Expected to be a subclass of Dry::System::Provider::Source

Since:

  • 1.1.0



148
# File 'lib/dry/system/provider_registrar.rb', line 148

def provider_source_class = Dry::System::Provider::Source

#provider_source_optionsObject

Extension point for subclasses to customize initialization params for provider_source_class

Since:

  • 1.1.0



155
# File 'lib/dry/system/provider_registrar.rb', line 155

def provider_source_options = {}

#register_provider(name, from: nil, source: nil, if: true, **provider_options, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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
79
80
81
82
83
# File 'lib/dry/system/provider_registrar.rb', line 49

def register_provider(name, from: nil, source: nil, if: true, **provider_options, &block)
  raise ProviderAlreadyRegisteredError, name if providers.key?(name)

  if from && source.is_a?(Class)
    raise ArgumentError, "You must supply a block when using a provider source"
  end

  if block && source.is_a?(Class)
    raise ArgumentError, "You must supply only a `source:` option or a block, not both"
  end

  return self unless binding.local_variable_get(:if)

  provider =
    if from
      build_provider_from_source(
        name,
        source: source || name,
        group: from,
        options: provider_options,
        &block
      )
    else
      build_provider(
        name,
        source: source,
        options: provider_options,
        &block
      )
    end

  providers[provider.name] = provider

  self
end

#shutdownObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



176
177
178
179
# File 'lib/dry/system/provider_registrar.rb', line 176

def shutdown
  providers.each_value(&:stop)
  self
end

#start(provider_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



188
189
190
191
# File 'lib/dry/system/provider_registrar.rb', line 188

def start(provider_name)
  with_provider(provider_name, &:start)
  self
end

#stop(provider_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.1.0



194
195
196
197
# File 'lib/dry/system/provider_registrar.rb', line 194

def stop(provider_name)
  with_provider(provider_name, &:stop)
  self
end