Module: AnnotateRoutes::Helpers

Defined in:
lib/annotate/annotate_routes/helpers.rb

Constant Summary collapse

MAGIC_COMMENT_MATCHER =
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze

Class Method Summary collapse

Class Method Details

.extract_magic_comments_from_array(content_array) ⇒ Array<String>

Parameters:

  • content (Array<String>)

Returns:

  • (Array<String>)

    all found magic comments

  • (Array<String>)

    content without magic comments



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/annotate/annotate_routes/helpers.rb', line 38

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

.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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/annotate/annotate_routes/helpers.rb', line 13

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