Class: AnnotateRb::ModelAnnotator::AnnotatedFile::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/annotated_file/generator.rb

Overview

Generates the file with fresh annotations

Instance Method Summary collapse

Constructor Details

#initialize(file_content, new_annotations, annotation_position, parser_klass, parsed_file, options) ⇒ Generator

Returns a new instance of Generator.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/annotate_rb/model_annotator/annotated_file/generator.rb', line 8

def initialize(file_content, new_annotations, annotation_position, parser_klass, parsed_file, options)
  @annotation_position = annotation_position
  @options = options

  @new_wrapped_annotations = wrapped_content(new_annotations)

  @new_annotations = new_annotations
  @file_content = file_content

  @parser = parser_klass
  @parsed_file = parsed_file
end

Instance Method Details

#generateString

Returns the annotated file content to be written back to a file

Returns:

  • (String)

    Returns the annotated file content to be written back to a file



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

def generate
  # Need to keep `.to_s` for now since the it can be either a String or Symbol
  annotation_write_position = @options[@annotation_position].to_s

  # New method: first remove annotations
  content_without_annotations = if @parsed_file.has_annotations?
    @file_content.sub(@parsed_file.annotations_with_whitespace, "")
  elsif @options[:force]
    @file_content.sub(@parsed_file.annotations_with_whitespace, "")
  else
    @file_content
  end

  # We need to get class start and class end depending on the position
  parsed = @parser.parse(content_without_annotations)

  _content = if %w[after bottom].include?(annotation_write_position)
    content_annotated_after(parsed, content_without_annotations)
  else
    content_annotated_before(parsed, content_without_annotations, annotation_write_position)
  end
end