Module: AnnotateRb::ModelAnnotator::FileParser::AnnotationTarget

Defined in:
lib/annotate_rb/model_annotator/file_parser/annotation_target.rb

Overview

When ‘model_class_name` is given, matches that class declaration directly so inner classes inside the model body cannot be mistaken for the target.

Constant Summary collapse

SKIP_NAMES =
%w[require require_relative load].freeze

Class Method Summary collapse

Class Method Details

.find(parser, options, model_class_name: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_target.rb', line 11

def self.find(parser, options, model_class_name: nil)
  starts = parser.starts.reject { |entry| SKIP_NAMES.include?(entry.first) }
  return nil if starts.empty?

  return starts.first unless options[:nested_position] && parser.respond_to?(:type_map)

  if model_class_name && parser.type_map[model_class_name] == :class
    match = starts.find { |name, _line| name == model_class_name }
    return match if match
  end

  class_entries = starts
    .select { |name, _line| parser.type_map[name] == :class }
    .uniq { |name, _line| name }

  class_entries.last || starts.first
end