Skip to content

Commit e3932af

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

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
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
@@ -36,13 +36,10 @@ def do_annotations(options = {})
3636
end
3737
end
3838

39-
def remove_annotations(options={})
39+
def remove_annotations(options = {})
4040
if routes_file_exist?
4141
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])
42+
if RemovalProcessor.update(routes_file, existing_text, options)
4643
puts "Annotations were removed from #{routes_file}."
4744
else
4845
puts "#{routes_file} was not changed (Annotation did not exist)."
@@ -61,32 +58,5 @@ def routes_file_exist?
6158
def routes_file
6259
@routes_rb ||= File.join('config', 'routes.rb')
6360
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
9161
end
9262
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require_relative './helpers'
2+
3+
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
24+
25+
# Make sure we end on a trailing newline.
26+
content << '' unless content.last == ''
27+
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
32+
end
33+
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
46+
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
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)