Class: RailsOpenapiGen::Parsers::Jbuilder::CallDetectors::DetectorRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb

Overview

Registry for managing all call detectors

Class Method Summary collapse

Class Method Details

.all_detectorsArray<Class>

Get all available detectors sorted by priority

Returns:

  • (Array<Class>)

    Detector classes sorted by priority (high to low)



18
19
20
21
22
23
24
25
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb', line 18

def all_detectors
  @all_detectors ||= [
    ArrayCallDetector,
    PartialCallDetector,
    JsonCallDetector,
    CacheCallDetector
  ].sort_by { |detector| -detector.priority }
end

.by_category(category) ⇒ Array<Class>

Get detectors by category

Parameters:

  • category (Symbol)

    Detector category

Returns:

  • (Array<Class>)

    Detectors in the category



56
57
58
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb', line 56

def by_category(category)
  all_detectors.select { |detector| detector.category == category }
end

.categoriesArray<Symbol>

Get all available categories

Returns:

  • (Array<Symbol>)

    Available categories



62
63
64
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb', line 62

def categories
  all_detectors.map(&:category).uniq
end

.find_detector(receiver, method_name, args = []) ⇒ Class?

Find the appropriate detector for a method call

Parameters:

  • receiver (Parser::AST::Node, nil)

    Method receiver

  • method_name (Symbol)

    Method name

  • args (Array<Parser::AST::Node>) (defaults to: [])

    Method arguments

Returns:

  • (Class, nil)

    Appropriate detector class or nil



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb', line 32

def find_detector(receiver, method_name, args = [])
  if ENV['RAILS_OPENAPI_DEBUG']
    puts "🔍 DEBUG: DetectorRegistry.find_detector called with method: #{method_name}"
    puts "🔍 DEBUG: Available detectors: #{all_detectors.map(&:name)}"
  end

  result = all_detectors.find do |detector|
    handles = detector.handles?(receiver, method_name, args)
    if ENV['RAILS_OPENAPI_DEBUG']
      puts "🔍 DEBUG: #{detector.name}.handles?(#{receiver}, #{method_name}, #{args}) = #{handles}"
    end
    handles
  end

  if ENV['RAILS_OPENAPI_DEBUG']
    puts "🔍 DEBUG: Found detector: #{result ? result.name : 'nil'}"
  end

  result
end

.handles?(receiver, method_name, args = []) ⇒ Boolean

Check if a method call is handled by any detector

Parameters:

  • receiver (Parser::AST::Node, nil)

    Method receiver

  • method_name (Symbol)

    Method name

  • args (Array<Parser::AST::Node>) (defaults to: [])

    Method arguments

Returns:

  • (Boolean)

    True if any detector handles the call



90
91
92
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb', line 90

def handles?(receiver, method_name, args = [])
  !find_detector(receiver, method_name, args).nil?
end

.register(detector_class) ⇒ void

This method returns an undefined value.

Add a custom detector to the registry

Parameters:

  • detector_class (Class)

    Detector class to add



69
70
71
72
73
74
75
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb', line 69

def register(detector_class)
  return unless detector_class < BaseDetector

  @all_detectors = nil # Reset cache
  all_detectors << detector_class unless all_detectors.include?(detector_class)
  @all_detectors = all_detectors.sort_by { |detector| -detector.priority }
end

.unregister(detector_class) ⇒ void

This method returns an undefined value.

Remove a detector from the registry

Parameters:

  • detector_class (Class)

    Detector class to remove



80
81
82
83
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors.rb', line 80

def unregister(detector_class)
  @all_detectors = nil # Reset cache
  all_detectors.delete(detector_class)
end