Class: Dry::System::ProviderRegistrar Private

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

Overview

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

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.

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.



25
26
27
28
# File 'lib/dry/system/provider_registrar.rb', line 25

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

Instance Attribute Details

#containerObject (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.



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

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.



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

def providers
  @providers
end

Instance Method Details

#[](provider_name) ⇒ Object Also known as: provider

Returns a provider for the given name, if it has already been loaded



76
77
78
# File 'lib/dry/system/provider_registrar.rb', line 76

def [](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.



141
142
143
144
145
146
147
148
149
# File 'lib/dry/system/provider_registrar.rb', line 141

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

  providers.each_value(&:start)

  freeze
end

#find_and_load_provider(name) ⇒ Dry::System::Provider?

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 provider if it can be found or loaded, otherwise nil

Returns:



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

def find_and_load_provider(name)
  name = name.to_sym

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

  return if finalized?

  require_provider_file(name)

  providers[name]
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.



31
32
33
34
# File 'lib/dry/system/provider_registrar.rb', line 31

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)


82
83
84
# File 'lib/dry/system/provider_registrar.rb', line 82

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.



165
166
167
168
# File 'lib/dry/system/provider_registrar.rb', line 165

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


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

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

#register_provider(name, namespace: nil, from: nil, source: nil, if: true, &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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dry/system/provider_registrar.rb', line 40

def register_provider(name, namespace: nil, from: nil, source: nil, if: true, &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,
        namespace: namespace,
        source: source || name,
        group: from,
        &block
      )
    else
      build_provider(name, namespace: namespace, source: source, &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.



159
160
161
162
# File 'lib/dry/system/provider_registrar.rb', line 159

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.



171
172
173
174
# File 'lib/dry/system/provider_registrar.rb', line 171

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

#start_provider_dependency(component) ⇒ 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.



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

def start_provider_dependency(component)
  if (provider = find_and_load_provider(component.root_key))
    provider.start
  end
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.



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

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