Class: AnnotateRb::ModelAnnotator::FileComponents

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

Constant Summary collapse

SKIP_ANNOTATION_STRING =
"# -*- SkipSchemaAnnotations"
SOME_PATTERN =

Unsure what this pattern is

/\A(?<start>\s*).*?\n(?<end>\s*)\z/m

Instance Method Summary collapse

Constructor Details

#initialize(file_content, new_annotations, options) ⇒ FileComponents

Returns a new instance of FileComponents.



9
10
11
12
13
14
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 9

def initialize(file_content, new_annotations, options)
  @file_content = file_content
  @diff = AnnotationDiffGenerator.new(file_content, new_annotations).generate
  @options = options
  @annotation_pattern = AnnotationPatternGenerator.call(options)
end

Instance Method Details

#annotations_changed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 62

def annotations_changed?
  @has_annotations_changed ||= @diff.changed?
end

#current_annotationsObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 66

def current_annotations
  @current_annotations ||=
    if has_annotations?
      # `#has_annotations?` uses a different regex pattern than the one in `@annotation_pattern`,
      # this could lead to unexpected behavior
      @file_content.match(@annotation_pattern).to_s
    else
      ""
    end
end

#current_file_contentObject



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

def current_file_content
  @file_content
end

#has_annotations?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 58

def has_annotations?
  @has_annotations ||= @diff.current_columns.present?
end

#has_skip_string?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 54

def has_skip_string?
  @has_skip_string ||= @file_content.include?(SKIP_ANNOTATION_STRING)
end

#magic_commentsObject



50
51
52
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 50

def magic_comments
  @magic_comments ||= MagicCommentParser.call(@file_content)
end

#pure_file_contentObject



40
41
42
43
44
45
46
47
48
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 40

def pure_file_content
  @pure_file_content ||=
    begin
      content_without_magic_comments = @file_content.gsub(MagicCommentParser::MAGIC_COMMENTS_REGEX, "")
      content_without_annotations = content_without_magic_comments.sub(@annotation_pattern, "")

      content_without_annotations
    end
end

#space_after_annotationObject

TODO: Rename method once it’s clear what this actually does



31
32
33
34
35
36
37
38
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 31

def space_after_annotation
  return @space_after_annotation if defined?(@space_after_annotation)

  match = current_annotations.match(SOME_PATTERN)
  @space_after_annotation = if match
    match[:end]
  end
end

#space_before_annotationObject

TODO: Rename method once it’s clear what this actually does



21
22
23
24
25
26
27
28
# File 'lib/annotate_rb/model_annotator/file_components.rb', line 21

def space_before_annotation
  return @space_before_annotation if defined?(@space_before_annotation)

  match = current_annotations.match(SOME_PATTERN)
  @space_before_annotation = if match
    match[:start]
  end
end