Skip to content

Commit 3a3b857

Browse files
committed
Add AnnotateRoutes::Helpers.extract_magic_comments_from_array
1 parent dce2ac6 commit 3a3b857

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lib/annotate/annotate_routes.rb

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
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?
@@ -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

@@ -168,7 +169,7 @@ def rewrite_contents(existing_text, new_text)
168169
end
169170

170171
def annotate_routes(header, content, header_position, options = {})
171-
magic_comments_map, content = extract_magic_comments_from_array(content)
172+
magic_comments_map, content = Helpers.extract_magic_comments_from_array(content)
172173
if %w(before top).include?(options[:position_in_routes])
173174
header = header << '' if content.first != ''
174175
magic_comments_map << '' if magic_comments_map.any?
@@ -208,24 +209,6 @@ def app_routes_map(options)
208209
routes_map
209210
end
210211

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-
229212
def content(line, maxs, options = {})
230213
return line.rstrip unless options[:format_markdown]
231214

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
# @param [Array<String>] content
7+
# @return [Array<String>] all found magic comments
8+
# @return [Array<String>] content without magic comments
9+
def extract_magic_comments_from_array(content_array)
10+
magic_comments = []
11+
new_content = []
12+
13+
content_array.each do |row|
14+
if row =~ MAGIC_COMMENT_MATCHER
15+
magic_comments << row.strip
16+
else
17+
new_content << row
18+
end
19+
end
20+
21+
[magic_comments, new_content]
22+
end
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)