@@ -8,29 +8,31 @@ class HeaderGenerator
8
8
9
9
class << self
10
10
def generate ( options = { } )
11
- routes_map = app_routes_map ( options )
12
- new ( options , routes_map ) . generate
11
+ new ( options , routes_map ( options ) ) . generate
13
12
end
14
13
15
14
private :new
16
15
17
16
private
18
17
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 )
21
20
22
21
# In old versions of Rake, the first line of output was the cwd. Not so
23
22
# much in newer ones. We ditch that line if it exists, and if not, we
24
23
# 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
26
28
27
29
# Skip routes which match given regex
28
30
# 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
31
35
end
32
-
33
- routes_map
34
36
end
35
37
end
36
38
@@ -51,13 +53,13 @@ def generate
51
53
52
54
out << comment ( options [ :wrapper_open ] ) if options [ :wrapper_open ]
53
55
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
55
57
out << comment
56
58
return out if contents_without_magic_comments . size . zero?
57
59
58
60
maxs = [ HEADER_ROW . map ( &:size ) ] + contents_without_magic_comments [ 1 ..-1 ] . map { |line | line . split . map ( &:size ) }
59
61
60
- if options [ :format_markdown ]
62
+ if markdown?
61
63
max = maxs . map ( &:max ) . compact . max
62
64
63
65
out << comment ( content ( HEADER_ROW , maxs ) )
@@ -66,7 +68,7 @@ def generate
66
68
out << comment ( content ( contents_without_magic_comments [ 0 ] , maxs ) )
67
69
end
68
70
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 ) ) }
70
72
out << comment ( options [ :wrapper_close ] ) if options [ :wrapper_close ]
71
73
72
74
out
@@ -85,13 +87,27 @@ def comment(row = '')
85
87
end
86
88
87
89
def content ( line , maxs )
88
- return line . rstrip unless options [ :format_markdown ]
90
+ return line . rstrip unless markdown?
89
91
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
92
99
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
95
111
end
96
112
end
97
113
end
0 commit comments