Skip to content

Commit

Permalink
Refactor AnnotateRoutes.header (ctran#714)
Browse files Browse the repository at this point in the history
I refactored `AnnotateRoutes.header` for performance, and add `AnnotateRoutes.comment` to make code DRY.
  • Loading branch information
nard-tech authored and vfonic committed May 8, 2020
1 parent 7cd339a commit 0208fdb
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,37 @@ def header(options = {})
end
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 += ['#']
out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
out << comment
return out if routes_map.size.zero?

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

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

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

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

out
end

def comment(row = '')
if row == ''
'#'
else
"# #{row}"
end
end

# TODO: write the method doc using ruby rdoc formats
# This method returns an array of 'real_content' and 'header_position'.
# 'header_position' will either be :before, :after, or
Expand Down

0 comments on commit 0208fdb

Please sign in to comment.