Module: Kotoshu::Embeddings::Protocol
- Included in:
- EmbeddingModelProtocol, SimilarityEngineProtocol, VocabularyProtocol
- Defined in:
- lib/kotoshu/embeddings/protocol.rb
Overview
Protocol - Ruby interface/contract system
Provides a simple way to define interfaces with required and optional methods.
Instance Method Summary collapse
-
#assert_implemented_by!(klass) ⇒ Object
Raise ProtocolError unless
klassimplements every required method. -
#compliance_errors(klass) ⇒ Object
Returns the subset of
required_methodsthatklassdoes not implement as an instance method. -
#optional(*names) ⇒ Object
Declare one or more optional methods on this protocol.
-
#optional_methods ⇒ Object
Set of method names that conforming classes may optionally implement.
-
#required(*names) ⇒ Object
Declare one or more required methods on this protocol.
-
#required_methods ⇒ Object
Set of method names that conforming classes must implement.
Instance Method Details
#assert_implemented_by!(klass) ⇒ Object
Raise ProtocolError unless klass implements every required method.
43 44 45 46 47 48 |
# File 'lib/kotoshu/embeddings/protocol.rb', line 43 def assert_implemented_by!(klass) errors = compliance_errors(klass) return if errors.empty? raise ProtocolError.new(klass, self, errors.to_a) end |
#compliance_errors(klass) ⇒ Object
Returns the subset of required_methods that klass does not
implement as an instance method. Empty result means full
conformance. klass should be a Class (not an instance); we
check method_defined? because protocol-declared methods are
mixed in via include, becoming instance methods of klass.
38 39 40 |
# File 'lib/kotoshu/embeddings/protocol.rb', line 38 def compliance_errors(klass) required_methods.reject { |m| klass.method_defined?(m) } end |
#optional(*names) ⇒ Object
Declare one or more optional methods on this protocol.
29 30 31 |
# File 'lib/kotoshu/embeddings/protocol.rb', line 29 def optional(*names) names.each { |n| optional_methods << n } end |
#optional_methods ⇒ Object
Set of method names that conforming classes may optionally implement.
Populated via optional (the writer); read via optional_methods.
19 20 21 |
# File 'lib/kotoshu/embeddings/protocol.rb', line 19 def optional_methods @optional_methods ||= Set.new end |
#required(*names) ⇒ Object
Declare one or more required methods on this protocol.
24 25 26 |
# File 'lib/kotoshu/embeddings/protocol.rb', line 24 def required(*names) names.each { |n| required_methods << n } end |
#required_methods ⇒ Object
Set of method names that conforming classes must implement.
Populated via required (the writer); read via required_methods.
13 14 15 |
# File 'lib/kotoshu/embeddings/protocol.rb', line 13 def required_methods @required_methods ||= Set.new end |