Module: LLM::Contract

Included in:
Completion
Defined in:
lib/llm/contract.rb,
lib/llm/contract/completion.rb

Overview

The LLM::Contract module provides the ability for modules who are extended by it to implement contracts which must be implemented by other modules who include a given contract.

Examples:

module LLM::Contract
   # ..
end

module LLM::Contract
  module Completion
    extend LLM::Contract
    # inheriting modules must implement these methods
    # otherwise an error is raised on include
    def foo = nil
    def bar = nil
  end
end

module LLM::OpenAI::ResponseAdapter
  module Completion
    def foo = nil
    def bar = nil
    include LLM::Contract::Completion
  end
end

Defined Under Namespace

Modules: Completion

Constant Summary collapse

ContractError =
Class.new(LLM::Error)

Instance Method Summary collapse

Instance Method Details

#included(mod) ⇒ 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.



37
38
39
40
41
42
43
44
45
46
# File 'lib/llm/contract.rb', line 37

def included(mod)
  meths = mod.instance_methods(false)
  if meths.empty?
    raise ContractError, "#{mod} does not implement any methods required by #{self}"
  end
  missing = instance_methods - meths
  if missing.any?
    raise ContractError, "#{mod} does not implement methods (#{missing.join(", ")}) required by #{self}"
  end
end