Class: RailsAiBridge::RubydexAdapter::MethodCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/rubydex_adapter/method_counter.rb

Overview

Counts method-like definitions across rubydex declarations.

Replaces the nested conditional counting in the adapter with a flat, testable pipeline. A definition is considered a method when its name includes parentheses or it is classified as a method type by the serializer.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer:) ⇒ MethodCounter

Returns a new instance of MethodCounter.

Parameters:

  • serializer (Serializer)

    for declaration_type classification



13
14
15
# File 'lib/rails_ai_bridge/rubydex_adapter/method_counter.rb', line 13

def initialize(serializer:)
  @serializer = serializer
end

Class Method Details

.method_count_for(decl, serializer) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/rails_ai_bridge/rubydex_adapter/method_counter.rb', line 28

def self.method_count_for(decl, serializer)
  defs = decl.try(:definitions)
  return 0 unless defs

  defs.count { |defn| method_like?(defn, serializer) }
rescue StandardError
  0
end

.method_like?(defn, serializer) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/rails_ai_bridge/rubydex_adapter/method_counter.rb', line 37

def self.method_like?(defn, serializer)
  defn.name.to_s.include?('(') || serializer.declaration_type(defn) == 'method'
rescue StandardError
  false
end

Instance Method Details

#count(declarations) ⇒ Integer

Counts method definitions across declarations.

Parameters:

  • declarations (Array<Object>)

    rubydex declaration objects

Returns:

  • (Integer)

    total method count

Raises:

  • (StandardError)

    rescued internally, returns 0



22
23
24
25
26
# File 'lib/rails_ai_bridge/rubydex_adapter/method_counter.rb', line 22

def count(declarations)
  declarations.sum { |decl| self.class.method_count_for(decl, @serializer) }
rescue StandardError
  0
end