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

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

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. When nil (or no providers are given), falls back to the first configured provider, or nil if none exist.

  • (Symbol)
  • (Hash[Symbol, TProvider])
  • (TProvider, nil)


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

#configured_defaultTProvider? (readonly)

Returns the value of attribute configured_default.

Returns:

  • (TProvider, nil)


95
96
97
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 95

def configured_default
  @configured_default
end

#configured_providersHash[Symbol, TProvider] (readonly)

Returns the value of attribute configured_providers.

Returns:

  • (Hash[Symbol, TProvider])


95
96
97
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 95

def configured_providers
  @configured_providers
end

#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?

    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.



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

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.

  • (Symbol)

Returns:

  • (TProvider, nil)

    the provider corresponding to that format, or nil if not found.

See Also:



89
90
91
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 89

def [] format
  providers[format]
end

#[]=(format, instance) ⇒ TProvider

Parameters:

  • (Symbol)
  • (TProvider)

Returns:

  • (TProvider)


97
98
99
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 97

def []= format, instance
  providers[format] = instance
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.

  • (Symbol)
  • (TProvider)

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

#contains?(provider) ⇒ Boolean

Parameters:

  • (TProvider, nil)

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 109

def contains? provider
  return true if provider.nil?

  provider_instances.include?(provider || raise)
end

#file_format(filepath) ⇒ Symbol

Parameters:

  • (String)

Returns:

  • (Symbol)


105
106
107
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 105

def file_format filepath
  File.extname(filepath).sub(%r{^\.}, '').to_sym
end

#formatsArray<Symbol>

List the formats of configured providers.

Returns:

  • (Array<Symbol>)

    the formats of all configured providers.

See Also:



81
82
83
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 81

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.



71
72
73
74
75
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 71

def reset
  providers.clear
  configured_providers.each { |extension, provider| self[extension] = provider }
  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.

  • (String)

Returns:

  • (TProvider, nil)

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



60
61
62
63
64
65
66
67
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 60

def resolve filepath
  extension = file_format filepath
  if providers.key? extension
    providers[extension]
  else
    default
  end
end

#valuesArray[TProvider] Also known as: provider_instances

Returns:

  • (Array[TProvider])


101
102
103
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 101

def values
  providers.values
end