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 =

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 ]+[[\p{L}\p{N}_]*.`\[\]():]+(?:\(.*?\))?[\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



23
24
25
26
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 23

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

Class Method Details

.call(file_content, annotation_block) ⇒ Object



16
17
18
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 16

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

Instance Method Details

#generateObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/annotate_rb/model_annotator/annotation_diff_generator.rb', line 28

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