Class: Rambling::Trie::Configuration::ProviderCollection
- Inherits:
-
Object
- Object
- Rambling::Trie::Configuration::ProviderCollection
- Defined in:
- lib/rambling/trie/configuration/provider_collection.rb
Overview
Collection of configurable providers.
Instance Attribute Summary collapse
-
#default ⇒ TProvider?
The default provider to use when a provider cannot be resolved in #resolve.
-
#name ⇒ Symbol
readonly
The name of this provider collection.
Instance Method Summary collapse
-
#[](format) ⇒ TProvider
Get provider corresponding to a given format.
-
#add(extension, provider) ⇒ TProvider
Adds a new provider to the provider collection.
-
#formats ⇒ Array<Symbol>
Get provider corresponding to a given format.
-
#initialize(name, providers = {}, default = nil) ⇒ ProviderCollection
constructor
Creates a new provider collection.
-
#providers ⇒ Hash<Symbol, TProvider>
List of configured providers.
-
#reset ⇒ void
Resets the provider collection to the initial values.
-
#resolve(filepath) ⇒ TProvider?
Resolves the provider from a filepath based on the file extension.
Constructor Details
#initialize(name, providers = {}, default = nil) ⇒ ProviderCollection
Creates a new provider collection.
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
#default ⇒ TProvider? #default=(provider) ⇒ TProvider?
Returns 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 |
#name ⇒ Symbol (readonly)
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.
89 90 91 |
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 89 def [] format providers[format] end |
#add(extension, provider) ⇒ TProvider
Adds a new provider to the provider collection.
41 42 43 |
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 41 def add extension, provider providers[extension] = provider end |
#formats ⇒ Array<Symbol>
Get provider corresponding to a given format.
81 82 83 |
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 81 def formats providers.keys end |
#providers ⇒ Hash<Symbol, TProvider>
List of configured providers.
53 54 55 |
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 53 def providers @providers ||= {} end |
#reset ⇒ void
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.
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 |