Skip to content

Commit 0208fdb

Browse files
nard-techvfonic
authored andcommitted
Refactor AnnotateRoutes.header (ctran#714)
I refactored `AnnotateRoutes.header` for performance, and add `AnnotateRoutes.comment` to make code DRY.
1 parent 7cd339a commit 0208fdb

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,37 @@ def header(options = {})
8888
end
8989
out << '' if magic_comments_map.any?
9090

91-
out += ["# #{options[:wrapper_open]}"] if options[:wrapper_open]
91+
out << comment(options[:wrapper_open]) if options[:wrapper_open]
9292

93-
out += ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
94-
out += ['#']
93+
out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
94+
out << comment
9595
return out if routes_map.size.zero?
9696

9797
maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) }
9898

9999
if options[:format_markdown]
100100
max = maxs.map(&:max).compact.max
101101

102-
out += ["# #{content(HEADER_ROW, maxs, options)}"]
103-
out += ["# #{content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)}"]
102+
out << comment(content(HEADER_ROW, maxs, options))
103+
out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options))
104104
else
105-
out += ["# #{content(routes_map[0], maxs, options)}"]
105+
out << comment(content(routes_map[0], maxs, options))
106106
end
107107

108-
out += routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
109-
out += ["# #{options[:wrapper_close]}"] if options[:wrapper_close]
108+
out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) }
109+
out << comment(options[:wrapper_close]) if options[:wrapper_close]
110110

111111
out
112112
end
113113

114+
def comment(row = '')
115+
if row == ''
116+
'#'
117+
else
118+
"# #{row}"
119+
end
120+
end
121+
114122
# TODO: write the method doc using ruby rdoc formats
115123
# This method returns an array of 'real_content' and 'header_position'.
116124
# 'header_position' will either be :before, :after, or

0 commit comments

Comments
 (0)