Skip to content

Commit f434885

Browse files
committed
Add AnnotateRoutes::RemovalProcessor
1 parent 78d90f2 commit f434885

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+
# 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
26+
27+
# Make sure we end on a trailing newline.
28+
content << '' unless content.last == ''
29+
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
34+
end
35+
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
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)