lib/annotate_rb/model_annotator/annotator.rb in annotaterb-4.0.0 vs lib/annotate_rb/model_annotator/annotator.rb in annotaterb-4.1.0

- old
+ new

@@ -1,33 +1,22 @@ -# require 'bigdecimal' +# frozen_string_literal: true module AnnotateRb module ModelAnnotator class Annotator - # Annotate Models plugin use this header - PREFIX = '== Schema Information'.freeze - PREFIX_MD = '## Schema Information'.freeze - - MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/).freeze - class << self - # We're passed a name of things that might be - # ActiveRecord models. If we can find the class, and - # if its a subclass of ActiveRecord::Base, - # then pass it to the associated block def do_annotations(options = {}) - header = options[:format_markdown] ? PREFIX_MD.dup : PREFIX.dup - version = ActiveRecord::Migrator.current_version rescue 0 - if options[:include_version] && version > 0 - header << "\n# Schema version: #{version}" - end - annotated = [] - model_files_to_annotate = ModelFilesGetter.call(options) - model_files_to_annotate.each do |path, filename| - ModelFileAnnotator.call(annotated, File.join(path, filename), header, options) + model_files_to_consider = ModelFilesGetter.call(options) + + model_files_to_consider.each do |path, filename| + file = File.join(path, filename) + + if AnnotationDecider.new(file, options).annotate? + ModelFileAnnotator.call(annotated, file, options) + end end if annotated.empty? puts 'Model files unchanged.' else @@ -35,37 +24,44 @@ end end def remove_annotations(options = {}) deannotated = [] - deannotated_klass = false - ModelFilesGetter.call(options).each do |file| - file = File.join(file) + + model_files_to_consider = ModelFilesGetter.call(options) + + model_files_to_consider.each do |path, filename| + deannotated_klass = false + file = File.join(path, filename) + begin klass = ModelClassGetter.call(file, options) if klass < ActiveRecord::Base && !klass.abstract_class? model_name = klass.name.underscore table_name = klass.table_name - model_file_name = file - deannotated_klass = true if FileAnnotationRemover.call(model_file_name, options) - patterns = PatternGetter.call(options) + if FileAnnotationRemover.call(file, options) + deannotated_klass = true + end - patterns - .map { |f| FileNameResolver.call(f, model_name, table_name) } - .each do |f| + related_files = RelatedFilesListBuilder.new(file, model_name, table_name, options).build + + related_files.each do |f, _position_key| if File.exist?(f) FileAnnotationRemover.call(f, options) - deannotated_klass = true end end end - deannotated << klass if deannotated_klass + + if deannotated_klass + deannotated << klass + end rescue StandardError => e $stderr.puts "Unable to deannotate #{File.join(file)}: #{e.message}" $stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace] end end + puts "Removed annotations from: #{deannotated.join(', ')}" end end end end