class Rambling::Trie::Configuration::ProviderCollection

Collection of configurable providers.

Attributes

configured_default[R]
configured_providers[R]
default[R]

@overload default

The default provider. Used when a provider cannot be resolved in
{ProviderCollection#resolve #resolve}.

@overload default=(provider)

Sets the default provider. Needs to be one of the configured
providers.
@param [TProvider] provider the provider to use as default.
@raise [ArgumentError] when the given provider is not in the
  provider collection.
@note If no providers have been configured, +nil+ will be assigned.

@return [TProvider, nil] the default provider to use when a provider

cannot be resolved in {ProviderCollection#resolve #resolve}.
name[R]

The name of this provider collection. @return [Symbol] the name of this provider collection.

Public Class Methods

new(name, providers = {}) click to toggle source

Creates a new provider collection. @param [Symbol] name the name for this provider collection. @param [Hash<Symbol, TProvider>] providers the configured providers. @param [TProvider, nil] default the configured default provider.

# File lib/rambling/trie/configuration/provider_collection.rb, line 30
def initialize name, providers = {}, default = nil
  @name = name
  @configured_providers = providers
  @configured_default = default || providers.values.first

  reset
end

Public Instance Methods

[](format) click to toggle source

Get provider corresponding to a given format. @param [Symbol] format the format to search for in the collection. @return [TProvider] the provider corresponding to that format. @see ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D

Hash#[]
# File lib/rambling/trie/configuration/provider_collection.rb, line 93
def [] format
  providers[format]
end
add(extension, provider) click to toggle source

Adds a new provider to the provider collection. @param [Symbol] extension the extension that the provider will

correspond to.

@param [TProvider] provider the provider to add to the provider

collection.

@return [TProvider] the provider just added.

# File lib/rambling/trie/configuration/provider_collection.rb, line 44
def add extension, provider
  providers[extension] = provider
end
default=(provider) click to toggle source
# File lib/rambling/trie/configuration/provider_collection.rb, line 48
def default= provider
  unless contains? provider
    raise ArgumentError,
      "default #{name} should be part of configured #{name}s"
  end

  @default = provider
end
formats() click to toggle source

Get provider corresponding to a given format. @return [Array<Symbol>] the provider corresponding to that format. @see ruby-doc.org/core-2.7.0/Hash.html#method-i-5B-5D

Hash#keys
# File lib/rambling/trie/configuration/provider_collection.rb, line 84
def formats
  providers.keys
end
providers() click to toggle source

List of configured providers. @return [Hash<Symbol, TProvider>] the mapping of extensions to their

corresponding providers.
# File lib/rambling/trie/configuration/provider_collection.rb, line 60
def providers
  @providers ||= {}
end
reset() click to toggle source

Resets the provider collection to the initial values. @return [void]

# File lib/rambling/trie/configuration/provider_collection.rb, line 74
def reset
  providers.clear
  configured_providers.each { |k, v| self[k] = v }
  self.default = configured_default
end
resolve(filepath) click to toggle source

Resolves the provider from a filepath based on the file extension. @param [String] filepath the filepath to resolve into a provider. @return [TProvider, nil] the provider for the given file’s extension.

{#default} if not found.
# File lib/rambling/trie/configuration/provider_collection.rb, line 68
def resolve filepath
  providers[file_format filepath] || default
end

Private Instance Methods

[]=(format, instance) click to toggle source
# File lib/rambling/trie/configuration/provider_collection.rb, line 101
def []= format, instance
  providers[format] = instance
end
contains?(provider) click to toggle source
# File lib/rambling/trie/configuration/provider_collection.rb, line 115
def contains? provider
  provider.nil? ||
    (providers.any? && provider_instances.include?(provider))
end
file_format(filepath) click to toggle source
# File lib/rambling/trie/configuration/provider_collection.rb, line 109
def file_format filepath
  format = File.extname filepath
  format.slice! 0
  format.to_sym
end
provider_instances()
Alias for: values
values() click to toggle source
# File lib/rambling/trie/configuration/provider_collection.rb, line 105
def values
  providers.values
end
Also aliased as: provider_instances