@@ -8,29 +8,31 @@ class HeaderGenerator
88
99 class << self
1010 def generate ( options = { } )
11- routes_map = app_routes_map ( options )
12- new ( options , routes_map ) . generate
11+ new ( options , routes_map ( options ) ) . generate
1312 end
1413
1514 private :new
1615
1716 private
1817
19- def app_routes_map ( options )
20- routes_map = `rake routes` . chomp ( "\n " ) . split ( /\n / , -1 )
18+ def routes_map ( options )
19+ result = `rake routes` . chomp ( "\n " ) . split ( /\n / , -1 )
2120
2221 # In old versions of Rake, the first line of output was the cwd. Not so
2322 # much in newer ones. We ditch that line if it exists, and if not, we
2423 # keep the line around.
25- routes_map . shift if routes_map . first =~ %r{^\( in \/ }
24+ result . shift if result . first =~ %r{^\( in \/ }
25+
26+ ignore_routes = options [ :ignore_routes ]
27+ regexp_for_ignoring_routes = ignore_routes ? /#{ ignore_routes } / : nil
2628
2729 # Skip routes which match given regex
2830 # Note: it matches the complete line (route_name, path, controller/action)
29- if options [ :ignore_routes ]
30- routes_map . reject! { |line | line =~ /#{ options [ :ignore_routes ] } / }
31+ if regexp_for_ignoring_routes
32+ result . reject { |line | line =~ regexp_for_ignoring_routes }
33+ else
34+ result
3135 end
32-
33- routes_map
3436 end
3537 end
3638
@@ -51,13 +53,13 @@ def generate
5153
5254 out << comment ( options [ :wrapper_open ] ) if options [ :wrapper_open ]
5355
54- out << comment ( options [ :format_markdown ] ? PREFIX_MD : PREFIX ) + ( options [ :timestamp ] ? " (Updated #{ Time . now . strftime ( '%Y-%m-%d %H:%M' ) } )" : '' )
56+ out << comment ( markdown? ? PREFIX_MD : PREFIX ) + timestamp_if_required
5557 out << comment
5658 return out if contents_without_magic_comments . size . zero?
5759
5860 maxs = [ HEADER_ROW . map ( &:size ) ] + contents_without_magic_comments [ 1 ..-1 ] . map { |line | line . split . map ( &:size ) }
5961
60- if options [ :format_markdown ]
62+ if markdown?
6163 max = maxs . map ( &:max ) . compact . max
6264
6365 out << comment ( content ( HEADER_ROW , maxs ) )
@@ -66,7 +68,7 @@ def generate
6668 out << comment ( content ( contents_without_magic_comments [ 0 ] , maxs ) )
6769 end
6870
69- out += contents_without_magic_comments [ 1 ..-1 ] . map { |line | comment ( content ( options [ :format_markdown ] ? line . split ( ' ' ) : line , maxs ) ) }
71+ out += contents_without_magic_comments [ 1 ..-1 ] . map { |line | comment ( content ( markdown? ? line . split ( ' ' ) : line , maxs ) ) }
7072 out << comment ( options [ :wrapper_close ] ) if options [ :wrapper_close ]
7173
7274 out
@@ -85,13 +87,27 @@ def comment(row = '')
8587 end
8688
8789 def content ( line , maxs )
88- return line . rstrip unless options [ :format_markdown ]
90+ return line . rstrip unless markdown?
8991
90- line . each_with_index . map do |elem , index |
91- min_length = maxs . map { |arr | arr [ index ] } . max || 0
92+ line . each_with_index . map { |elem , index | format_line_element ( elem , maxs , index ) } . join ( ' | ' )
93+ end
94+
95+ def format_line_element ( elem , maxs , index )
96+ min_length = maxs . map { |arr | arr [ index ] } . max || 0
97+ format ( "%-#{ min_length } .#{ min_length } s" , elem . tr ( '|' , '-' ) )
98+ end
9299
93- format ( "%-#{ min_length } .#{ min_length } s" , elem . tr ( '|' , '-' ) )
94- end . join ( ' | ' )
100+ def markdown?
101+ options [ :format_markdown ]
102+ end
103+
104+ def timestamp_if_required ( time = Time . now )
105+ if options [ :timestamp ]
106+ time_formatted = time . strftime ( '%Y-%m-%d %H:%M' )
107+ " (Updated #{ time_formatted } )"
108+ else
109+ ''
110+ end
95111 end
96112 end
97113end
0 commit comments