Skip to content

Commit fa694f1

Browse files
committed
Add AnnotateRoutes::RemovalProcessor
1 parent f434885 commit fa694f1

File tree

4 files changed

+107
-108
lines changed

4 files changed

+107
-108
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
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."
@@ -37,9 +38,10 @@ def do_annotations(options = {})
3738
end
3839

3940
def remove_annotations(options = {})
40-
if routes_file_exist?
41-
existing_text = File.read(routes_file)
42-
if RemovalProcessor.update(routes_file, existing_text, 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
4345
puts "Annotations were removed from #{routes_file}."
4446
else
4547
puts "#{routes_file} was not changed (Annotation did not exist)."
@@ -48,15 +50,5 @@ def remove_annotations(options = {})
4850
puts "#{routes_file} could not be found."
4951
end
5052
end
51-
52-
private
53-
54-
def routes_file_exist?
55-
File.exist?(routes_file)
56-
end
57-
58-
def routes_file
59-
@routes_rb ||= File.join('config', 'routes.rb')
60-
end
6153
end
6254
end
Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,43 @@
1+
require_relative './base_processor'
12
require_relative './helpers'
23
require_relative './header_generator'
34

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

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

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

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

40-
new_content
34+
new_content = magic_comments_map + content + header
4135
end
4236

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

54-
if content_changed
55-
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
56-
true
57-
else
58-
false
59-
end
60-
end
40+
new_content
6141
end
6242
end
6343
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: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,34 @@
1+
require_relative './base_processor'
12
require_relative './helpers'
23

34
module AnnotateRoutes
45
# Class to remove annotations in routes.rb
5-
module RemovalProcessor
6-
class << self
7-
# @param routes_file [String]
8-
# @param existing_text [String]
9-
# @param options [Hash]
10-
def update(routes_file, existing_text, options)
11-
content, header_position = Helpers.strip_annotations(existing_text)
12-
new_content = strip_on_removal(content, header_position)
13-
new_text = new_content.join("\n")
14-
rewrite_contents(routes_file, existing_text, new_text, options)
15-
end
16-
17-
private
18-
19-
def strip_on_removal(content, header_position)
20-
case header_position
21-
when :before
22-
content.shift while content.first == ''
23-
when :after
24-
content.pop while content.last == ''
25-
end
6+
class RemovalProcessor < BaseProcessor
7+
# @return [Boolean]
8+
def update
9+
content, header_position = Helpers.strip_annotations(existing_text)
10+
new_content = strip_on_removal(content, header_position)
11+
new_text = new_content.join("\n")
12+
rewrite_contents(new_text)
13+
end
2614

27-
# Make sure we end on a trailing newline.
28-
content << '' unless content.last == ''
15+
private
2916

30-
# TODO: If the user buried it in the middle, we should probably see about
31-
# TODO: preserving a single line of space between the content above and
32-
# TODO: below...
33-
content
17+
def strip_on_removal(content, header_position)
18+
case header_position
19+
when :before
20+
content.shift while content.first == ''
21+
when :after
22+
content.pop while content.last == ''
3423
end
3524

36-
# @param routes_file [String]
37-
# @param existing_text [String]
38-
# @param new_text [String]
39-
# @param options [Hash]
40-
# @return [Boolean]
41-
def rewrite_contents(routes_file, existing_text, new_text, options)
42-
content_changed = existing_text != new_text
43-
frozen = options[:frozen]
44-
45-
abort "annotate error. #{routes_file} needs to be updated, but annotate was run with `--frozen`." if content_changed && frozen
25+
# Make sure we end on a trailing newline.
26+
content << '' unless content.last == ''
4627

47-
if content_changed
48-
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
49-
true
50-
else
51-
false
52-
end
53-
end
28+
# TODO: If the user buried it in the middle, we should probably see about
29+
# TODO: preserving a single line of space between the content above and
30+
# TODO: below...
31+
content
5432
end
5533
end
5634
end

0 commit comments

Comments
 (0)