Module: Implements::Interface
- Defined in:
- lib/implements/interface.rb
Overview
Interface: mix into your interfaces.
Class Method Summary collapse
-
.extended(base) ⇒ Object
private
Set up the interface.
-
.included(base) ⇒ Object
private
Bad things happen when used improperly.
Instance Method Summary collapse
-
#implementation(*selectors) ⇒ Implementation::Registry::Finder
Used to find a suitable implementation.
-
#list_implementation_names ⇒ Array<String>
Returns a list of implementations by resolvable name.
-
#new(*args, &block) ⇒ Object
Find an instantiate a suitable implementation on auto mode.
-
#register_implementation(implementation, options, &block) ⇒ Object
private
Used by Implementation#implements.
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.
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
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_names ⇒ Array<String>
Returns a list of implementations by resolvable name.
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
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, , &block) @implementations.register(implementation, , block) end |