Sha256: 63b5e9bcab5c16fa8f5afeaff9381b26010a0f87c54300fa4bedb645ea2f3cd0

Contents?: true

Size: 932 Bytes

Versions: 5

Compression:

Stored size: 932 Bytes

Contents

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

      # Add a stub to the container
      def stub(key, value, &block)
        _stubs[key] = 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) }
      end

      # Stubs have already been enabled turning this into a noop
      def enable_stubs!
      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

5 entries across 5 versions & 1 rubygems

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