Module: Braintrust::Classifier::Callable

Defined in:
lib/braintrust/classifier.rb

Overview

Included into classes that include Classifier. Prepends KeywordFilter and ClassificationNormalizer so #call receives only declared kwargs and always returns Array<Hash>. Also provides a default #name and #call_parameters.

Defined Under Namespace

Modules: ClassificationNormalizer

Constant Summary collapse

PREPENDED =

Infrastructure modules prepended onto every classifier class. Used both to set up the ancestor chain and to skip past them in #call_parameters so KeywordFilter sees the real call signature.

[Internal::Callable::KeywordFilter, ClassificationNormalizer].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Parameters:

  • base (Class)

    the class including Callable



97
98
99
# File 'lib/braintrust/classifier.rb', line 97

def self.included(base)
  PREPENDED.each { |mod| base.prepend(mod) }
end

Instance Method Details

#call_parametersArray<Array>

Provides KeywordFilter with the actual call signature of the subclass. Walks past PREPENDED modules in the ancestor chain so that user-defined #call keyword params are correctly introspected. Block overrides this to point directly at @block.parameters.

Returns:

  • (Array<Array>)

    parameter list



114
115
116
117
118
# File 'lib/braintrust/classifier.rb', line 114

def call_parameters
  meth = method(:call)
  meth = meth.super_method while meth.super_method && PREPENDED.include?(meth.owner)
  meth.parameters
end

#nameString

Default name derived from the class name (e.g. CategoryClassifier -> “category_classifier”).

Returns:

  • (String)


103
104
105
106
107
# File 'lib/braintrust/classifier.rb', line 103

def name
  klass = self.class.name&.split("::")&.last
  return Classifier::DEFAULT_NAME unless klass
  klass.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end