Skip to content

Commit 02c6059

Browse files
committed
Merge branch 'develop' into feature/refactor_annotate_routes/extract_magic_comments_from_array
2 parents 4f6a2f9 + da0adbd commit 02c6059

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

lib/annotate/annotate_models.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ module AnnotateModels
8282
}
8383
}.freeze
8484

85+
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/).freeze
86+
8587
class << self
8688
def annotate_pattern(options = {})
8789
if options[:wrapper_open]
@@ -537,7 +539,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
537539
# need to insert it in correct position
538540
if old_annotation.empty? || options[:force]
539541
magic_comments_block = magic_comments_as_string(old_content)
540-
old_content.gsub!(magic_comment_matcher, '')
542+
old_content.gsub!(MAGIC_COMMENT_MATCHER, '')
541543
old_content.sub!(annotate_pattern(options), '')
542544

543545
new_content = if %w(after bottom).include?(options[position].to_s)
@@ -561,12 +563,8 @@ def annotate_one_file(file_name, info_block, position, options = {})
561563
true
562564
end
563565

564-
def magic_comment_matcher
565-
Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/)
566-
end
567-
568566
def magic_comments_as_string(content)
569-
magic_comments = content.scan(magic_comment_matcher).flatten.compact
567+
magic_comments = content.scan(MAGIC_COMMENT_MATCHER).flatten.compact
570568

571569
if magic_comments.any?
572570
magic_comments.join

lib/annotate/annotate_routes.rb

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
module AnnotateRoutes
2323
PREFIX = '== Route Map'.freeze
2424
PREFIX_MD = '## Route Map'.freeze
25-
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
25+
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze
26+
27+
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
2628

2729
class << self
2830
def do_annotations(options = {})
@@ -86,29 +88,37 @@ def header(options = {})
8688
end
8789
out << '' if magic_comments_map.any?
8890

89-
out += ["# #{options[:wrapper_open]}"] if options[:wrapper_open]
91+
out << comment(options[:wrapper_open]) if options[:wrapper_open]
9092

91-
out += ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')]
92-
out += ['#']
93+
out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')
94+
out << comment
9395
return out if routes_map.size.zero?
9496

9597
maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) }
9698

9799
if options[:format_markdown]
98100
max = maxs.map(&:max).compact.max
99101

100-
out += ["# #{content(HEADER_ROW, maxs, options)}"]
101-
out += ["# #{content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)}"]
102+
out << comment(content(HEADER_ROW, maxs, options))
103+
out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options))
102104
else
103-
out += ["# #{content(routes_map[0], maxs, options)}"]
105+
out << comment(content(routes_map[0], maxs, options))
104106
end
105107

106-
out += routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" }
107-
out += ["# #{options[:wrapper_close]}"] if options[:wrapper_close]
108+
out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) }
109+
out << comment(options[:wrapper_close]) if options[:wrapper_close]
108110

109111
out
110112
end
111113

114+
def comment(row = '')
115+
if row == ''
116+
'#'
117+
else
118+
"# #{row}"
119+
end
120+
end
121+
112122
# TODO: write the method doc using ruby rdoc formats
113123
# This method returns an array of 'real_content' and 'header_position'.
114124
# 'header_position' will either be :before, :after, or
@@ -212,7 +222,7 @@ def extract_magic_comments_from_array(content_array)
212222
new_content = []
213223

214224
content_array.each do |row|
215-
if row =~ magic_comment_matcher
225+
if row =~ MAGIC_COMMENT_MATCHER
216226
magic_comments << row.strip
217227
else
218228
new_content << row
@@ -244,9 +254,5 @@ def real_content_and_header_position(real_content, header_position)
244254
# and the default
245255
return real_content, header_position
246256
end
247-
248-
def magic_comment_matcher
249-
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
250-
end
251257
end
252258
end

0 commit comments

Comments
 (0)