1919#
2020# Released under the same license as Ruby. No Support. No Warranty.
2121#
22+
23+ require_relative './annotate_routes/helpers'
24+
2225module AnnotateRoutes
2326 PREFIX = '== Route Map' . freeze
2427 PREFIX_MD = '## Route Map' . freeze
2528 HEADER_ROW = [ 'Prefix' , 'Verb' , 'URI Pattern' , 'Controller#Action' ] . freeze
2629
27- MAGIC_COMMENT_MATCHER = Regexp . new ( /(^#\s *encoding:.*)|(^# coding:.*)|(^# -\* - coding:.*)|(^# -\* - encoding\s ?:.*)|(^#\s *frozen_string_literal:.+)|(^# -\* - frozen_string_literal\s *:.+-\* -)/ ) . freeze
28-
2930 class << self
3031 def do_annotations ( options = { } )
3132 if routes_file_exist?
3233 existing_text = File . read ( routes_file )
33- content , header_position = strip_annotations ( existing_text )
34+ content , header_position = Helpers . strip_annotations ( existing_text )
3435 new_content = annotate_routes ( header ( options ) , content , header_position , options )
3536 new_text = new_content . join ( "\n " )
3637
@@ -47,7 +48,7 @@ def do_annotations(options = {})
4748 def remove_annotations ( _options = { } )
4849 if routes_file_exist?
4950 existing_text = File . read ( routes_file )
50- content , header_position = strip_annotations ( existing_text )
51+ content , header_position = Helpers . strip_annotations ( existing_text )
5152 new_content = strip_on_removal ( content , header_position )
5253 new_text = new_content . join ( "\n " )
5354 if rewrite_contents ( existing_text , new_text )
@@ -73,7 +74,7 @@ def routes_file
7374 def header ( options = { } )
7475 routes_map = app_routes_map ( options )
7576
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 )
7778
7879 out = [ ]
7980
@@ -113,35 +114,6 @@ def comment(row = '')
113114 end
114115 end
115116
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-
145117 def strip_on_removal ( content , header_position )
146118 if header_position == :before
147119 content . shift while content . first == ''
@@ -168,7 +140,7 @@ def rewrite_contents(existing_text, new_text)
168140 end
169141
170142 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 )
172144 if %w( before top ) . include? ( options [ :position_in_routes ] )
173145 header = header << '' if content . first != ''
174146 magic_comments_map << '' if magic_comments_map . any?
@@ -208,24 +180,6 @@ def app_routes_map(options)
208180 routes_map
209181 end
210182
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-
229183 def content ( line , maxs , options = { } )
230184 return line . rstrip unless options [ :format_markdown ]
231185
@@ -235,18 +189,5 @@ def content(line, maxs, options = {})
235189 sprintf ( "%-#{ min_length } .#{ min_length } s" , elem . tr ( '|' , '-' ) )
236190 end . join ( ' | ' )
237191 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
251192 end
252193end
0 commit comments