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 (String)

    the name for this provider collection.

  • providers (Hash) (defaults to: {})

    the configured providers.

  • default (Object) (defaults to: nil)

    the configured default provider.


30
31
32
33
34
35
36
# 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

Instance Attribute Details

#defaultObject? #default=(provider) ⇒ Object?

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

Overloads:

  • #defaultObject?

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

  • #default=(provider) ⇒ Object?
    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 (Object)

      the provider to use as default.

    Raises:

    • (ArgumentError)

      when the given provider is not in the provider collection.

Returns:

  • (Object, nil)

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


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

def default
  @default
end

#nameString (readonly)

The name of this provider collection.

Returns:

  • (String)

    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) ⇒ Object

Get provider corresponding to a given format.

Parameters:

  • format (Symbol)

    the format to search for in the collection.

Returns:

  • (Object)

    the provider corresponding to that format.

See Also:


91
92
93
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 91

def [] format
  providers[format]
end

#add(extension, provider) ⇒ Object

Adds a new provider to the provider collection.

Parameters:

  • extension (Symbol)

    the extension that the provider will correspond to.

  • provider (provider)

    the provider to add to the provider collection.


43
44
45
# File 'lib/rambling/trie/configuration/provider_collection.rb', line 43

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:


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

def formats
  providers.keys
end

#providersHash

List of configured providers.

Returns:

  • (Hash)

    the mapping of extensions to their corresponding providers.


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

def providers
  @providers ||= {}
end

#resetObject

Resets the provider collection to the initial values.


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

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

#resolve(filepath) ⇒ Object

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

Parameters:

  • filepath (String)

    the filepath to resolve into a provider.

Returns:

  • (Object)

    the provider corresponding to the file extension in this provider collection. #default if not found.


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

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