Skip to content

Commit 135d7e7

Browse files
committed
Use refactor from ctran/annotate_models#793
1 parent a3b0358 commit 135d7e7

File tree

5 files changed

+209
-49
lines changed

5 files changed

+209
-49
lines changed

lib/annotate_rb/route_annotator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ module RouteAnnotator
55
autoload :Annotator, 'annotate_rb/route_annotator/annotator'
66
autoload :Helper, 'annotate_rb/route_annotator/helper'
77
autoload :HeaderGenerator, 'annotate_rb/route_annotator/header_generator'
8+
autoload :BaseProcessor, 'annotate_rb/route_annotator/base_processor'
9+
autoload :AnnotationProcessor, 'annotate_rb/route_annotator/annotation_processor'
10+
autoload :RemovalProcessor, 'annotate_rb/route_annotator/removal_processor'
811
end
912
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# This module provides methods for annotating config/routes.rb.
4+
module AnnotateRb
5+
module RouteAnnotator
6+
# This class is abstract class of classes adding and removing annotation to config/routes.rb.
7+
class AnnotationProcessor < BaseProcessor
8+
9+
# @return [String]
10+
def execute
11+
if routes_file_exist?
12+
if update
13+
"#{routes_file} was annotated."
14+
else
15+
"#{routes_file} was not changed."
16+
end
17+
else
18+
"#{routes_file} could not be found."
19+
end
20+
end
21+
22+
private
23+
24+
def header
25+
@header ||= HeaderGenerator.generate(options)
26+
end
27+
28+
def generate_new_content_array(content, header_position)
29+
magic_comments_map, content = Helper.extract_magic_comments_from_array(content)
30+
if %w(before top).include?(options[:position_in_routes])
31+
new_content_array = []
32+
new_content_array += magic_comments_map
33+
new_content_array << '' if magic_comments_map.any?
34+
new_content_array += header
35+
new_content_array << '' if content.first != ''
36+
new_content_array += content
37+
else
38+
# Ensure we have adequate trailing newlines at the end of the file to
39+
# ensure a blank line separating the content from the annotation.
40+
content << '' unless content.last == ''
41+
42+
# We're moving something from the top of the file to the bottom, so ditch
43+
# the spacer we put in the first time around.
44+
content.shift if header_position == :before && content.first == ''
45+
46+
new_content_array = magic_comments_map + content + header
47+
end
48+
49+
# Make sure we end on a trailing newline.
50+
new_content_array << '' unless new_content_array.last == ''
51+
52+
new_content_array
53+
end
54+
end
55+
end
56+
end

lib/annotate_rb/route_annotator/annotator.rb

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,17 @@ def initialize(options = {})
1818
end
1919

2020
def add_annotations
21-
if Helper.routes_file_exist?
22-
existing_text = File.read(Helper.routes_file)
23-
content, header_position = Helper.strip_annotations(existing_text)
24-
new_content = annotate_routes(HeaderGenerator.generate(@options), content, header_position, @options)
25-
new_text = new_content.join("\n")
26-
27-
if Helper.rewrite_contents(existing_text, new_text)
28-
puts "#{Helper.routes_file} was annotated."
29-
else
30-
puts "#{Helper.routes_file} was not changed."
31-
end
32-
else
33-
puts "#{Helper.routes_file} could not be found."
21+
routes_file = File.join('config', 'routes.rb')
22+
AnnotationProcessor.execute(@options, routes_file).tap do |result|
23+
puts result
3424
end
3525
end
3626

