Skip to content

Commit ff3ef3c

Browse files
committed
Add AnnotateRoutes::RemovalProcessor
1 parent a0a8cc4 commit ff3ef3c

File tree

4 files changed

+119
-98
lines changed

4 files changed

+119
-98
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
# Released under the same license as Ruby. No Support. No Warranty.
1919
#
2020

21-
require_relative './annotate_routes/helpers'
2221
require_relative './annotate_routes/annotation_processor'
22+
require_relative './annotate_routes/removal_processor'
2323

2424
module AnnotateRoutes
2525
class << self
2626
def do_annotations(options = {})
27-
if routes_file_exist?
28-
existing_text = File.read(routes_file)
29-
if AnnotationProcessor.update(routes_file, existing_text, options)
27+
routes_file = File.join('config', 'routes.rb')
28+
processor = AnnotationProcessor.new(options, routes_file)
29+
if processor.routes_file_exist?
30+
if processor.update
3031
puts "#{routes_file} was annotated."
3132
else
3233
puts "#{routes_file} was not changed."
@@ -36,13 +37,11 @@ def do_annotations(options = {})
3637
end
3738
end
3839

39-
def remove_annotations(options={})
40-
if routes_file_exist?
41-
existing_text = File.read(routes_file)
42-
content, header_position = Helpers.strip_annotations(existing_text)
43-
new_content = strip_on_removal(content, header_position)
44-
new_text = new_content.join("\n")
45-
if rewrite_contents(existing_text, new_text, options[:frozen])
40+
def remove_annotations(options = {})
41+
routes_file = File.join('config', 'routes.rb')
42+
processor = RemovalProcessor.new(options, routes_file)
43+
if processor.routes_file_exist?
44+
if processor.update
4645
puts "Annotations were removed from #{routes_file}."
4746
else
4847
puts "#{routes_file} was not changed (Annotation did not exist)."
@@ -51,42 +50,5 @@ def remove_annotations(options={})
5150
puts "#{routes_file} could not be found."
5251
end
5352
end
54-
55-
private
56-
57-
def routes_file_exist?
58-
File.exist?(routes_file)
59-
end
60-
61-
def routes_file
62-
@routes_rb ||= File.join('config', 'routes.rb')
63-
end
64-
65-
def strip_on_removal(content, header_position)
66-
if header_position == :before
67-
content.shift while content.first == ''
68-
elsif header_position == :after
69-
content.pop while content.last == ''
70-
end
71-
72-
# Make sure we end on a trailing newline.
73-
content << '' unless content.last == ''
74-
75-
# TODO: If the user buried it in the middle, we should probably see about
76-
# TODO: preserving a single line of space between the content above and
77-
# TODO: below...
78-
content
79-
end
80-
81-
def rewrite_contents(existing_text, new_text, frozen)
82-
content_changed = (existing_text != new_text)
83-
84-
if content_changed
85-
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if frozen
86-
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
87-
end
88-
89-
content_changed
90-
end
9153
end
9254
end
Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,42 @@
1+
require_relative './base_processor'
12
require_relative './helpers'
23
require_relative './header_generator'
34

45
module AnnotateRoutes
5-
module AnnotationProcessor
6-
class << self
7-
# @param [Boolean]
8-
def update(routes_file, existing_text, options = {})
9-
header = HeaderGenerator.generate(options)
10-
content, header_position = Helpers.strip_annotations(existing_text)
11-
new_content = annotate_routes(header, content, header_position, options)
12-
new_text = new_content.join("\n")
13-
rewrite_contents(routes_file, existing_text, new_text, options)
14-
end
15-
16-
private
17-
18-
def annotate_routes(header, content, header_position, options = {})
19-
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
20-
if %w(before top).include?(options[:position_in_routes])
21-
header = header << '' if content.first != ''
22-
magic_comments_map << '' if magic_comments_map.any?
23-
new_content = magic_comments_map + header + content
24-
else
25-
# Ensure we have adequate trailing newlines at the end of the file to
26-
# ensure a blank line separating the content from the annotation.
27-
content << '' unless content.last == ''
6+
class AnnotationProcessor < BaseProcessor
7+
# @return [Boolean]
8+
def update
9+
header = HeaderGenerator.generate(options)
10+
content, header_position = Helpers.strip_annotations(existing_text)
11+
new_content = annotate_routes(header, content, header_position)
12+
new_text = new_content.join("\n")
13+
rewrite_contents(new_text)
14+
end
2815

