Sha256: 6fc86939b5c9c7bfefa2007cb893a0fe96a6c506cc7890b99f81c8611b5d9cd7

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

module Dry
  class Container
    module Stub
      # Overrides resolve to look into stubbed keys first
      #
      # @api public
      def resolve(key)
        _stubs.fetch(key.to_s) { super }
      end

      # Add a stub to the container
      def stub(key, value, &block)
        unless key?(key)
          raise ArgumentError, "cannot stub #{ key.to_s.inspect } - no such key in container"
        end

        _stubs[key.to_s] = value

        if block
          yield
          unstub(key)
        end

        self
      end

      # Remove stubbed keys from the container
      def unstub(*keys)
        keys = _stubs.keys if keys.empty?
        keys.each { |key| _stubs.delete(key.to_s) }
      end

      # Stubs have already been enabled turning this into a noop
      def enable_stubs!
        # DO NOTHING
      end

      private

      # Stubs container
      def _stubs
        @_stubs ||= {}
      end
    end

    module Mixin
      # Enable stubbing functionality into the current container
      def enable_stubs!
        extend ::Dry::Container::Stub
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/dry-container-0.7.2/lib/dry/container/stub.rb
dry-container-0.7.2 lib/dry/container/stub.rb
dry-container-0.7.1 lib/dry/container/stub.rb
dry-container-0.7.0 lib/dry/container/stub.rb