Skip to content

Commit 289621b

Browse files
nard-techdrwl
authored andcommitted
Make methods private in AnnotateRoutes (#598)
In AnnotateRoutes, there were many public methods that is not used in other classes or modules. Before my extension, I made these methods private and sort them in order of appearance. All tests were passed. Please trace each commits carefully. I didn't nothing special. The commits consist of coordinating code.
1 parent 4e2946d commit 289621b

File tree

1 file changed

+149
-147
lines changed

1 file changed

+149
-147
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 149 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,53 @@ module AnnotateRoutes
2525
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
2626

2727
class << self
28-
def content(line, maxs, options = {})
29-
return line.rstrip unless options[:format_markdown]
28+
def do_annotations(options = {})
29+
return unless routes_exists?
30+
existing_text = File.read(routes_file)
3031

31-
line.each_with_index.map do |elem, index|
32-
min_length = maxs.map { |arr| arr[index] }.max || 0
32+
if rewrite_contents_with_header(existing_text, header(options), options)
33+
puts "#{routes_file} annotated."
34+
end
35+
end
3336

34-
sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
35-
end.join(' | ')
37+
def remove_annotations(_options={})
38+
return unless routes_exists?
39+
existing_text = File.read(routes_file)
40+
content, where_header_found = strip_annotations(existing_text)
41+
new_content = strip_on_removal(content, where_header_found)
42+
if rewrite_contents(existing_text, new_content)
43+
puts "Removed annotations from #{routes_file}."
44+
end
45+
end
46+
47+
private
48+
49+
def routes_exists?
50+
routes_exists = File.exists?(routes_file)
51+
puts "Can't find routes.rb" unless routes_exists
52+
53+
routes_exists
54+
end
55+
56+
def routes_file
57+
@routes_rb ||= File.join('config', 'routes.rb')
58+
end
59+
60+
def rewrite_contents_with_header(existing_text, header, options = {})
61+
content, where_header_found = strip_annotations(existing_text)
62+
new_content = annotate_routes(header, content, where_header_found, options)
63+
64+
# Make sure we end on a trailing newline.
65+
new_content << '' unless new_content.last == ''
66+
new_text = new_content.join("\n")
67+
68+
if existing_text == new_text
69+
puts "#{routes_file} unchanged."
70+
false
71+
else
72+
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
73+
true
74+
end
3675
end
3776

3877
def header(options = {})
@@ -70,180 +109,143 @@ def header(options = {})
70109
out
71110
end
72111

73-
def do_annotations(options = {})
74-
return unless routes_exists?
75-
existing_text = File.read(routes_file)
76-
77-
if rewrite_contents_with_header(existing_text, header(options), options)
78-
puts "#{routes_file} annotated."
112+
# TODO: write the method doc using ruby rdoc formats
113+
# where_header_found => This will either be :before, :after, or
114+
# a number. If the number is > 0, the
115+
# annotation was found somewhere in the
116+
# middle of the file. If the number is
117+
# zero, no annotation was found.
118+
def strip_annotations(content)
119+
real_content = []
120+
mode = :content
121+
header_found_at = 0
122+
123+
content.split(/\n/, -1).each_with_index do |line, line_number|
124+
if mode == :header && line !~ /\s*#/
125+
mode = :content
126+
real_content << line unless line.blank?
127+
elsif mode == :content
128+
if line =~ /^\s*#\s*== Route.*$/
129+
header_found_at = line_number + 1 # index start's at 0
130+
mode = :header
131+
else
132+
real_content << line
133+
end
134+
end
79135
end
136+
137+
where_header_found(real_content, header_found_at)
80138
end
81139

82-
def remove_annotations(_options={})
83-
return unless routes_exists?
84-
existing_text = File.read(routes_file)
85-
content, where_header_found = strip_annotations(existing_text)
86-
new_content = strip_on_removal(content, where_header_found)
87-
if rewrite_contents(existing_text, new_content)
88-
puts "Removed annotations from #{routes_file}."
140+
def strip_on_removal(content, where_header_found)
141+
if where_header_found == :before
142+
content.shift while content.first == ''
143+
elsif where_header_found == :after
144+
content.pop while content.last == ''
89145
end
90-
end
91-
end
92146

93-
def self.magic_comment_matcher
94-
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
95-
end
147+
# TODO: If the user buried it in the middle, we should probably see about
148+
# TODO: preserving a single line of space between the content above and
149+
# TODO: below...
150+
content
151+
end
96152

97-
# @param [Array<String>] content
98-
# @return [Array<String>] all found magic comments
99-
# @return [Array<String>] content without magic comments
100-
def self.extract_magic_comments_from_array(content_array)
101-
magic_comments = []
102-
new_content = []
153+
# @param [String, Array<String>]
154+
def rewrite_contents(existing_text, new_content)
155+
# Make sure we end on a trailing newline.
156+
new_content << '' unless new_content.last == ''
157+
new_text = new_content.join("\n")
103158

104-
content_array.map do |row|
105-
if row =~ magic_comment_matcher
106-
magic_comments << row.strip
159+
if existing_text == new_text
160+
puts "#{routes_file} unchanged."
161+
false
107162
else
108-
new_content << row
163+
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
164+
true
109165
end
110166
end
111167

112-
[magic_comments, new_content]
113-
end
114-
115-
def self.app_routes_map(options)
116-
routes_map = `rake routes`.chomp("\n").split(/\n/, -1)
117-
118-
# In old versions of Rake, the first line of output was the cwd. Not so
119-
# much in newer ones. We ditch that line if it exists, and if not, we
120-
# keep the line around.
121-
routes_map.shift if routes_map.first =~ /^\(in \//
122-
123-
# Skip routes which match given regex
124-
# Note: it matches the complete line (route_name, path, controller/action)
125-
if options[:ignore_routes]
126-
routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
127-
end
128-
129-
routes_map
130-
end
131-
132-
def self.routes_file
133-
@routes_rb ||= File.join('config', 'routes.rb')
134-
end
168+
def annotate_routes(header, content, where_header_found, options = {})
169+
magic_comments_map, content = extract_magic_comments_from_array(content)
170+
if %w(before top).include?(options[:position_in_routes])
171+
header = header << '' if content.first != ''
172+
magic_comments_map << '' if magic_comments_map.any?
173+
new_content = magic_comments_map + header + content
174+
else
175+
# Ensure we have adequate trailing newlines at the end of the file to
176+
# ensure a blank line separating the content from the annotation.
177+
content << '' unless content.last == ''
135178

136-
def self.routes_exists?
137-
routes_exists = File.exists?(routes_file)
138-
puts "Can't find routes.rb" unless routes_exists
179+
# We're moving something from the top of the file to the bottom, so ditch
180+
# the spacer we put in the first time around.
181+
content.shift if where_header_found == :before && content.first == ''
139182

140-
routes_exists
141-
end
183+
new_content = magic_comments_map + content + header
184+
end
142185

143-
# @param [String, Array<String>]
144-
def self.rewrite_contents(existing_text, new_content)
145-
# Make sure we end on a trailing newline.
146-
new_content << '' unless new_content.last == ''
147-
new_text = new_content.join("\n")
148-
149-
if existing_text == new_text
150-
puts "#{routes_file} unchanged."
151-
false
152-
else
153-
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
154-
true
186+
new_content
155187
end
156-
end
157188

158-
def self.rewrite_contents_with_header(existing_text, header, options = {})
159-
content, where_header_found = strip_annotations(existing_text)
160-
new_content = annotate_routes(header, content, where_header_found, options)
189+
def app_routes_map(options)
190+
routes_map = `rake routes`.chomp("\n").split(/\n/, -1)
161191

162-
# Make sure we end on a trailing newline.
163-
new_content << '' unless new_content.last == ''
164-
new_text = new_content.join("\n")
192+
# In old versions of Rake, the first line of output was the cwd. Not so
193+
# much in newer ones. We ditch that line if it exists, and if not, we
194+
# keep the line around.
195+
routes_map.shift if routes_map.first =~ /^\(in \//
165196

166-
if existing_text == new_text
167-
puts "#{routes_file} unchanged."
168-
false
169-
else
170-
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
171-
true
172-
end
173-
end
197+
# Skip routes which match given regex
198+
# Note: it matches the complete line (route_name, path, controller/action)
199+
if options[:ignore_routes]
200+
routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
201+
end
174202

175-
def self.annotate_routes(header, content, where_header_found, options = {})
176-
magic_comments_map, content = extract_magic_comments_from_array(content)
177-
if %w(before top).include?(options[:position_in_routes])
178-
header = header << '' if content.first != ''
179-
magic_comments_map << '' if magic_comments_map.any?
180-
new_content = magic_comments_map + header + content
181-
else
182-
# Ensure we have adequate trailing newlines at the end of the file to
183-
# ensure a blank line separating the content from the annotation.
184-
content << '' unless content.last == ''
185-
186-
# We're moving something from the top of the file to the bottom, so ditch
187-
# the spacer we put in the first time around.
188-
content.shift if where_header_found == :before && content.first == ''
189-
190-
new_content = magic_comments_map + content + header
203+
routes_map
191204
end
192205

193-
new_content
194-
end
206+
# @param [Array<String>] content
207+
# @return [Array<String>] all found magic comments
208+
# @return [Array<String>] content without magic comments
209+
def extract_magic_comments_from_array(content_array)
210+
magic_comments = []
211+
new_content = []
195212

196-
# TODO: write the method doc using ruby rdoc formats
197-
# where_header_found => This will either be :before, :after, or
198-
# a number. If the number is > 0, the
199-
# annotation was found somewhere in the
200-
# middle of the file. If the number is
201-
# zero, no annotation was found.
202-
def self.strip_annotations(content)
203-
real_content = []
204-
mode = :content
205-
header_found_at = 0
206-
207-
content.split(/\n/, -1).each_with_index do |line, line_number|
208-
if mode == :header && line !~ /\s*#/
209-
mode = :content
210-
real_content << line unless line.blank?
211-
elsif mode == :content
212-
if line =~ /^\s*#\s*== Route.*$/
213-
header_found_at = line_number + 1 # index start's at 0
214-
mode = :header
213+
content_array.map do |row|
214+
if row =~ magic_comment_matcher
215+
magic_comments << row.strip
215216
else
216-
real_content << line
217+
new_content << row
217218
end
218219
end
220+
221+
[magic_comments, new_content]
219222
end
220223

221-
where_header_found(real_content, header_found_at)
222-
end
224+
def content(line, maxs, options = {})
225+
return line.rstrip unless options[:format_markdown]
226+
227+
line.each_with_index.map do |elem, index|
228+
min_length = maxs.map { |arr| arr[index] }.max || 0
223229

224-
def self.where_header_found(real_content, header_found_at)
225-
# By default assume the annotation was found in the middle of the file
230+
sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
231+
end.join(' | ')
232+
end
226233

227-
# ... unless we have evidence it was at the beginning ...
228-
return real_content, :before if header_found_at == 1
234+
def where_header_found(real_content, header_found_at)
235+
# By default assume the annotation was found in the middle of the file
229236

230-
# ... or that it was at the end.
231-
return real_content, :after if header_found_at >= real_content.count
237+
# ... unless we have evidence it was at the beginning ...
238+
return real_content, :before if header_found_at == 1
232239

233-
# and the default
234-
return real_content, header_found_at
235-
end
240+
# ... or that it was at the end.
241+
return real_content, :after if header_found_at >= real_content.count
236242

237-
def self.strip_on_removal(content, where_header_found)
238-
if where_header_found == :before
239-
content.shift while content.first == ''
240-
elsif where_header_found == :after
241-
content.pop while content.last == ''
243+
# and the default
244+
return real_content, header_found_at
242245
end
243246

244-
# TODO: If the user buried it in the middle, we should probably see about
245-
# TODO: preserving a single line of space between the content above and
246-
# TODO: below...
247-
content
247+
def magic_comment_matcher
248+
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
249+
end
248250
end
249251
end

0 commit comments

Comments
 (0)