-
Notifications
You must be signed in to change notification settings - Fork 6
/
annotation_decider.rb
64 lines (51 loc) · 1.98 KB
/
annotation_decider.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
module AnnotateRb
module ModelAnnotator
# Class that encapsulates the logic to decide whether to annotate a model file and its related files or not.
class AnnotationDecider
SKIP_ANNOTATION_PREFIX = '# -\*- SkipSchemaAnnotations'.freeze
def initialize(file, options)
@file = file
@options = options
end
def annotate?
return false if file_contains_skip_annotation
begin
klass = ModelClassGetter.call(@file, @options)
klass_is_a_class = klass.is_a?(Class)
klass_inherits_active_record_base = klass < ActiveRecord::Base
klass_is_not_abstract = klass.respond_to?(:abstract_class) && !klass.abstract_class?
klass_table_exists = klass.respond_to?(:abstract_class) && klass.table_exists?
not_sure_this_conditional = (!@options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name))
annotate_conditions = [
klass_is_a_class,
klass_inherits_active_record_base,
not_sure_this_conditional,
klass_is_not_abstract,
klass_table_exists
]
to_annotate = annotate_conditions.all?
return to_annotate
rescue BadModelFileError => e
unless @options[:ignore_unknown_models]
$stderr.puts "Unable to process #{@file}: #{e.message}"
$stderr.puts "\t" + e.backtrace.join("\n\t") if @options[:trace]
end
rescue StandardError => e
$stderr.puts "Unable to process #{@file}: #{e.message}"
$stderr.puts "\t" + e.backtrace.join("\n\t") if @options[:trace]
end
false
end
private
def file_contains_skip_annotation
file_string = File.exist?(@file) ? File.read(@file) : ''
if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ file_string
true
else
false
end
end
end
end
end