@@ -37,8 +37,8 @@ def do_annotations(options = {})
37
37
def remove_annotations ( _options = { } )
38
38
return unless routes_exists?
39
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 )
40
+ content , header_position = strip_annotations ( existing_text )
41
+ new_content = strip_on_removal ( content , header_position )
42
42
if rewrite_contents ( existing_text , new_content )
43
43
puts "Removed annotations from #{ routes_file } ."
44
44
end
@@ -58,8 +58,8 @@ def routes_file
58
58
end
59
59
60
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 )
61
+ content , header_position = strip_annotations ( existing_text )
62
+ new_content = annotate_routes ( header , content , header_position , options )
63
63
64
64
# Make sure we end on a trailing newline.
65
65
new_content << '' unless new_content . last == ''
@@ -110,37 +110,38 @@ def header(options = {})
110
110
end
111
111
112
112
# 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
114
115
# a number. If the number is > 0, the
115
116
# annotation was found somewhere in the
116
117
# middle of the file. If the number is
117
118
# zero, no annotation was found.
118
119
def strip_annotations ( content )
119
120
real_content = [ ]
120
121
mode = :content
121
- header_found_at = 0
122
+ header_position = 0
122
123
123
124
content . split ( /\n / , -1 ) . each_with_index do |line , line_number |
124
125
if mode == :header && line !~ /\s *#/
125
126
mode = :content
126
127
real_content << line unless line . blank?
127
128
elsif mode == :content
128
129
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
130
131
mode = :header
131
132
else
132
133
real_content << line
133
134
end
134
135
end
135
136
end
136
137
137
- where_header_found ( real_content , header_found_at )
138
+ real_content_and_header_position ( real_content , header_position )
138
139
end
139
140
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
142
143
content . shift while content . first == ''
143
- elsif where_header_found == :after
144
+ elsif header_position == :after
144
145
content . pop while content . last == ''
145
146
end
146
147
@@ -165,7 +166,7 @@ def rewrite_contents(existing_text, new_content)
165
166
end
166
167
end
167
168
168
- def annotate_routes ( header , content , where_header_found , options = { } )
169
+ def annotate_routes ( header , content , header_position , options = { } )
169
170
magic_comments_map , content = extract_magic_comments_from_array ( content )
170
171
if %w( before top ) . include? ( options [ :position_in_routes ] )
171
172
header = header << '' if content . first != ''
@@ -178,7 +179,7 @@ def annotate_routes(header, content, where_header_found, options = {})
178
179
179
180
# We're moving something from the top of the file to the bottom, so ditch
180
181
# 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 == ''
182
183
183
184
new_content = magic_comments_map + content + header
184
185
end
@@ -231,17 +232,17 @@ def content(line, maxs, options = {})
231
232
end . join ( ' | ' )
232
233
end
233
234
234
- def where_header_found ( real_content , header_found_at )
235
+ def real_content_and_header_position ( real_content , header_position )
235
236
# By default assume the annotation was found in the middle of the file
236
237
237
238
# ... 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
239
240
240
241
# ... 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
242
243
243
244
# and the default
244
- return real_content , header_found_at
245
+ return real_content , header_position
245
246
end
246
247
247
248
def magic_comment_matcher
0 commit comments