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?

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



155
156
157
158
159
160
161
162
163
# File 'lib/dry/system/provider_registrar.rb', line 155

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



107
108
109
# File 'lib/dry/system/provider_registrar.rb', line 107

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



179
180
181
182
# File 'lib/dry/system/provider_registrar.rb', line 179

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dry/system/provider_registrar.rb', line 124

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



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

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



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

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



173
174
175
176
# File 'lib/dry/system/provider_registrar.rb', line 173

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



185
186
187
188
# File 'lib/dry/system/provider_registrar.rb', line 185

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



191
192
193
194
# File 'lib/dry/system/provider_registrar.rb', line 191

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