19
19
#
20
20
# Released under the same license as Ruby. No Support. No Warranty.
21
21
#
22
+
23
+ require_relative './annotate_routes/helpers'
24
+
22
25
module AnnotateRoutes
23
26
PREFIX = '== Route Map' . freeze
24
27
PREFIX_MD = '## Route Map' . freeze
25
28
HEADER_ROW = [ 'Prefix' , 'Verb' , 'URI Pattern' , 'Controller#Action' ] . freeze
26
29
27
- MAGIC_COMMENT_MATCHER = Regexp . new ( /(^#\s *encoding:.*)|(^# coding:.*)|(^# -\* - coding:.*)|(^# -\* - encoding\s ?:.*)|(^#\s *frozen_string_literal:.+)|(^# -\* - frozen_string_literal\s *:.+-\* -)/ ) . freeze
28
-
29
30
class << self
30
31
def do_annotations ( options = { } )
31
32
if routes_file_exist?
32
33
existing_text = File . read ( routes_file )
33
- content , header_position = strip_annotations ( existing_text )
34
+ content , header_position = Helpers . strip_annotations ( existing_text )
34
35
new_content = annotate_routes ( header ( options ) , content , header_position , options )
35
36
new_text = new_content . join ( "\n " )
36
37
@@ -47,7 +48,7 @@ def do_annotations(options = {})
47
48
def remove_annotations ( _options = { } )
48
49
if routes_file_exist?
49
50
existing_text = File . read ( routes_file )
50
- content , header_position = strip_annotations ( existing_text )
51
+ content , header_position = Helpers . strip_annotations ( existing_text )
51
52
new_content = strip_on_removal ( content , header_position )
52
53
new_text = new_content . join ( "\n " )
53
54
if rewrite_contents ( existing_text , new_text )
@@ -73,7 +74,7 @@ def routes_file
73
74
def header ( options = { } )
74
75
routes_map = app_routes_map ( options )
75
76
76
- magic_comments_map , routes_map = extract_magic_comments_from_array ( routes_map )
77
+ magic_comments_map , routes_map = Helpers . extract_magic_comments_from_array ( routes_map )
77
78
78
79
out = [ ]
79
80
@@ -113,35 +114,6 @@ def comment(row = '')
113
114
end
114
115
end
115
116
116
- # TODO: write the method doc using ruby rdoc formats
117
- # This method returns an array of 'real_content' and 'header_position'.
118
- # 'header_position' will either be :before, :after, or
119
- # a number. If the number is > 0, the
120
- # annotation was found somewhere in the
121
- # middle of the file. If the number is
122
- # zero, no annotation was found.
123
- def strip_annotations ( content )
124
- real_content = [ ]
125
- mode = :content
126
- header_position = 0
127
-
128
- content . split ( /\n / , -1 ) . each_with_index do |line , line_number |
129
- if mode == :header && line !~ /\s *#/
130
- mode = :content
131
- real_content << line unless line . blank?
132
- elsif mode == :content
133
- if line =~ /^\s *#\s *== Route.*$/
134
- header_position = line_number + 1 # index start's at 0
135
- mode = :header
136
- else
137
- real_content << line
138
- end
139
- end
140
- end
141
-
142
- real_content_and_header_position ( real_content , header_position )
143
- end
144
-
145
117
def strip_on_removal ( content , header_position )
146
118
if header_position == :before
147
119
content . shift while content . first == ''
@@ -168,7 +140,7 @@ def rewrite_contents(existing_text, new_text)
168
140
end
169
141
170
142
def annotate_routes ( header , content , header_position , options = { } )
171
- magic_comments_map , content = extract_magic_comments_from_array ( content )
143
+ magic_comments_map , content = Helpers . extract_magic_comments_from_array ( content )
172
144
if %w( before top ) . include? ( options [ :position_in_routes ] )
173
145
header = header << '' if content . first != ''
174
146
magic_comments_map << '' if magic_comments_map . any?
@@ -208,24 +180,6 @@ def app_routes_map(options)
208
180
routes_map
209
181
end
210
182
211
- # @param [Array<String>] content
212
- # @return [Array<String>] all found magic comments
213
- # @return [Array<String>] content without magic comments
214
- def extract_magic_comments_from_array ( content_array )
215
- magic_comments = [ ]
216
- new_content = [ ]
217
-
218
- content_array . each do |row |
219
- if row =~ MAGIC_COMMENT_MATCHER
220
- magic_comments << row . strip
221
- else
222
- new_content << row
223
- end
224
- end
225
-
226
- [ magic_comments , new_content ]
227
- end
228
-
229
183
def content ( line , maxs , options = { } )
230
184
return line . rstrip unless options [ :format_markdown ]
231
185
@@ -235,18 +189,5 @@ def content(line, maxs, options = {})
235
189
sprintf ( "%-#{ min_length } .#{ min_length } s" , elem . tr ( '|' , '-' ) )
236
190
end . join ( ' | ' )
237
191
end
238
-
239
- def real_content_and_header_position ( real_content , header_position )
240
- # By default assume the annotation was found in the middle of the file
241
-
242
- # ... unless we have evidence it was at the beginning ...
243
- return real_content , :before if header_position == 1
244
-
245
- # ... or that it was at the end.
246
- return real_content , :after if header_position >= real_content . count
247
-
248
- # and the default
249
- return real_content , header_position
250
- end
251
192
end
252
193
end
0 commit comments