29-
# We're moving something from the top of the file to the bottom, so ditch
30-
# the spacer we put in the first time around.
31-
content.shift if header_position == :before && content.first == ''
16+
private
3217

33-
new_content = magic_comments_map + content + header
34-
end
18+
def annotate_routes(header, content, header_position)
19+
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
20+
if %w(before top).include?(options[:position_in_routes])
21+
header = header << '' if content.first != ''
22+
magic_comments_map << '' if magic_comments_map.any?
23+
new_content = magic_comments_map + header + content
24+
else
25+
# Ensure we have adequate trailing newlines at the end of the file to
26+
# ensure a blank line separating the content from the annotation.
27+
content << '' unless content.last == ''
3528

36-
# Make sure we end on a trailing newline.
37-
new_content << '' unless new_content.last == ''
29+
# We're moving something from the top of the file to the bottom, so ditch
30+
# the spacer we put in the first time around.
31+
content.shift if header_position == :before && content.first == ''
3832

39-
new_content
33+
new_content = magic_comments_map + content + header
4034
end
4135

42-
# @param routes_file [String]
43-
# @param existing_text [String]
44-
# @param new_text [String]
45-
# @param options [Hash]
46-
# @return [Boolean]
47-
def rewrite_contents(routes_file, existing_text, new_text, options)
48-
content_changed = existing_text != new_text
49-
frozen = options[:frozen]
50-
51-
if content_changed && frozen
52-
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`."
53-
end
36+
# Make sure we end on a trailing newline.
37+
new_content << '' unless new_content.last == ''
5438

55-
if content_changed
56-
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
57-
true
58-
else
59-
false
60-
end
61-
end
39+
new_content
6240
end
6341
end
6442
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module AnnotateRoutes
2+
class BaseProcessor
3+
def initialize(options, routes_file)
4+
@options = options
5+
@routes_file = routes_file
6+
end
7+
8+
def routes_file_exist?
9+
File.exist?(routes_file)
10+
end
11+
12+
private
13+
14+
attr_reader :options, :routes_file
15+
16+
def existing_text
17+
@existing_text ||= File.read(routes_file)
18+
end
19+
20+
# @param new_text [String]
21+
# @return [Boolean]
22+
def rewrite_contents(new_text)
23+
content_changed = content_changed?(new_text)
24+
25+
if content_changed && frozen?
26+
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`."
27+
end
28+
29+
if content_changed
30+
write(new_text)
31+
true
32+
else
33+
false
34+
end
35+
end
36+
37+
def write(text)
38+
File.open(routes_file, 'wb') { |f| f.puts(text) }
39+
end
40+
41+
def content_changed?(new_text)
42+
!(existing_text == new_text)
43+
end
44+
45+
def frozen?
46+
options[:frozen]
47+
end
48+
end
49+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require_relative './base_processor'
2+
require_relative './helpers'
3+
4+
module AnnotateRoutes
5+
class RemovalProcessor < BaseProcessor
6+
# @return [Boolean]
7+
def update
8+
content, header_position = Helpers.strip_annotations(existing_text)
9+
new_content = strip_on_removal(content, header_position)
10+
new_text = new_content.join("\n")
11+
rewrite_contents(new_text)
12+
end
13+
14+
private
15+
16+
def strip_on_removal(content, header_position)
17+
if header_position == :before
18+
content.shift while content.first == ''
19+
elsif header_position == :after
20+
content.pop while content.last == ''
21+
end
22+
23+
# Make sure we end on a trailing newline.
24+
content << '' unless content.last == ''
25+
26+
# TODO: If the user buried it in the middle, we should probably see about
27+
# TODO: preserving a single line of space between the content above and
28+
# TODO: below...
29+
content
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)