Class: AnnotateRb::ModelAnnotator::AnnotationDiffGenerator
- Inherits:
-
Object
- Object
- AnnotateRb::ModelAnnotator::AnnotationDiffGenerator
- Defined in:
- lib/annotate_rb/model_annotator/annotation_diff_generator.rb
Overview
Compares the current file content and new annotation block and generates the column annotation differences
Constant Summary collapse
- HEADER_PATTERN =
Leading whitespace is tolerated so that annotations placed inside a nested module/class (i.e. with –nested-position, or hand-aligned) still match.
/(^[ \t]*# Table name:.*?\n([ \t]*#.*\r?\n)*\r?)/- COLUMN_PATTERN =
Example matches:
- "# id :uuid not null, primary key" - "# title(length 255) :string not null" - "# status(a/b/c) :string not null" - "# created_at :datetime not null" - "# updated_at :datetime not null" /^[ \t]*#[\t ]+[[\p{L}\p{N}_]*.`\[\]():]+(?:\(.*?\))?[\t ]+.+$/
Class Method Summary collapse
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(file_content, annotation_block) ⇒ AnnotationDiffGenerator
constructor
A new instance of AnnotationDiffGenerator.
Constructor Details
#initialize(file_content, annotation_block) ⇒ AnnotationDiffGenerator
Returns a new instance of AnnotationDiffGenerator.
26 27 28 29 |
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 26 def initialize(file_content, annotation_block) @file_content = file_content @annotation_block = annotation_block end |
Class Method Details
.call(file_content, annotation_block) ⇒ Object
19 20 21 |
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 19 def call(file_content, annotation_block) new(file_content, annotation_block).generate end |
Instance Method Details
#generate ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 31 def generate # Ignore the Schema version line because it changes with each migration current_annotations = @file_content.match(HEADER_PATTERN).to_s new_annotations = @annotation_block.match(HEADER_PATTERN).to_s # Strip leading whitespace from each scanned column so that an # indented existing block compares equal to a flush-left new block. current_annotation_columns = if current_annotations.present? current_annotations.scan(COLUMN_PATTERN).map(&:lstrip) else [] end new_annotation_columns = if new_annotations.present? new_annotations.scan(COLUMN_PATTERN).map(&:lstrip) else [] end _result = AnnotationDiff.new(current_annotation_columns, new_annotation_columns) end |