lib/rambling/trie/configuration/provider_collection.rb in rambling-trie-1.0.2 vs lib/rambling/trie/configuration/provider_collection.rb in rambling-trie-1.0.3
- old
+ new
@@ -1,12 +1,10 @@
module Rambling
module Trie
module Configuration
# Collection of configurable providers.
class ProviderCollection
- extend ::Forwardable
-
# The name of this provider collection.
# @return [String] the name of this provider collection.
attr_reader :name
# @overload default
@@ -21,17 +19,10 @@
# @note If no providers have been configured, `nil` will be assigned.
# @return [Object, nil] the default provider to use when a provider
# cannot be resolved in {ProviderCollection#resolve #resolve}.
attr_reader :default
- delegate [
- :[],
- :[]=,
- :keys,
- :values,
- ] => :providers
-
# Creates a new provider collection.
# @param [String] name the name for this provider collection.
# @param [Hash] providers the configured providers.
# @param [Object] default the configured default provider.
def initialize name, providers = {}, default = nil
@@ -50,11 +41,11 @@
def add extension, provider
providers[extension] = provider
end
def default= provider
- if provider_not_in_list? provider
+ unless contains? provider
raise ArgumentError, "default #{name} should be part of configured #{name}s"
end
@default = provider
end
@@ -69,33 +60,60 @@
# Resolves the provider from a filepath based on the file extension.
# @param [String] filepath the filepath to resolve into a provider.
# @return [Object] the provider corresponding to the file extension in
# this provider collection. {#default} if not found.
def resolve filepath
- providers[format filepath] || default
+ providers[file_format filepath] || default
end
# Resets the provider collection to the initial values.
def reset
providers.clear
- configured_providers.each { |k, v| providers[k] = v }
+ configured_providers.each { |k, v| self[k] = v }
self.default = configured_default
end
+ # Get provider corresponding to a given format.
+ # @return [Array<Symbol>] the provider corresponding to that format.
+ # @see https://ruby-doc.org/core-2.5.0/Hash.html#method-i-5B-5D
+ # Hash#keys
+ def formats
+ providers.keys
+ end
+
+ # Get provider corresponding to a given format.
+ # @param [Symbol] format the format to search for in the collection.
+ # @return [Object] the provider corresponding to that format.
+ # @see https://ruby-doc.org/core-2.5.0/Hash.html#method-i-5B-5D
+ # Hash#[]
+ def [] format
+ providers[format]
+ end
+
private
attr_reader :configured_providers, :configured_default
- def format filepath
+ def []= format, instance
+ providers[format] = instance
+ end
+
+ def values
+ providers.values
+ end
+
+ def file_format filepath
format = File.extname filepath
format.slice! 0
format.to_sym
end
- def provider_not_in_list? provider
- (provider && providers.values.empty?) ||
- (providers.values.any? && !providers.values.include?(provider))
+ def contains? provider
+ provider.nil? ||
+ (providers.any? && provider_instances.include?(provider))
end
+
+ alias_method :provider_instances, :values
end
end
end
end