Module: AnnotateRb::RouteAnnotator::Helper
- Defined in:
- lib/annotate_rb/route_annotator/helper.rb
Constant Summary collapse
- MAGIC_COMMENT_MATCHER =
/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/.freeze
Class Method Summary collapse
- .extract_magic_comments_from_array(content_array) ⇒ Array<String>
- .real_content_and_header_position(real_content, header_position) ⇒ Object
- .rewrite_contents(existing_text, new_text) ⇒ Object
- .routes_file ⇒ Object
- .routes_file_exist? ⇒ Boolean
-
.strip_annotations(content) ⇒ Object
TODO: write the method doc using ruby rdoc formats This method returns an array of ‘real_content’ and ‘header_position’.
- .strip_on_removal(content, header_position) ⇒ Object
Class Method Details
.extract_magic_comments_from_array(content_array) ⇒ Array<String>
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/annotate_rb/route_annotator/helper.rb', line 45 def extract_magic_comments_from_array(content_array) magic_comments = [] new_content = [] content_array.each do |row| if MAGIC_COMMENT_MATCHER.match?(row) magic_comments << row.strip else new_content << row end end [magic_comments, new_content] end |
.real_content_and_header_position(real_content, header_position) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/annotate_rb/route_annotator/helper.rb', line 89 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 [real_content, header_position] end |
.rewrite_contents(existing_text, new_text) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/annotate_rb/route_annotator/helper.rb', line 33 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 ⇒ Object
13 14 15 |
# File 'lib/annotate_rb/route_annotator/helper.rb', line 13 def routes_file @routes_rb ||= File.join("config", "routes.rb") end |
.routes_file_exist? ⇒ Boolean
9 10 11 |
# File 'lib/annotate_rb/route_annotator/helper.rb', line 9 def routes_file_exist? File.exist?(routes_file) end |
.strip_annotations(content) ⇒ Object
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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/annotate_rb/route_annotator/helper.rb', line 67 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 /^\s*#\s*== Route.*$/.match?(line) 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 |
.strip_on_removal(content, header_position) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/annotate_rb/route_annotator/helper.rb', line 17 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 |