Sha256: 17fb0724911d27bb2b12c45a816b4057f9350d81208c28910e4b62e893168f27

Contents?: true

Size: 1.82 KB

Versions: 15

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

# This module provides methods for annotating config/routes.rb.
module AnnotateRb
  module RouteAnnotator
    # This class is abstract class of classes adding and removing annotation to config/routes.rb.
    class AnnotationProcessor < BaseProcessor
      # @return [String]
      def execute
        if routes_file_exist?
          if update
            "#{routes_file} was annotated."
          else
            "#{routes_file} was not changed."
          end
        else
          "#{routes_file} could not be found."
        end
      end

      private

      def header
        @header ||= HeaderGenerator.generate(options)
      end

      def generate_new_content_array(content, header_position)
        magic_comments_map, content = Helper.extract_magic_comments_from_array(content)
        if %w[before top].include?(options[:position_in_routes])
          new_content_array = []
          new_content_array += magic_comments_map
          new_content_array << "" if magic_comments_map.any?
          new_content_array += header
          new_content_array << "" if content.first != ""
          new_content_array += 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_array = magic_comments_map + content + header
        end

        # Make sure we end on a trailing newline.
        new_content_array << "" unless new_content_array.last == ""

        new_content_array
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
annotaterb-4.13.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.12.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.11.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.10.2 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.10.1 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.10.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.9.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.7.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.6.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.5.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.4.1 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.4.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.3.1 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.3.0 lib/annotate_rb/route_annotator/annotation_processor.rb
annotaterb-4.2.0 lib/annotate_rb/route_annotator/annotation_processor.rb