Class: Rambling::Trie::Configuration::ProviderCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/rambling/trie/configuration/provider_collection.rb

Overview

Collection of configurable providers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, providers = {}, default = nil) ⇒ ProviderCollection

Creates a new provider collection.

Parameters:

  • name (Symbol)

    the name for this provider collection.

  • providers (Hash<Symbol, TProvider>) (defaults to: {})

    the configured providers.

  • default (TProvider, nil) (defaults to: nil)

    the configured default provider.



29
30
31
32
33
34
35
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 29

def initialize name, providers = {}, default = nil
  @name = name
  @configured_providers = providers
  @configured_default = default || providers.values.first

  reset
end

Instance Attribute Details

#defaultTProvider? #default=(provider) ⇒ TProvider?

Returns the default provider to use when a provider cannot be resolved in #resolve.

Overloads:

  • #defaultTProvider?

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

  • #default=(provider) ⇒ TProvider?
    Note:

    If no providers have been configured, nil will be assigned.

    Sets the default provider. Needs to be one of the configured providers.

    Parameters:

    • provider (TProvider)

      the provider to use as default.

    Raises:

    • (ArgumentError)

      when the given provider is not in the provider collection.

Returns:

  • (TProvider, nil)

    the default provider to use when a provider cannot be resolved in #resolve.



23
24
25
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 23

def default
  @default
end

#nameSymbol (readonly)

The name of this provider collection.

Returns:

  • (Symbol)

    the name of this provider collection.



10
11
12
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 10

def name
  @name
end

Instance Method Details

#[](format) ⇒ TProvider

Get provider corresponding to a given format.

Parameters:

  • format (Symbol)

    the format to search for in the collection.

Returns:

  • (TProvider)

    the provider corresponding to that format.

See Also:



84
85
86
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 84

def [] format
  providers[format]
end

#add(extension, provider) ⇒ TProvider

Adds a new provider to the provider collection.

Parameters:

  • extension (Symbol)

    the extension that the provider will correspond to.

  • provider (TProvider)

    the provider to add to the provider collection.

Returns:

  • (TProvider)

    the provider just added.



41
42
43
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 41

def add extension, provider
  providers[extension] = provider
end

#formatsArray<Symbol>

Get provider corresponding to a given format.

Returns:

  • (Array<Symbol>)

    the provider corresponding to that format.

See Also:



76
77
78
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 76

def formats
  providers.keys
end

#providersHash<Symbol, TProvider>

List of configured providers.

Returns:

  • (Hash<Symbol, TProvider>)

    the mapping of extensions to their corresponding providers.



53
54
55
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 53

def providers
  @providers ||= {}
end

#resetvoid

This method returns an undefined value.

Resets the provider collection to the initial values.



66
67
68
69
70
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 66

def reset
  providers.clear
  configured_providers.each { |k, v| self[k] = v }
  self.default = configured_default
end

#resolve(filepath) ⇒ TProvider?

Resolves the provider from a filepath based on the file extension.

Parameters:

  • filepath (String)

    the filepath to resolve into a provider.

Returns:

  • (TProvider, nil)

    the provider for the given file’s extension. #default if not found.



60
61
62
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 60

def resolve filepath
  providers[file_format filepath] || default
end