Skip to content

Commit ce7f22d

Browse files
authored
Add methods to AnnotateRoutes::HeaderGenerator and refactor methods (ctran#792)
cf. ctran#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.
1 parent 214da4f commit ce7f22d

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

Diff for: .rubocop_todo.yml

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-04-03 00:51:53 +0900 using RuboCop version 0.68.1.
3+
# on 2020-04-05 20:42:06 +0900 using RuboCop version 0.68.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -179,11 +179,11 @@ Lint/ShadowingOuterLocalVariable:
179179
Exclude:
180180
- 'Rakefile'
181181

182-
# Offense count: 21
182+
# Offense count: 22
183183
Metrics/AbcSize:
184-
Max: 145
184+
Max: 103
185185

186-
# Offense count: 8
186+
# Offense count: 7
187187
# Configuration parameters: CountComments, ExcludedMethods.
188188
# ExcludedMethods: refine
189189
Metrics/BlockLength:
@@ -194,18 +194,18 @@ Metrics/BlockLength:
194194
Metrics/BlockNesting:
195195
Max: 4
196196

197-
# Offense count: 11
197+
# Offense count: 12
198198
Metrics/CyclomaticComplexity:
199-
Max: 37
199+
Max: 25
200200

201-
# Offense count: 29
201+
# Offense count: 30
202202
# Configuration parameters: CountComments, ExcludedMethods.
203203
Metrics/MethodLength:
204-
Max: 71
204+
Max: 40
205205

206-
# Offense count: 8
206+
# Offense count: 9
207207
Metrics/PerceivedComplexity:
208-
Max: 42
208+
Max: 28
209209

210210
# Offense count: 1
211211
Naming/AccessorMethodName:
@@ -331,14 +331,13 @@ Style/HashSyntax:
331331
- 'lib/tasks/annotate_routes.rake'
332332
- 'spec/lib/annotate/annotate_models_spec.rb'
333333

334-
# Offense count: 8
334+
# Offense count: 7
335335
# Cop supports --auto-correct.
336336
Style/IfUnlessModifier:
337337
Exclude:
338338
- 'Rakefile'
339339
- 'bin/annotate'
340340
- 'lib/annotate/annotate_models.rb'
341-
- 'lib/annotate/annotate_routes/header_generator.rb'
342341

343342
# Offense count: 1
344343
# Cop supports --auto-correct.
@@ -520,7 +519,7 @@ Style/UnneededPercentQ:
520519
Exclude:
521520
- 'annotate.gemspec'
522521

523-
# Offense count: 375
522+
# Offense count: 377
524523
# Cop supports --auto-correct.
525524
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
526525
# URISchemes: http, https

Diff for: lib/annotate/annotate_routes/header_generator.rb

+33-17
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,31 @@ class HeaderGenerator
88

99
class << self
1010
def generate(options = {})
11-
routes_map = app_routes_map(options)
12-
new(options, routes_map).generate
11+
new(options, routes_map(options)).generate
1312
end
1413

1514
private :new
1615

1716
private
1817

19-
def app_routes_map(options)
20-
routes_map = `rake routes`.chomp("\n").split(/\n/, -1)
18+
def routes_map(options)
19+
result = `rake routes`.chomp("\n").split(/\n/, -1)
2120

2221
# In old versions of Rake, the first line of output was the cwd. Not so
2322
# much in newer ones. We ditch that line if it exists, and if not, we
2423
# keep the line around.
25-
routes_map.shift if routes_map.first =~ %r{^\(in \/}
24+
result.shift if result.first =~ %r{^\(in \/}
25+
26+
ignore_routes = options[:ignore_routes]
27+
regexp_for_ignoring_routes = ignore_routes ? /#{ignore_routes}/ : nil
2628

2729
# Skip routes which match given regex
2830
# Note: it matches the complete line (route_name, path, controller/action)
29-
if options[:ignore_routes]
30-
routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
31+
if regexp_for_ignoring_routes
32+
result.reject { |line| line =~ regexp_for_ignoring_routes }
33+
else
34+
result
3135
end
32-
33-
routes_map
3436
end
3537
end
3638

@@ -51,13 +53,13 @@ def generate
5153

5254
out << comment(options[:wrapper_open]) if options[:wrapper_open]
5355

54-
out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
56+
out << comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required
5557
out << comment
5658
return out if contents_without_magic_comments.size.zero?
5759

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

60-
if options[:format_markdown]
62+
if markdown?
6163
max = maxs.map(&:max).compact.max
6264

6365
out << comment(content(HEADER_ROW, maxs))
@@ -66,7 +68,7 @@ def generate
6668
out << comment(content(contents_without_magic_comments[0], maxs))
6769
end
6870

69-
out += contents_without_magic_comments[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs)) }
71+
out += contents_without_magic_comments[1..-1].map { |line| comment(content(markdown? ? line.split(' ') : line, maxs)) }
7072
out << comment(options[:wrapper_close]) if options[:wrapper_close]
7173

7274
out
@@ -85,13 +87,27 @@ def comment(row = '')
8587
end
8688

8789
def content(line, maxs)
88-
return line.rstrip unless options[:format_markdown]
90+
return line.rstrip unless markdown?
8991

90-
line.each_with_index.map do |elem, index|
91-
min_length = maxs.map { |arr| arr[index] }.max || 0
92+
line.each_with_index.map { |elem, index| format_line_element(elem, maxs, index) }.join(' | ')
93+
end
94+
95+
def format_line_element(elem, maxs, index)
96+
min_length = maxs.map { |arr| arr[index] }.max || 0
97+
format("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
98+
end
9299

93-
format("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
94-
end.join(' | ')
100+
def markdown?
101+
options[:format_markdown]
102+
end
103+
104+
def timestamp_if_required(time = Time.now)
105+
if options[:timestamp]
106+
time_formatted = time.strftime('%Y-%m-%d %H:%M')
107+
" (Updated #{time_formatted})"
108+
else
109+
''
110+
end
95111
end
96112
end
97113
end

0 commit comments

Comments
 (0)