Module: Implements::Interface

Defined in:
lib/implements/interface.rb

Overview

Interface: mix into your interfaces.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set up the interface.

Parameters:

  • base (Module)


49
50
51
52
53
54
55
56
# File 'lib/implements/interface.rb', line 49

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

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bad things happen when used improperly. Make it harder to get it wrong.



42
43
44
# File 'lib/implements/interface.rb', line 42

def self.included(base)
  base && fail(ScriptError, "#{self} supports only extend, not include.")
end

Instance Method Details

#implementation(*selectors) ⇒ Implementation::Registry::Finder

Used to find a suitable implementation

Parameters:

  • zero (*selectors)

    or more selectors to use for finding an implementation of this interface. If none is given, :auto is assumed.

Returns:



11
12
13
14
# File 'lib/implements/interface.rb', line 11

def implementation(*selectors)
  selectors << :auto if selectors.empty?
  Implementation::Registry::Finder.new(@implementations, selectors)
end

#list_implementation_namesArray<String>

Returns a list of implementations by resolvable name.

Returns:

  • (Array<String>)


19
20
21
# File 'lib/implements/interface.rb', line 19

def list_implementation_names
  @implementations.list_names.map(&:to_s).uniq
end

#new(*args, &block) ⇒ Object

Find an instantiate a suitable implementation on auto mode

See Also:

  • Implementation::Registry::Find#new


26
27
28
# File 'lib/implements/interface.rb', line 26

def new(*args, &block)
  implementation(:auto).new(*args, &block)
end

#register_implementation(implementation, options, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used by Implementation#implements



36
37
38
# File 'lib/implements/interface.rb', line 36

def register_implementation(implementation, options, &block)
  @implementations.register(implementation, options, block)
end