Commit 2213ece2 by Shu Fujita Committed by Andrew W. Lee

Add constants MAGIC_COMMENT_MATCHER (#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.
parent ee88f0fd
...@@ -82,6 +82,8 @@ module AnnotateModels ...@@ -82,6 +82,8 @@ module AnnotateModels
} }
}.freeze }.freeze
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
class << self class << self
def annotate_pattern(options = {}) def annotate_pattern(options = {})
if options[:wrapper_open] if options[:wrapper_open]
...@@ -537,7 +539,7 @@ module AnnotateModels ...@@ -537,7 +539,7 @@ module AnnotateModels
# need to insert it in correct position # need to insert it in correct position
if old_annotation.empty? || options[:force] if old_annotation.empty? || options[:force]
magic_comments_block = magic_comments_as_string(old_content) magic_comments_block = magic_comments_as_string(old_content)
old_content.gsub!(magic_comment_matcher, '') old_content.gsub!(MAGIC_COMMENT_MATCHER, '')
old_content.sub!(annotate_pattern(options), '') old_content.sub!(annotate_pattern(options), '')
new_content = if %w(after bottom).include?(options[position].to_s) new_content = if %w(after bottom).include?(options[position].to_s)
...@@ -561,12 +563,8 @@ module AnnotateModels ...@@ -561,12 +563,8 @@ module AnnotateModels
true true
end end
def 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))/)
end
def magic_comments_as_string(content) def magic_comments_as_string(content)
magic_comments = content.scan(magic_comment_matcher).flatten.compact magic_comments = content.scan(MAGIC_COMMENT_MATCHER).flatten.compact
if magic_comments.any? if magic_comments.any?
magic_comments.join magic_comments.join
......
...@@ -24,6 +24,8 @@ module AnnotateRoutes ...@@ -24,6 +24,8 @@ module AnnotateRoutes
PREFIX_MD = '## Route Map'.freeze PREFIX_MD = '## Route Map'.freeze
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'] HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze
class << self class << self
def do_annotations(options = {}) def do_annotations(options = {})
return unless routes_exists? return unless routes_exists?
...@@ -212,7 +214,7 @@ module AnnotateRoutes ...@@ -212,7 +214,7 @@ module AnnotateRoutes
new_content = [] new_content = []
content_array.map do |row| content_array.map do |row|
if row =~ magic_comment_matcher if row =~ MAGIC_COMMENT_MATCHER
magic_comments << row.strip magic_comments << row.strip
else else
new_content << row new_content << row
...@@ -244,9 +246,5 @@ module AnnotateRoutes ...@@ -244,9 +246,5 @@ module AnnotateRoutes
# and the default # and the default
return real_content, header_position return real_content, header_position
end end
def magic_comment_matcher
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment