Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AnnotateRoutes.header #714

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,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')})" : '')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In looking at this line, it seems kinda strange and hard to read.

I'm sure there's a lot of this going on in the code base.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drwl I agree with you. I want to try to fix it, but I think that it is better to add test cases before refactor this more.

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