Skip to content

Commit 6fcab95

Browse files
nard-techvfonic
authored andcommitted
Rename method and variable of AnnotateRoutes for readability (ctran#709)
Before I commited, the method `AnnotateRoutes.where_header_found` returned an array of `real_content` and `header_found_at`. I fixed the name of method and variable because they did not match. I renamed the method `real_content_and_header_position` and unified related variable names to `header_position`.
1 parent 95fbd35 commit 6fcab95

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def do_annotations(options = {})
3737
def remove_annotations(_options={})
3838
return unless routes_exists?
3939
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)
40+
content, header_position = strip_annotations(existing_text)
41+
new_content = strip_on_removal(content, header_position)
4242
if rewrite_contents(existing_text, new_content)
4343
puts "Removed annotations from #{routes_file}."
4444
end
@@ -58,8 +58,8 @@ def routes_file
5858
end
5959

6060
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)
61+
content, header_position = strip_annotations(existing_text)
62+
new_content = annotate_routes(header, content, header_position, options)
6363

6464
# Make sure we end on a trailing newline.
6565
new_content << '' unless new_content.last == ''
@@ -110,37 +110,38 @@ def header(options = {})
110110
end
111111

112112
# TODO: write the method doc using ruby rdoc formats
113-
# where_header_found => This will either be :before, :after, or
113+
# This method returns an array of 'real_content' and 'header_position'.
114+
# 'header_position' will either be :before, :after, or
114115
# a number. If the number is > 0, the
115116
# annotation was found somewhere in the
116117
# middle of the file. If the number is
117118
# zero, no annotation was found.
118119
def strip_annotations(content)
119120
real_content = []
120121
mode = :content
121-
header_found_at = 0
122+
header_position = 0
122123

123124
content.split(/\n/, -1).each_with_index do |line, line_number|
124125
if mode == :header && line !~ /\s*#/
125126
mode = :content
126127
real_content << line unless line.blank?
127128
elsif mode == :content
128129
if line =~ /^\s*#\s*== Route.*$/
129-
header_found_at = line_number + 1 # index start's at 0
130+
header_position = line_number + 1 # index start's at 0
130131
mode = :header
131132
else
132133
real_content << line
133134
end
134135
end
135136
end
136137

137-
where_header_found(real_content, header_found_at)
138+
real_content_and_header_position(real_content, header_position)
138139
end
139140

140-
def strip_on_removal(content, where_header_found)
141-
if where_header_found == :before
141+
def strip_on_removal(content, header_position)
142+
if header_position == :before
142143
content.shift while content.first == ''
143-
elsif where_header_found == :after
144+
elsif header_position == :after
144145
content.pop while content.last == ''
145146
end
146147

@@ -165,7 +166,7 @@ def rewrite_contents(existing_text, new_content)
165166
end
166167
end
167168

168-
def annotate_routes(header, content, where_header_found, options = {})
169+
def annotate_routes(header, content, header_position, options = {})
169170
magic_comments_map, content = extract_magic_comments_from_array(content)
170171
if %w(before top).include?(options[:position_in_routes])
171172
header = header << '' if content.first != ''
@@ -178,7 +179,7 @@ def annotate_routes(header, content, where_header_found, options = {})
178179

179180
# We're moving something from the top of the file to the bottom, so ditch
180181
# the spacer we put in the first time around.
181-
content.shift if where_header_found == :before && content.first == ''
182+
content.shift if header_position == :before && content.first == ''
182183

183184
new_content = magic_comments_map + content + header
184185
end
@@ -231,17 +232,17 @@ def content(line, maxs, options = {})
231232
end.join(' | ')
232233
end
233234

234-
def where_header_found(real_content, header_found_at)
235+
def real_content_and_header_position(real_content, header_position)
235236
# By default assume the annotation was found in the middle of the file
236237

237238
# ... unless we have evidence it was at the beginning ...
238-
return real_content, :before if header_found_at == 1
239+
return real_content, :before if header_position == 1
239240

240241
# ... or that it was at the end.
241-
return real_content, :after if header_found_at >= real_content.count
242+
return real_content, :after if header_position >= real_content.count
242243

243244
# and the default
244-
return real_content, header_found_at
245+
return real_content, header_position
245246
end
246247

247248
def magic_comment_matcher

0 commit comments

Comments
 (0)