Commit da0adbdb by Shu Fujita Committed by Andrew W. Lee

Refactor AnnotateRoutes.header (#714)

I refactored `AnnotateRoutes.header` for performance, and add `AnnotateRoutes.comment` to make code DRY.
parent 3e3d6056
...@@ -88,10 +88,10 @@ module AnnotateRoutes ...@@ -88,10 +88,10 @@ module AnnotateRoutes
end end
out << '' if magic_comments_map.any? out << '' if magic_comments_map.any?
out += ["# #{options[:wrapper_open]}"] if options[:wrapper_open] out << comment(options[:wrapper_open]) if options[:wrapper_open]
out += ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')] out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
out += ['#'] out << comment
return out if routes_map.size.zero? return out if routes_map.size.zero?
maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) } maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) }
...@@ -99,18 +99,26 @@ module AnnotateRoutes ...@@ -99,18 +99,26 @@ module AnnotateRoutes
if options[:format_markdown] if options[:format_markdown]
max = maxs.map(&:max).compact.max max = maxs.map(&:max).compact.max
out += ["# #{content(HEADER_ROW, maxs, options)}"] out << comment(content(HEADER_ROW, maxs, options))
out += ["# #{content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)}"] out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options))
else else
out += ["# #{content(routes_map[0], maxs, options)}"] out << comment(content(routes_map[0], maxs, options))
end end
out += routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" } out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) }
out += ["# #{options[:wrapper_close]}"] if options[:wrapper_close] out << comment(options[:wrapper_close]) if options[:wrapper_close]
out out
end end
def comment(row = '')
if row == ''
'#'
else
"# #{row}"
end
end
# TODO: write the method doc using ruby rdoc formats # TODO: write the method doc using ruby rdoc formats
# This method returns an array of 'real_content' and 'header_position'. # This method returns an array of 'real_content' and 'header_position'.
# 'header_position' will either be :before, :after, or # 'header_position' will either be :before, :after, or
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment