module AnnotateRoutes
Public Class Methods
do_annotations(options = {})
click to toggle source
# File lib/annotate/annotate_routes.rb, line 26 def do_annotations(options = {}) if routes_file_exist? existing_text = File.read(routes_file) content, header_position = Helpers.strip_annotations(existing_text) new_content = annotate_routes(HeaderGenerator.generate(options), content, header_position, options) new_text = new_content.join("\n") if rewrite_contents(existing_text, new_text) puts "#{routes_file} was annotated." else puts "#{routes_file} was not changed." end else puts "#{routes_file} could not be found." end end
remove_annotations(_options={})
click to toggle source
# File lib/annotate/annotate_routes.rb, line 43 def remove_annotations(_options={}) if routes_file_exist? existing_text = File.read(routes_file) content, header_position = Helpers.strip_annotations(existing_text) new_content = strip_on_removal(content, header_position) new_text = new_content.join("\n") if rewrite_contents(existing_text, new_text) puts "Annotations were removed from #{routes_file}." else puts "#{routes_file} was not changed (Annotation did not exist)." end else puts "#{routes_file} could not be found." end end
Private Class Methods
annotate_routes(header, content, header_position, options = {})
click to toggle source
# File lib/annotate/annotate_routes.rb, line 94 def annotate_routes(header, content, header_position, options = {}) magic_comments_map, content = Helpers.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 header_position == :before && content.first == '' new_content = magic_comments_map + content + header end # Make sure we end on a trailing newline. new_content << '' unless new_content.last == '' new_content end
rewrite_contents(existing_text, new_text)
click to toggle source
# File lib/annotate/annotate_routes.rb, line 85 def rewrite_contents(existing_text, new_text) if existing_text == new_text false else File.open(routes_file, 'wb') { |f| f.puts(new_text) } true end end
routes_file()
click to toggle source
# File lib/annotate/annotate_routes.rb, line 65 def routes_file @routes_rb ||= File.join('config', 'routes.rb') end
routes_file_exist?()
click to toggle source
# File lib/annotate/annotate_routes.rb, line 61 def routes_file_exist? File.exist?(routes_file) end
strip_on_removal(content, header_position)
click to toggle source
# File lib/annotate/annotate_routes.rb, line 69 def strip_on_removal(content, header_position) if header_position == :before content.shift while content.first == '' elsif header_position == :after content.pop while content.last == '' end # Make sure we end on a trailing newline. content << '' unless content.last == '' # TODO: If the user buried it in the middle, we should probably see about # TODO: preserving a single line of space between the content above and # TODO: below... content end