Commit 289621b8 by Shu Fujita Committed by Andrew W. Lee

Make methods private in AnnotateRoutes (#598)

In AnnotateRoutes, there were many public methods that is not used in other classes or modules. Before my extension, I made these methods private and sort them in order of appearance. All tests were passed. Please trace each commits carefully. I didn't nothing special. The commits consist of coordinating code.
parent 4e2946d1
...@@ -25,14 +25,53 @@ module AnnotateRoutes ...@@ -25,14 +25,53 @@ module AnnotateRoutes
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'] HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']
class << self class << self
def content(line, maxs, options = {}) def do_annotations(options = {})
return line.rstrip unless options[:format_markdown] return unless routes_exists?
existing_text = File.read(routes_file)
line.each_with_index.map do |elem, index| if rewrite_contents_with_header(existing_text, header(options), options)
min_length = maxs.map { |arr| arr[index] }.max || 0 puts "#{routes_file} annotated."
end
end
sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) def remove_annotations(_options={})
end.join(' | ') return unless routes_exists?
existing_text = File.read(routes_file)
content, where_header_found = strip_annotations(existing_text)
new_content = strip_on_removal(content, where_header_found)
if rewrite_contents(existing_text, new_content)
puts "Removed annotations from #{routes_file}."
end
end
private
def routes_exists?
routes_exists = File.exists?(routes_file)
puts "Can't find routes.rb" unless routes_exists
routes_exists
end
def routes_file
@routes_rb ||= File.join('config', 'routes.rb')
end
def rewrite_contents_with_header(existing_text, header, options = {})
content, where_header_found = strip_annotations(existing_text)
new_content = annotate_routes(header, content, where_header_found, options)
# Make sure we end on a trailing newline.
new_content << '' unless new_content.last == ''
new_text = new_content.join("\n")
if existing_text == new_text
puts "#{routes_file} unchanged."
false
else
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
end
end end
def header(options = {}) def header(options = {})
...@@ -70,180 +109,143 @@ module AnnotateRoutes ...@@ -70,180 +109,143 @@ module AnnotateRoutes
out out
end end
def do_annotations(options = {}) # TODO: write the method doc using ruby rdoc formats
return unless routes_exists? # where_header_found => This will either be :before, :after, or
existing_text = File.read(routes_file) # a number. If the number is > 0, the
# annotation was found somewhere in the
if rewrite_contents_with_header(existing_text, header(options), options) # middle of the file. If the number is
puts "#{routes_file} annotated." # zero, no annotation was found.
def strip_annotations(content)
real_content = []
mode = :content
header_found_at = 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_found_at = line_number + 1 # index start's at 0
mode = :header
else
real_content << line
end
end
end end
where_header_found(real_content, header_found_at)
end end
def remove_annotations(_options={}) def strip_on_removal(content, where_header_found)
return unless routes_exists? if where_header_found == :before
existing_text = File.read(routes_file) content.shift while content.first == ''
content, where_header_found = strip_annotations(existing_text) elsif where_header_found == :after
new_content = strip_on_removal(content, where_header_found) content.pop while content.last == ''
if rewrite_contents(existing_text, new_content)
puts "Removed annotations from #{routes_file}."
end end
end
end
def self.magic_comment_matcher # TODO: If the user buried it in the middle, we should probably see about
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/) # TODO: preserving a single line of space between the content above and
end # TODO: below...
content
end
# @param [Array<String>] content # @param [String, Array<String>]
# @return [Array<String>] all found magic comments def rewrite_contents(existing_text, new_content)
# @return [Array<String>] content without magic comments # Make sure we end on a trailing newline.
def self.extract_magic_comments_from_array(content_array) new_content << '' unless new_content.last == ''
magic_comments = [] new_text = new_content.join("\n")
new_content = []
content_array.map do |row| if existing_text == new_text
if row =~ magic_comment_matcher puts "#{routes_file} unchanged."
magic_comments << row.strip false
else else
new_content << row File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
end end
end end
[magic_comments, new_content] def annotate_routes(header, content, where_header_found, options = {})
end magic_comments_map, content = extract_magic_comments_from_array(content)
if %w(before top).include?(options[:position_in_routes])
def self.app_routes_map(options) header = header << '' if content.first != ''
routes_map = `rake routes`.chomp("\n").split(/\n/, -1) magic_comments_map << '' if magic_comments_map.any?
new_content = magic_comments_map + header + content
# In old versions of Rake, the first line of output was the cwd. Not so else
# much in newer ones. We ditch that line if it exists, and if not, we # Ensure we have adequate trailing newlines at the end of the file to
# keep the line around. # ensure a blank line separating the content from the annotation.
routes_map.shift if routes_map.first =~ /^\(in \// content << '' unless content.last == ''
# Skip routes which match given regex
# Note: it matches the complete line (route_name, path, controller/action)
if options[:ignore_routes]
routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
end
routes_map
end
def self.routes_file
@routes_rb ||= File.join('config', 'routes.rb')
end
def self.routes_exists? # We're moving something from the top of the file to the bottom, so ditch
routes_exists = File.exists?(routes_file) # the spacer we put in the first time around.
puts "Can't find routes.rb" unless routes_exists content.shift if where_header_found == :before && content.first == ''
routes_exists new_content = magic_comments_map + content + header
end end
# @param [String, Array<String>] new_content
def self.rewrite_contents(existing_text, new_content)
# Make sure we end on a trailing newline.
new_content << '' unless new_content.last == ''
new_text = new_content.join("\n")
if existing_text == new_text
puts "#{routes_file} unchanged."
false
else
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
true
end end
end
def self.rewrite_contents_with_header(existing_text, header, options = {}) def app_routes_map(options)
content, where_header_found = strip_annotations(existing_text) routes_map = `rake routes`.chomp("\n").split(/\n/, -1)
new_content = annotate_routes(header, content, where_header_found, options)
# Make sure we end on a trailing newline. # In old versions of Rake, the first line of output was the cwd. Not so
new_content << '' unless new_content.last == '' # much in newer ones. We ditch that line if it exists, and if not, we
new_text = new_content.join("\n") # keep the line around.
routes_map.shift if routes_map.first =~ /^\(in \//
if existing_text == new_text # Skip routes which match given regex
puts "#{routes_file} unchanged." # Note: it matches the complete line (route_name, path, controller/action)
false if options[:ignore_routes]
else routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ }
File.open(routes_file, 'wb') { |f| f.puts(new_text) } end
true
end
end
def self.annotate_routes(header, content, where_header_found, options = {}) routes_map
magic_comments_map, content = extract_magic_comments_from_array(content)
if %w(before top).include?(options[:position_in_routes])
header = header << '' if content.first != ''
magic_comments_map << '' if magic_comments_map.any?
new_content = magic_comments_map + header + content
else
# Ensure we have adequate trailing newlines at the end of the file to
# ensure a blank line separating the content from the annotation.
content << '' unless content.last == ''
# We're moving something from the top of the file to the bottom, so ditch
# the spacer we put in the first time around.
content.shift if where_header_found == :before && content.first == ''
new_content = magic_comments_map + content + header
end end
new_content # @param [Array<String>] content
end # @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 = []
# TODO: write the method doc using ruby rdoc formats content_array.map do |row|
# where_header_found => This will either be :before, :after, or if row =~ magic_comment_matcher
# a number. If the number is > 0, the magic_comments << row.strip
# annotation was found somewhere in the
# middle of the file. If the number is
# zero, no annotation was found.
def self.strip_annotations(content)
real_content = []
mode = :content
header_found_at = 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_found_at = line_number + 1 # index start's at 0
mode = :header
else else
real_content << line new_content << row
end end
end end
[magic_comments, new_content]
end end
where_header_found(real_content, header_found_at) def content(line, maxs, options = {})
end return line.rstrip unless options[:format_markdown]
line.each_with_index.map do |elem, index|
min_length = maxs.map { |arr| arr[index] }.max || 0
def self.where_header_found(real_content, header_found_at) sprintf("%-#{min_length}.#{min_length}s", elem.tr('|', '-'))
# By default assume the annotation was found in the middle of the file end.join(' | ')
end
# ... unless we have evidence it was at the beginning ... def where_header_found(real_content, header_found_at)
return real_content, :before if header_found_at == 1 # By default assume the annotation was found in the middle of the file
# ... or that it was at the end. # ... unless we have evidence it was at the beginning ...
return real_content, :after if header_found_at >= real_content.count return real_content, :before if header_found_at == 1
# and the default # ... or that it was at the end.
return real_content, header_found_at return real_content, :after if header_found_at >= real_content.count
end
def self.strip_on_removal(content, where_header_found) # and the default
if where_header_found == :before return real_content, header_found_at
content.shift while content.first == ''
elsif where_header_found == :after
content.pop while content.last == ''
end end
# TODO: If the user buried it in the middle, we should probably see about def magic_comment_matcher
# TODO: preserving a single line of space between the content above and Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
# TODO: below... end
content
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