Class: Ast::Merge::SectionTyping::CallableClassifier

Inherits:
Classifier
  • Object
show all
Defined in:
lib/ast/merge/section_typing.rb

Overview

A classifier that uses a callable (proc/lambda) for classification.

This allows defining classifiers without creating a subclass.

Examples:

Using a lambda classifier

classifier = CallableClassifier.new(->(node) {
  return nil unless node.respond_to?(:name) && node.name == :appraise
  TypedSection.new(type: :appraise, name: extract_name(node), node: node)
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Classifier

#classifies?, #classify_all

Constructor Details

#initialize(callable) ⇒ CallableClassifier

Initialize with a callable.

Parameters:

  • callable (#call)

    A callable that takes a node and returns TypedSection or nil



177
178
179
# File 'lib/ast/merge/section_typing.rb', line 177

def initialize(callable)
  @callable = callable
end

Instance Attribute Details

#callable#call (readonly)

Returns The callable used for classification.

Returns:

  • (#call)

    The callable used for classification



172
173
174
# File 'lib/ast/merge/section_typing.rb', line 172

def callable
  @callable
end

Instance Method Details

#classify(node) ⇒ TypedSection?

Classify using the callable.

Parameters:

  • node (Object)

    Node to classify

Returns:



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ast/merge/section_typing.rb', line 185

def classify(node)
  result = callable.call(node)
  return if result.nil?

  # Allow callable to return a Hash and convert to TypedSection
  if result.is_a?(Hash)
    TypedSection.new(**result)
  else
    result
  end
end