Unverified Commit ce7f22df by Shu Fujita Committed by GitHub

Add methods to AnnotateRoutes::HeaderGenerator and refactor methods (#792)

cf. #790 In order to refactor `AnnoateRoutes`, I added methods to `AnnotateRoutes::HeaderGenerator` and refactor methods. I will add `AnnotateRoutes::AnnotationProcessor` and `AnnotateRoutes::RemovalProcessor` in next PR.
parent 214da4f6
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-04-03 00:51:53 +0900 using RuboCop version 0.68.1.
# on 2020-04-05 20:42:06 +0900 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
......@@ -179,11 +179,11 @@ Lint/ShadowingOuterLocalVariable:
Exclude:
- 'Rakefile'
# Offense count: 21
# Offense count: 22
Metrics/AbcSize:
Max: 145
Max: 103
# Offense count: 8
# Offense count: 7
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
......@@ -194,18 +194,18 @@ Metrics/BlockLength:
Metrics/BlockNesting:
Max: 4
# Offense count: 11
# Offense count: 12
Metrics/CyclomaticComplexity:
Max: 37
Max: 25
# Offense count: 29
# Offense count: 30
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 71
Max: 40
# Offense count: 8
# Offense count: 9
Metrics/PerceivedComplexity:
Max: 42
Max: 28
# Offense count: 1
Naming/AccessorMethodName:
......@@ -331,14 +331,13 @@ Style/HashSyntax:
- 'lib/tasks/annotate_routes.rake'
- 'spec/lib/annotate/annotate_models_spec.rb'
# Offense count: 8
# Offense count: 7
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'Rakefile'
- 'bin/annotate'
- 'lib/annotate/annotate_models.rb'
- 'lib/annotate/annotate_routes/header_generator.rb'
# Offense count: 1
# Cop supports --auto-correct.
......@@ -520,7 +519,7 @@ Style/UnneededPercentQ:
Exclude:
- 'annotate.gemspec'
# Offense count: 375
# Offense count: 377
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
......
......@@ -8,29 +8,31 @@ module AnnotateRoutes
class << self
def generate(options = {})
routes_map = app_routes_map(options)
new(options, routes_map).generate
new(options, routes_map(options)).generate
end
private :new
private
def app_routes_map(options)
routes_map = `rake routes`.chomp("\n").split(/\n/, -1)
def routes_map(options)
result = `rake routes`.chomp("\n").split(/\n/, -1)
# In old versions of Rake, the first line of output was the cwd. Not so
# much in newer ones. We ditch that line if it exists, and if not, we
# keep the line around.
routes_map.shift if routes_map.first =~ %r{^\(in \/}
result.shift if result.first =~ %r{^\(in \/}
ignore_routes = options[:ignore_routes]
regexp_for_ignoring_routes = ignore_routes ? /#{ignore_routes}/ : nil
# Skip routes which match given regex
# Note: it matches the complete line (route_name, path, controller/action)
if options[:ignore_routes]
routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
if regexp_for_ignoring_routes
result.reject { |line| line =~ regexp_for_ignoring_routes }
else
result
end
routes_map
end
end
......@@ -51,13 +53,13 @@ module AnnotateRoutes
out << comment(options[:wrapper_open]) if options[:wrapper_open]
out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
out << comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required
out << comment
return out if contents_without_magic_comments.size.zero?
maxs = [HEADER_ROW.map(&:size)] + contents_without_magic_comments[1..-1].map { |line| line.split.map(&:size) }
if options[:format_markdown]
if markdown?
max = maxs.map(&:max).compact.max
out << comment(content(HEADER_ROW, maxs))
......@@ -66,7 +68,7 @@ module AnnotateRoutes
out << comment(content(contents_without_magic_comments[0], maxs))
end
out += contents_without_magic_comments[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs)) }
out += contents_without_magic_comments[1..-1].map { |line| comment(content(markdown? ? line.split(' ') : line, maxs)) }
out << comment(options[:wrapper_close]) if options[:wrapper_close]
out
......@@ -85,13 +87,27 @@ module AnnotateRoutes
end
def content(line, maxs)
return line.rstrip unless options[:format_markdown]
return line.rstrip unless markdown?
line.each_with_index.map do |elem, index|
min_length = maxs.map { |arr| arr[index] }.max || 0
line.each_with_index.map { |elem, index| format_line_element(elem, maxs, index) }.join(' | ')
end
def format_line_element(elem, maxs, index)
min_length = maxs.map { |arr| arr[index] }.max || 0
format("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
end.join(' | ')
end
def markdown?
options[:format_markdown]
end
def timestamp_if_required(time = Time.now)
if options[:timestamp]
time_formatted = time.strftime('%Y-%m-%d %H:%M')
" (Updated #{time_formatted})"
else
''
end
end
end
end
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