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