Skip to content

Commit 6e919fb

Browse files
authored
Refactor AnnotatedFile classes (#91)
This PR does a small refactor to tidy up `AnnotatedFile::Generator` and `AnnotatedFile::Updater`.
1 parent 8cfc3ed commit 6e919fb

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

lib/annotate_rb/model_annotator/annotated_file/generator.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def initialize(file_content, new_annotations, annotation_position, options)
1717
@parsed_file = FileParser::ParsedFile.new(@file_content, @new_annotations, options).parse
1818
end
1919

20+
# @return [String] Returns the annotated file content to be written back to a file
2021
def generate
2122
# Need to keep `.to_s` for now since the it can be either a String or Symbol
2223
annotation_write_position = @options[@annotation_position].to_s
@@ -33,33 +34,43 @@ def generate
3334
# We need to get class start and class end depending on the position
3435
parsed = FileParser::CustomParser.new(content_without_annotations, "", 0).tap(&:parse)
3536

36-
same_write_position = @parsed_file.has_annotations? && @parsed_file.annotation_position.to_s == annotation_write_position
37+
_content = if %w[after bottom].include?(annotation_write_position)
38+
content_annotated_after(parsed, content_without_annotations)
39+
else
40+
content_annotated_before(parsed, content_without_annotations, annotation_write_position)
41+
end
42+
end
43+
44+
private
45+
46+
def content_annotated_before(parsed, content_without_annotations, write_position)
47+
same_write_position = @parsed_file.has_annotations? && @parsed_file.annotation_position.to_s == write_position
3748

3849
# Could error if there's no class or module declaration
3950
_constant_name, line_number_before = parsed.starts.first
51+
4052
content_with_annotations_written_before = []
4153
content_with_annotations_written_before << content_without_annotations.lines[0...line_number_before]
4254
content_with_annotations_written_before << $/ if @parsed_file.has_leading_whitespace? && same_write_position
4355
content_with_annotations_written_before << @new_wrapped_annotations.lines
4456
content_with_annotations_written_before << $/ if @parsed_file.has_trailing_whitespace? && same_write_position
4557
content_with_annotations_written_before << content_without_annotations.lines[line_number_before..]
4658

59+
content_with_annotations_written_before.join
60+
end
61+
62+
def content_annotated_after(parsed, content_without_annotations)
4763
_constant_name, line_number_after = parsed.ends.last
64+
4865
content_with_annotations_written_after = []
4966
content_with_annotations_written_after << content_without_annotations.lines[0..line_number_after]
5067
content_with_annotations_written_after << $/
5168
content_with_annotations_written_after << @new_wrapped_annotations.lines
5269
content_with_annotations_written_after << content_without_annotations.lines[(line_number_after + 1)..]
5370

54-
_content = if %w[after bottom].include?(annotation_write_position)
55-
content_with_annotations_written_after.join
56-
else
57-
content_with_annotations_written_before.join
58-
end
71+
content_with_annotations_written_after.join
5972
end
6073

61-
private
62-
6374
def wrapped_content(content)
6475
wrapper_open = if @options[:wrapper_open]
6576
"# #{@options[:wrapper_open]}\n"

lib/annotate_rb/model_annotator/annotated_file/updater.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def initialize(file_content, new_annotations, _annotation_position, options)
1414
@parsed_file = FileParser::ParsedFile.new(@file_content, @new_annotations, options).parse
1515
end
1616

17+
# @return [String] Returns the annotated file content to be written back to a file
1718
def update
1819
return "" if !@parsed_file.has_annotations?
1920

lib/annotate_rb/model_annotator/single_file_annotation_remover.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ def call(file_name, options = Options.from({}))
1818
warn "Unable to process #{file_name}: #{e.message}"
1919
warn "\t" + e.backtrace.join("\n\t") if @options[:trace]
2020
return false
21-
# rescue FileParser::AnnotationFinder::NoAnnotationFound => _e
22-
# return false # False since there's no annotations to remove
2321
end
2422

2523
return false if !parsed_file.has_annotations?
26-
2724
return false if parsed_file.has_skip_string?
2825

2926
updated_file_content = old_content.sub(parsed_file.annotations_with_whitespace, "")

lib/annotate_rb/model_annotator/single_file_annotator.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def call(file_name, annotation, annotation_position, options)
3131
warn "Unable to process #{file_name}: #{e.message}"
3232
warn "\t" + e.backtrace.join("\n\t") if @options[:trace]
3333
return false
34-
# rescue FileParser::AnnotationFinder::NoAnnotationFound => _e
35-
# return false # False since there's no annotations to remove
3634
end
3735

3836
return false if parsed_file.has_skip_string?

0 commit comments

Comments
 (0)