Sha256: 8ea35a2612bf0fed7a49cf8d7b5e2e8f1cf34cd80e59901e20168af99b75e3e9

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8

module Implements
  # Interface: mix into your interfaces.
  module Interface
    # Used to find a suitable implementation
    # @api public
    # @param [*selectors] zero or more selectors to use for finding an
    #   implementation of this interface. If none is given, :auto is assumed.
    # @return [Implementation::Registry::Finder]
    def implementation(*selectors)
      selectors << :auto if selectors.empty?
      Implementation::Registry::Finder.new(@implementations, selectors)
    end

    # Returns a list of implementations by resolvable name.
    # @api public
    # @return [Array<String>]
    def list_implementation_names
      @implementations.list_names.map(&:to_s).uniq
    end

    # Find an instantiate a suitable implementation on auto mode
    # @see Implementation::Registry::Find#new
    # @api public
    def new(*args, &block)
      implementation(:auto).new(*args, &block)
    end

    # @api private
    # Used by Implementation#implements
    # @param implements (see Registry#register)
    # @param options (see Registry#register)
    # @param &block (see Registry#register)
    # @return (see Registry#register)
    def register_implementation(implementation, options, &block)
      @implementations.register(implementation, options, block)
    end

    # Bad things happen when used improperly. Make it harder to get it wrong.
    # @api private
    def self.included(base)
      base && fail(ScriptError, "#{self} supports only extend, not include.")
    end

    # Set up the interface.
    # @param base [Module]
    # @api private
    def self.extended(base)
      unless base.instance_of?(Module)
        fail(TypeError, "expected Module, got #{base.class}")
      end

      base.instance_variable_set(:@implementations,
                                 Implementation::Registry.new(base))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
implements-0.0.2 lib/implements/interface.rb