Skip to content

Commit 3183b9f

Browse files
committed
Add AnnotateRoutes::BaseProcessor
1 parent e3932af commit 3183b9f

File tree

4 files changed

+106
-111
lines changed

4 files changed

+106
-111
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 & 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: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,32 @@
1+
require_relative './base_processor'
12
require_relative './helpers'
23

34
module AnnotateRoutes
4-
module RemovalProcessor
5-
class << self
6-
# @param routes_file [String]
7-
# @param existing_text [String]
8-
# @param options [Hash]
9-
def update(routes_file, existing_text, options)
10-
content, header_position = Helpers.strip_annotations(existing_text)
11-
new_content = strip_on_removal(content, header_position)
12-
new_text = new_content.join("\n")
13-
rewrite_contents(routes_file, existing_text, new_text, options[:frozen])
14-
end
15-
16-
private
17-
18-
def strip_on_removal(content, header_position)
19-
if header_position == :before
20-
content.shift while content.first == ''
21-
elsif header_position == :after
22-
content.pop while content.last == ''
23-
end
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
2413

25-
# Make sure we end on a trailing newline.
26-
content << '' unless content.last == ''
14+
private
2715

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
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 == ''
3221
end
3322

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

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
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
5430
end
5531
end
5632
end

0 commit comments

Comments
 (0)