Class: AnnotateRb::ModelAnnotator::FileParser::AnnotationFinder
- Inherits:
-
Object
- Object
- AnnotateRb::ModelAnnotator::FileParser::AnnotationFinder
- Defined in:
- lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb
Defined Under Namespace
Classes: MalformedAnnotation, NoAnnotationFound
Constant Summary collapse
- COMPAT_PREFIX =
"== Schema Info"- COMPAT_PREFIX_MD =
"## Schema Info"- DEFAULT_ANNOTATION_ENDING =
"#"- SCHEMA_HEADER_PREFIXES =
[ COMPAT_PREFIX, COMPAT_PREFIX_MD, "Table name:", "Database name:", "Schema version:" ].freeze
- SCHEMA_HEADER_EXACT =
[ IndexAnnotation::Annotation::HEADER_TEXT, ForeignKeyAnnotation::Annotation::HEADER_TEXT, CheckConstraintAnnotation::Annotation::HEADER_TEXT ].freeze
Instance Attribute Summary collapse
-
#annotation_end ⇒ Object
readonly
Returns the line index (not the line number) that the annotation ends, inclusive.
-
#annotation_start ⇒ Object
readonly
Returns the line index (not the line number) that the annotation starts.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
-
#annotated? ⇒ Boolean
Returns true if annotations are detected in the file content.
- #annotation ⇒ Object
-
#initialize(content, wrapper_open, wrapper_close, parser) ⇒ AnnotationFinder
constructor
A new instance of AnnotationFinder.
-
#run ⇒ Object
Find the annotation’s line start and line end.
Constructor Details
#initialize(content, wrapper_open, wrapper_close, parser) ⇒ AnnotationFinder
Returns a new instance of AnnotationFinder.
36 37 38 39 40 41 42 43 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 36 def initialize(content, wrapper_open, wrapper_close, parser) @content = content @wrapper_open = wrapper_open @wrapper_close = wrapper_close @annotation_start = nil @annotation_end = nil @parser = parser end |
Instance Attribute Details
#annotation_end ⇒ Object (readonly)
Returns the line index (not the line number) that the annotation ends, inclusive.
32 33 34 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 32 def annotation_end @annotation_end end |
#annotation_start ⇒ Object (readonly)
Returns the line index (not the line number) that the annotation starts.
30 31 32 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 30 def annotation_start @annotation_start end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
34 35 36 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 34 def parser @parser end |
Instance Method Details
#annotated? ⇒ Boolean
Returns true if annotations are detected in the file content
106 107 108 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 106 def annotated? @annotation_start && @annotation_end end |
#annotation ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 97 def annotation @annotation ||= begin lines = @content.lines lines[@annotation_start..@annotation_end].join end end |
#run ⇒ Object
Find the annotation’s line start and line end
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/annotate_rb/model_annotator/file_parser/annotation_finder.rb', line 46 def run comments = @parser.comments start = comments.find_index { |comment, _| comment.include?(COMPAT_PREFIX) || comment.include?(COMPAT_PREFIX_MD) } raise NoAnnotationFound if start.nil? # Stop execution because we did not find if @wrapper_open prev_comment, _prev_line_number = comments[start - 1] # Change start to the line before if wrapper_open is defined and we find the wrapper open comment if prev_comment&.include?(@wrapper_open) start -= 1 end end # Find a contiguous block of comments from the starting point ending = start while ending < comments.size - 1 _comment, line_number = comments[ending] _next_comment, next_line_number = comments[ending + 1] if next_line_number - line_number == 1 ending += 1 else break end end raise MalformedAnnotation if start == ending if @wrapper_close if comments[ending].first.include?(@wrapper_close) # We can end here because it's the end of the annotation block else # Walk back until we find the end of the annotation comment block or the wrapper close to be flexible # We check if @wrapper_close is a substring because `comments` contains strings with the comment character while ending > start && comments[ending].first != DEFAULT_ANNOTATION_ENDING && !comments[ending].first.include?(@wrapper_close) ending -= 1 end end else ending = walk_forward_through_schema(comments, start, ending) end # We want .last because we want the line indexes @annotation_start = comments[start].last @annotation_end = comments[ending].last [@annotation_start, @annotation_end] end |