Sha256: c74ac5fdf6f08abe5124b68911764d0136a1611e00f9f3575140c9cac318f76b

Contents?: true

Size: 1.92 KB

Versions: 15

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  module ModelAnnotator
    class ProjectAnnotationRemover
      def initialize(options)
        @options = options
      end

      def remove_annotations
        project_model_files = model_files

        removal_instructions = project_model_files.map do |path, filename|
          file = File.join(path, filename)

          if AnnotationDecider.new(file, @options).annotate?
            _instructions = build_instructions_for_file(file)
          end
        end.flatten.compact

        deannotated = removal_instructions.map do |instruction|
          if SingleFileAnnotationRemover.call_with_instructions(instruction)
            instruction.file
          end
        rescue => e
          warn "Unable to process #{File.join(instruction.file)}: #{e.message}"
          warn "\t" + e.backtrace.join("\n\t") if @options[:trace]
        end.flatten.compact

        if deannotated.empty?
          puts "Model files unchanged."
        else
          puts "Removed annotations (#{deannotated.length}) from: #{deannotated.join(", ")}"
        end
      end

      private

      def build_instructions_for_file(file)
        klass = ModelClassGetter.call(file, @options)

        instructions = []

        klass.reset_column_information
        model_name = klass.name.underscore
        table_name = klass.table_name

        model_instruction = SingleFileRemoveAnnotationInstruction.new(file, @options)
        instructions << model_instruction

        related_files = RelatedFilesListBuilder.new(file, model_name, table_name, @options).build
        related_file_instructions = related_files.map do |f, _position_key|
          _instruction = SingleFileRemoveAnnotationInstruction.new(f, @options)
        end
        instructions.concat(related_file_instructions)

        instructions
      end

      def model_files
        @model_files ||= ModelFilesGetter.call(@options)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

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