Class: RailsOpenapiGen::Parsers::Jbuilder::CallDetectors::ArrayCallDetector

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

Overview

Detects array calls (json.array!) Handles array generation in Jbuilder templates

Class Method Summary collapse

Class Method Details

.array_call?(receiver, method_name) ⇒ Boolean

Check if this is an array call

Parameters:

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

    Method receiver

  • method_name (Symbol)

    Method name

Returns:

  • (Boolean)

    True if this is an array call



48
49
50
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 48

def array_call?(receiver, method_name)
  method_name == :array! && json_receiver?(receiver)
end

.categorySymbol

Returns Detector category.

Returns:

  • (Symbol)

    Detector category



35
36
37
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 35

def category
  :array
end

.descriptionString

Returns Description.

Returns:

  • (String)

    Description



40
41
42
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 40

def description
  "Array calls (json.array!)"
end

.extract_collection(args) ⇒ Parser::AST::Node?

Extract collection from array arguments

Parameters:

  • args (Array<Parser::AST::Node>)

    Method arguments

Returns:

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

    Collection node



83
84
85
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 83

def extract_collection(args)
  args.find { |arg| arg.type != :hash }
end

.extract_options(args) ⇒ Parser::AST::Node?

Extract options hash from array arguments

Parameters:

  • args (Array<Parser::AST::Node>)

    Method arguments

Returns:

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

    Options hash node



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

def extract_options(args)
  args.find { |arg| arg.type == :hash }
end

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

Check if this detector handles the method call

Parameters:

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

    Method receiver node

  • method_name (Symbol)

    Method name

  • args (Array<Parser::AST::Node>)

    Method arguments

Returns:

  • (Boolean)

    True if this is an array call



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

def handles?(receiver, method_name, _args = [])
  result = array_call?(receiver, method_name)
  if ENV['RAILS_OPENAPI_DEBUG']
    puts "🔍 DEBUG: ArrayCallDetector.handles? for #{method_name}"
    puts "   receiver: #{receiver.inspect}"
    puts "   method_name: #{method_name.inspect}"
    puts "   method_name == :array!: #{method_name == :array!}"
    puts "   json_receiver?: #{json_receiver?(receiver)}"
    puts "   result: #{result}"
  end
  result
end

.has_collection?(args) ⇒ Boolean

Check if array call has collection argument

Parameters:

  • args (Array<Parser::AST::Node>)

    Method arguments

Returns:

  • (Boolean)

    True if array has collection



69
70
71
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 69

def has_collection?(args)
  args.any? && !args.first.type == :hash
end

.has_options?(args) ⇒ Boolean

Check if array call has options hash

Parameters:

  • args (Array<Parser::AST::Node>)

    Method arguments

Returns:

  • (Boolean)

    True if array has options



76
77
78
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 76

def has_options?(args)
  args.any? { |arg| arg.type == :hash }
end

.has_partial_key?(hash_node) ⇒ Boolean

Check if arguments contain partial key in hash

Parameters:

  • hash_node (Parser::AST::Node)

    Hash node to check

Returns:

  • (Boolean)

    True if hash contains partial key



55
56
57
58
59
60
61
62
63
64
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 55

def has_partial_key?(hash_node)
  return false unless hash_node.type == :hash

  hash_node.children.any? do |pair|
    next false unless pair.type == :pair

    key, _value = pair.children
    key.type == :sym && key.children.first == :partial
  end
end

.priorityInteger

High priority for array calls

Returns:

  • (Integer)

    Priority value



30
31
32
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 30

def priority
  20
end

.uses_partial?(args) ⇒ Boolean

Check if array call uses partial rendering

Parameters:

  • args (Array<Parser::AST::Node>)

    Method arguments

Returns:

  • (Boolean)

    True if array uses partial



97
98
99
100
# File 'lib/rails-openapi-gen/parsers/jbuilder/call_detectors/array_call_detector.rb', line 97

def uses_partial?(args)
  options = extract_options(args)
  options && has_partial_key?(options)
end