Class: AnnotateRb::ModelAnnotator::AnnotationDiffGenerator

Inherits:
Object
  • Object
show all
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 =
/(^# Table name:.*?\n(#.*\r?\n)*\r?)/
COLUMN_PATTERN =
/^#[\t ]+[\w*.`\[\]():]+[\t ]+.+$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_content, annotation_block) ⇒ AnnotationDiffGenerator

Returns a new instance of AnnotationDiffGenerator.

Parameters:

  • file_content (String)

    The current file content of the model file we intend to annotate

  • annotation_block (String)

    The annotation block we intend to write to the model file



18
19
20
21
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 18

def initialize(file_content, annotation_block)
  @file_content = file_content
  @annotation_block = annotation_block
end

Class Method Details

.call(file_content, annotation_block) ⇒ Object



11
12
13
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 11

def call(file_content, annotation_block)
  new(file_content, annotation_block).generate
end

Instance Method Details

#generateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 23

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

  current_annotation_columns = if current_annotations.present?
    current_annotations.scan(COLUMN_PATTERN).sort
  else
    []
  end

  new_annotation_columns = if new_annotations.present?
    new_annotations.scan(COLUMN_PATTERN).sort
  else
    []
  end

  _result = AnnotationDiff.new(current_annotation_columns, new_annotation_columns)
end