Skip to content

Commit 060e7f2

Browse files
authored
Refactor by adding AnnotateRoutes::Helpers (#770)
For further refactoring toward better architecture, I moved methods `.strip_annotations`, `.extract_magic_comments_from_array` and `.real_content_and_header_position` from `AnnotateRoutes` to `AnnotateRoutes::Helpers`.
1 parent 444cab8 commit 060e7f2

File tree

3 files changed

+82
-70
lines changed

3 files changed

+82
-70
lines changed

.rubocop_todo.yml

+6-4
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-02-13 20:05:34 +0900 using RuboCop version 0.68.1.
3+
# on 2020-03-01 03:15:48 +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
@@ -266,14 +266,16 @@ Style/Dir:
266266
Exclude:
267267
- 'bin/annotate'
268268

269-
# Offense count: 7
269+
# Offense count: 9
270270
Style/Documentation:
271271
Exclude:
272272
- 'spec/**/*'
273273
- 'test/**/*'
274274
- 'lib/annotate.rb'
275275
- 'lib/annotate/active_record_patch.rb'
276276
- 'lib/annotate/annotate_models.rb'
277+
- 'lib/annotate/annotate_routes.rb'
278+
- 'lib/annotate/annotate_routes/helpers.rb'
277279
- 'lib/annotate/version.rb'
278280
- 'lib/generators/annotate/install_generator.rb'
279281
- 'lib/tasks/annotate_models_migrate.rake'
@@ -307,7 +309,7 @@ Style/FormatStringToken:
307309
Exclude:
308310
- 'lib/annotate/annotate_models.rb'
309311

310-
# Offense count: 26
312+
# Offense count: 27
311313
# Cop supports --auto-correct.
312314
# Configuration parameters: EnforcedStyle.
313315
# SupportedStyles: when_needed, always, never
@@ -422,7 +424,7 @@ Style/RedundantParentheses:
422424
# Configuration parameters: AllowMultipleReturnValues.
423425
Style/RedundantReturn:
424426
Exclude:
425-
- 'lib/annotate/annotate_routes.rb'
427+
- 'lib/annotate/annotate_routes/helpers.rb'
426428

427429
# Offense count: 2
428430
# Cop supports --auto-correct.

lib/annotate/annotate_routes.rb

+7-66
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
#
2020
# Released under the same license as Ruby. No Support. No Warranty.
2121
#
22+
23+
require_relative './annotate_routes/helpers'
24+
2225
module AnnotateRoutes
2326
PREFIX = '== Route Map'.freeze
2427
PREFIX_MD = '## Route Map'.freeze
2528
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze
2629

27-
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
28-
2930
class << self
3031
def do_annotations(options = {})
3132
if routes_file_exist?
3233
existing_text = File.read(routes_file)
33-
content, header_position = strip_annotations(existing_text)
34+
content, header_position = Helpers.strip_annotations(existing_text)
3435
new_content = annotate_routes(header(options), content, header_position, options)
3536
new_text = new_content.join("\n")
3637

@@ -47,7 +48,7 @@ def do_annotations(options = {})
4748
def remove_annotations(_options={})
4849
if routes_file_exist?
4950
existing_text = File.read(routes_file)
50-
content, header_position = strip_annotations(existing_text)
51+
content, header_position = Helpers.strip_annotations(existing_text)
5152
new_content = strip_on_removal(content, header_position)
5253
new_text = new_content.join("\n")
5354
if rewrite_contents(existing_text, new_text)
@@ -73,7 +74,7 @@ def routes_file
7374
def header(options = {})
7475
routes_map = app_routes_map(options)
7576

76-
magic_comments_map, routes_map = extract_magic_comments_from_array(routes_map)
77+
magic_comments_map, routes_map = Helpers.extract_magic_comments_from_array(routes_map)
7778

7879
out = []
7980

@@ -113,35 +114,6 @@ def comment(row = '')
113114
end
114115
end
115116

116-
# TODO: write the method doc using ruby rdoc formats
117-
# This method returns an array of 'real_content' and 'header_position'.
118-
# 'header_position' will either be :before, :after, or
119-
# a number. If the number is > 0, the
120-
# annotation was found somewhere in the
121-
# middle of the file. If the number is
122-
# zero, no annotation was found.
123-
def strip_annotations(content)
124-
real_content = []
125-
mode = :content
126-
header_position = 0
127-
128-
content.split(/\n/, -1).each_with_index do |line, line_number|
129-
if mode == :header && line !~ /\s*#/
130-
mode = :content
131-
real_content << line unless line.blank?
132-
elsif mode == :content
133-
if line =~ /^\s*#\s*== Route.*$/
134-
header_position = line_number + 1 # index start's at 0
135-
mode = :header
136-
else
137-
real_content << line
138-
end
139-
end
140-
end
141-
142-
real_content_and_header_position(real_content, header_position)
143-
end
144-
145117
def strip_on_removal(content, header_position)
146118
if header_position == :before
147119
content.shift while content.first == ''
@@ -168,7 +140,7 @@ def rewrite_contents(existing_text, new_text)
168140
end
169141

170142
def annotate_routes(header, content, header_position, options = {})
171-
magic_comments_map, content = extract_magic_comments_from_array(content)
143+
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
172144
if %w(before top).include?(options[:position_in_routes])
173145
header = header << '' if content.first != ''
174146
magic_comments_map << '' if magic_comments_map.any?
@@ -208,24 +180,6 @@ def app_routes_map(options)
208180
routes_map
209181
end
210182

211-
# @param [Array<String>] content
212-
# @return [Array<String>] all found magic comments
213-
# @return [Array<String>] content without magic comments
214-
def extract_magic_comments_from_array(content_array)
215-
magic_comments = []
216-
new_content = []
217-
218-
content_array.each do |row|
219-
if row =~ MAGIC_COMMENT_MATCHER
220-
magic_comments << row.strip
221-
else
222-
new_content << row
223-
end
224-
end
225-
226-
[magic_comments, new_content]
227-
end
228-
229183
def content(line, maxs, options = {})
230184
return line.rstrip unless options[:format_markdown]
231185

@@ -235,18 +189,5 @@ def content(line, maxs, options = {})
235189
sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
236190
end.join(' | ')
237191
end
238-
239-
def real_content_and_header_position(real_content, header_position)
240-
# By default assume the annotation was found in the middle of the file
241-
242-
# ... unless we have evidence it was at the beginning ...
243-
return real_content, :before if header_position == 1
244-
245-
# ... or that it was at the end.
246-
return real_content, :after if header_position >= real_content.count
247-
248-
# and the default
249-
return real_content, header_position
250-
end
251192
end
252193
end
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module AnnotateRoutes
2+
module Helpers
3+
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
4+
5+
class << self
6+
# TODO: write the method doc using ruby rdoc formats
7+
# This method returns an array of 'real_content' and 'header_position'.
8+
# 'header_position' will either be :before, :after, or
9+
# a number. If the number is > 0, the
10+
# annotation was found somewhere in the
11+
# middle of the file. If the number is
12+
# zero, no annotation was found.
13+
def strip_annotations(content)
14+
real_content = []
15+
mode = :content
16+
header_position = 0
17+
18+
content.split(/\n/, -1).each_with_index do |line, line_number|
19+
if mode == :header && line !~ /\s*#/
20+
mode = :content
21+
real_content << line unless line.blank?
22+
elsif mode == :content
23+
if line =~ /^\s*#\s*== Route.*$/
24+
header_position = line_number + 1 # index start's at 0
25+
mode = :header
26+
else
27+
real_content << line
28+
end
29+
end
30+
end
31+
32+
real_content_and_header_position(real_content, header_position)
33+
end
34+
35+
# @param [Array<String>] content
36+
# @return [Array<String>] all found magic comments
37+
# @return [Array<String>] content without magic comments
38+
def extract_magic_comments_from_array(content_array)
39+
magic_comments = []
40+
new_content = []
41+
42+
content_array.each do |row|
43+
if row =~ MAGIC_COMMENT_MATCHER
44+
magic_comments << row.strip
45+
else
46+
new_content << row
47+
end
48+
end
49+
50+
[magic_comments, new_content]
51+
end
52+
53+
private
54+
55+
def real_content_and_header_position(real_content, header_position)
56+
# By default assume the annotation was found in the middle of the file
57+
58+
# ... unless we have evidence it was at the beginning ...
59+
return real_content, :before if header_position == 1
60+
61+
# ... or that it was at the end.
62+
return real_content, :after if header_position >= real_content.count
63+
64+
# and the default
65+
return real_content, header_position
66+
end
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)