-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor by adding AnnotateRoutes::Helpers (#770)
For further refactoring toward better architecture, I moved methods `.strip_annotations`, `.extract_magic_comments_from_array` and `.real_content_and_header_position` from `AnnotateRoutes` to `AnnotateRoutes::Helpers`.
- Loading branch information
Showing
3 changed files
with
82 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module AnnotateRoutes | ||
module Helpers | ||
MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze | ||
|
||
class << self | ||
# TODO: write the method doc using ruby rdoc formats | ||
# This method returns an array of 'real_content' and 'header_position'. | ||
# 'header_position' will either be :before, :after, or | ||
# a number. If the number is > 0, the | ||
# annotation was found somewhere in the | ||
# middle of the file. If the number is | ||
# zero, no annotation was found. | ||
def strip_annotations(content) | ||
real_content = [] | ||
mode = :content | ||
header_position = 0 | ||
|
||
content.split(/\n/, -1).each_with_index do |line, line_number| | ||
if mode == :header && line !~ /\s*#/ | ||
mode = :content | ||
real_content << line unless line.blank? | ||
elsif mode == :content | ||
if line =~ /^\s*#\s*== Route.*$/ | ||
header_position = line_number + 1 # index start's at 0 | ||
mode = :header | ||
else | ||
real_content << line | ||
end | ||
end | ||
end | ||
|
||
real_content_and_header_position(real_content, header_position) | ||
end | ||
|
||
# @param [Array<String>] content | ||
# @return [Array<String>] all found magic comments | ||
# @return [Array<String>] content without magic comments | ||
def extract_magic_comments_from_array(content_array) | ||
magic_comments = [] | ||
new_content = [] | ||
|
||
content_array.each do |row| | ||
if row =~ MAGIC_COMMENT_MATCHER | ||
magic_comments << row.strip | ||
else | ||
new_content << row | ||
end | ||
end | ||
|
||
[magic_comments, new_content] | ||
end | ||
|
||
private | ||
|
||
def real_content_and_header_position(real_content, header_position) | ||
# By default assume the annotation was found in the middle of the file | ||
|
||
# ... unless we have evidence it was at the beginning ... | ||
return real_content, :before if header_position == 1 | ||
|
||
# ... or that it was at the end. | ||
return real_content, :after if header_position >= real_content.count | ||
|
||
# and the default | ||
return real_content, header_position | ||
end | ||
end | ||
end | ||
end |