Skip to content

Commit a9f7a22

Browse files
nard-techvfonic
authored andcommitted
Add constants MAGIC_COMMENT_MATCHER (ctran#711)
I added `AnnotateModels::MAGIC_COMMENT_MATCHER` and `AnnotateRoutes::MAGIC_COMMENT_MATCHER` instad of `AnnotateModels.magic_comment_matcher` and `AnnotateRoutes.magic_comment_matcher`. I think that they should be constants because the result of Regexp is always fixed.
1 parent 6fcab95 commit a9f7a22

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module AnnotateRoutes
2424
PREFIX_MD = '## Route Map'.freeze
2525
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
2626

27+
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
28+
2729
class << self
2830
def do_annotations(options = {})
2931
return unless routes_exists?
@@ -212,7 +214,7 @@ def extract_magic_comments_from_array(content_array)
212214
new_content = []
213215

214216
content_array.map do |row|
215-
if row =~ magic_comment_matcher
217+
if row =~ MAGIC_COMMENT_MATCHER
216218
magic_comments << row.strip
217219
else
218220
new_content << row
@@ -244,9 +246,5 @@ def real_content_and_header_position(real_content, header_position)
244246
# and the default
245247
return real_content, header_position
246248
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
251249
end
252250
end

0 commit comments

Comments
 (0)