Sha256: 629cda8d1b40eca17e2b0eae1e937674d54bfc714e32262b7174ee9bc6ef534f

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

module Dry
  class Container
    # Default resolver for resolving items from container
    #
    # @api public
    class Resolver
      # Resolve an item from the container
      #
      # @param [Concurrent::Hash] container
      #   The container
      # @param [Mixed] key
      #   The key for the item you wish to resolve
      #
      # @raise [Dry::Conainer::Error]
      #   If the given key is not registered with the container
      #
      # @return [Mixed]
      #
      # @api public
      def call(container, key)
        item = container.fetch(key.to_s) do
          raise Error, "Nothing registered with the key #{key.inspect}"
        end

        item.call
      end

      # Check whether an items is registered under the given key
      #
      # @param [Concurrent::Hash] container
      #   The container
      # @param [Mixed] key
      #   The key you wish to check for registration with
      #
      # @return [Bool]
      #
      # @api public
      def key?(container, key)
        container.key?(key.to_s)
      end

      # An array of registered names for the container
      #
      # @return [Array]
      #
      # @api public
      def keys(container)
        container.keys
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-container-0.3.4 lib/dry/container/resolver.rb
dry-container-0.3.3 lib/dry/container/resolver.rb
dry-container-0.3.2 lib/dry/container/resolver.rb
dry-container-0.3.1 lib/dry/container/resolver.rb