3727
def remove_annotations
38-
if Helper.routes_file_exist?
39-
existing_text = File.read(Helper.routes_file)
40-
content, header_position = Helper.strip_annotations(existing_text)
41-
new_content = Helper.strip_on_removal(content, header_position)
42-
new_text = new_content.join("\n")
43-
if Helper.rewrite_contents(existing_text, new_text)
44-
puts "Annotations were removed from #{Helper.routes_file}."
45-
else
46-
puts "#{Helper.routes_file} was not changed (Annotation did not exist)."
47-
end
48-
else
49-
puts "#{Helper.routes_file} could not be found."
50-
end
51-
end
52-
53-
def annotate_routes(header, content, header_position, options = {})
54-
magic_comments_map, content = Helper.extract_magic_comments_from_array(content)
55-
if %w(before top).include?(options[:position_in_routes])
56-
header = header << '' if content.first != ''
57-
magic_comments_map << '' if magic_comments_map.any?
58-
new_content = magic_comments_map + header + content
59-
else
60-
# Ensure we have adequate trailing newlines at the end of the file to
61-
# ensure a blank line separating the content from the annotation.
62-
content << '' unless content.last == ''
63-
64-
# We're moving something from the top of the file to the bottom, so ditch
65-
# the spacer we put in the first time around.
66-
content.shift if header_position == :before && content.first == ''
67-
68-
new_content = magic_comments_map + content + header
28+
routes_file = File.join('config', 'routes.rb')
29+
RemovalProcessor.execute(@options, routes_file).tap do |result|
30+
puts result
6931
end
70-
71-
# Make sure we end on a trailing newline.
72-
new_content << '' unless new_content.last == ''
73-
74-
new_content
7532
end
7633
end
7734
end
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# frozen_string_literal: true
2+
3+
# This module provides methods for annotating config/routes.rb.
4+
module AnnotateRb
5+
module RouteAnnotator
6+
# This class is abstract class of classes adding and removing annotation to config/routes.rb.
7+
class BaseProcessor
8+
class << self
9+
# @param options [Hash]
10+
# @param routes_file [String]
11+
# @return [String]
12+
def execute(options, routes_file)
13+
new(options, routes_file).execute
14+
end
15+
16+
private :new
17+
end
18+
19+
def initialize(options, routes_file)
20+
@options = options
21+
@routes_file = routes_file
22+
end
23+
24+
# @return [Boolean]
25+
def update
26+
if existing_text == new_text
27+
false
28+
else
29+
write(new_text)
30+
true
31+
end
32+
end
33+
34+
def routes_file_exist?
35+
File.exist?(routes_file)
36+
end
37+
38+
private
39+
40+
attr_reader :options, :routes_file
41+
42+
def generate_new_content_array(_content, _header_position)
43+
raise NoMethodError
44+
end
45+
46+
def existing_text
47+
@existing_text ||= File.read(routes_file)
48+
end
49+
50+
# @return [String]
51+
def new_text
52+
content, header_position = strip_annotations(existing_text)
53+
new_content = generate_new_content_array(content, header_position)
54+
new_content.join("\n")
55+
end
56+
57+
def write(text)
58+
File.open(routes_file, 'wb') { |f| f.puts(text) }
59+
end
60+
61+
# TODO: write the method doc using ruby rdoc formats
62+
# This method returns an array of 'real_content' and 'header_position'.
63+
# 'header_position' will either be :before, :after, or
64+
# a number. If the number is > 0, the
65+
# annotation was found somewhere in the
66+
# middle of the file. If the number is
67+
# zero, no annotation was found.
68+
def strip_annotations(content)
69+
real_content = []
70+
mode = :content
71+
header_position = 0
72+
73+
content.split(/\n/, -1).each_with_index do |line, line_number|
74+
if mode == :header && line !~ /\s*#/
75+
mode = :content
76+
real_content << line unless line.blank?
77+
elsif mode == :content
78+
if line =~ /^\s*#\s*== Route.*$/
79+
header_position = line_number + 1 # index start's at 0
80+
mode = :header
81+
else
82+
real_content << line
83+
end
84+
end
85+
end
86+
87+
real_content_and_header_position(real_content, header_position)
88+
end
89+
90+
def real_content_and_header_position(real_content, header_position)
91+
# By default assume the annotation was found in the middle of the file
92+
93+
# ... unless we have evidence it was at the beginning ...
94+
return real_content, :before if header_position == 1
95+
96+
# ... or that it was at the end.
97+
return real_content, :after if header_position >= real_content.count
98+
99+
# and the default
100+
return real_content, header_position
101+
end
102+
end
103+
end
104+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# This module provides methods for annotating config/routes.rb.
4+
module AnnotateRb
5+
module RouteAnnotator
6+
# This class is abstract class of classes adding and removing annotation to config/routes.rb.
7+
class RemovalProcessor < BaseProcessor
8+
# @return [String]
9+
def execute
10+
if routes_file_exist?
11+
if update
12+
"Annotations were removed from #{routes_file}."
13+
else
14+
"#{routes_file} was not changed (Annotation did not exist)."
15+
end
16+
else
17+
"#{routes_file} could not be found."
18+
end
19+
end
20+
21+
private
22+
23+
def generate_new_content_array(content, header_position)
24+
if header_position == :before
25+
content.shift while content.first == ''
26+
elsif header_position == :after
27+
content.pop while content.last == ''
28+
end
29+
30+
# Make sure we end on a trailing newline.
31+
content << '' unless content.last == ''
32+
33+
# TODO: If the user buried it in the middle, we should probably see about
34+
# TODO: preserving a single line of space between the content above and
35+
# TODO: below...
36+
content
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)