Class: RailsOpenapiGen::Parsers::ControllerParser

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

Defined Under Namespace

Classes: ActionMethodProcessor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, template_processor: nil) ⇒ ControllerParser

Initializes controller parser with route information and template processor

Parameters:

  • Route hash containing controller and action info

  • (defaults to: nil)

    Template processor for extracting template paths



14
15
16
17
18
19
# File 'lib/rails-openapi-gen/parsers/controller_parser.rb', line 14

def initialize(route, template_processor: nil)
  @route = route
  @template_processor = template_processor || RailsOpenapiGen::Parsers::TemplateProcessors::JbuilderTemplateProcessor.new(
    route[:controller], route[:action]
  )
end

Instance Attribute Details

#routeObject (readonly)

Returns the value of attribute route.



9
10
11
# File 'lib/rails-openapi-gen/parsers/controller_parser.rb', line 9

def route
  @route
end

#template_processorObject (readonly)

Returns the value of attribute template_processor.



9
10
11
# File 'lib/rails-openapi-gen/parsers/controller_parser.rb', line 9

def template_processor
  @template_processor
end

Instance Method Details

#parseHash

Parses controller to find action method and response template

Returns:

  • Controller info including paths and parameters



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails-openapi-gen/parsers/controller_parser.rb', line 23

def parse
  controller_path = find_controller_file
  return {} unless controller_path

  content = File.read(controller_path)
  ast = Parser::CurrentRuby.parse(content)

  action_node = find_action_method(ast)
  return {} unless action_node

  template_path = extract_template_path(action_node)
  parameters = extract_parameters_from_comments(content, action_node)

  {
    controller_path: controller_path,
    jbuilder_path: template_path, # Keep existing key name for backward compatibility
    action: route[:action],
    parameters: parameters
  }
end