Skip to content

Commit b1f7342

Browse files
committed
Add AnnotateRoutes::Helpers.strip_annotations and .real_content_and_header_position
1 parent 3a3b857 commit b1f7342

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class << self
3131
def do_annotations(options = {})
3232
if routes_file_exist?
3333
existing_text = File.read(routes_file)
34-
content, header_position = strip_annotations(existing_text)
34+
content, header_position = Helpers.strip_annotations(existing_text)
3535
new_content = annotate_routes(header(options), content, header_position, options)
3636
new_text = new_content.join("\n")
3737

@@ -48,7 +48,7 @@ def do_annotations(options = {})
4848
def remove_annotations(_options={})
4949
if routes_file_exist?
5050
existing_text = File.read(routes_file)
51-
content, header_position = strip_annotations(existing_text)
51+
content, header_position = Helpers.strip_annotations(existing_text)
5252
new_content = strip_on_removal(content, header_position)
5353
new_text = new_content.join("\n")
5454
if rewrite_contents(existing_text, new_text)
@@ -114,35 +114,6 @@ def comment(row = '')
114114
end
115115
end
116116

117-
# TODO: write the method doc using ruby rdoc formats
118-
# This method returns an array of 'real_content' and 'header_position'.
119-
# 'header_position' will either be :before, :after, or
120-
# a number. If the number is > 0, the
121-
# annotation was found somewhere in the
122-
# middle of the file. If the number is
123-
# zero, no annotation was found.
124-
def strip_annotations(content)
125-
real_content = []
126-
mode = :content
127-
header_position = 0
128-
129-
content.split(/\n/, -1).each_with_index do |line, line_number|
130-
if mode == :header && line !~ /\s*#/
131-
mode = :content
132-
real_content << line unless line.blank?
133-
elsif mode == :content
134-
if line =~ /^\s*#\s*== Route.*$/
135-
header_position = line_number + 1 # index start's at 0
136-
mode = :header
137-
else
138-
real_content << line
139-
end
140-
end
141-
end
142-
143-
real_content_and_header_position(real_content, header_position)
144-
end
145-
146117
def strip_on_removal(content, header_position)
147118
if header_position == :before
148119
content.shift while content.first == ''
@@ -218,18 +189,5 @@ def content(line, maxs, options = {})
218189
sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
219190
end.join(' | ')
220191
end
221-
222-
def real_content_and_header_position(real_content, header_position)
223-
# By default assume the annotation was found in the middle of the file
224-
225-
# ... unless we have evidence it was at the beginning ...
226-
return real_content, :before if header_position == 1
227-
228-
# ... or that it was at the end.
229-
return real_content, :after if header_position >= real_content.count
230-
231-
# and the default
232-
return real_content, header_position
233-
end
234192
end
235193
end

lib/annotate/annotate_routes/helpers.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@ module Helpers
33
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
44

55
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+
635
# @param [Array<String>] content
736
# @return [Array<String>] all found magic comments
837
# @return [Array<String>] content without magic comments
@@ -20,6 +49,21 @@ def extract_magic_comments_from_array(content_array)
2049

2150
[magic_comments, new_content]
2251
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
2367
end
2468
end
2569
end

0 commit comments

Comments
 (0)