Class: AnnotateRb::ModelAnnotator::AnnotationDecider

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/annotation_decider.rb

Overview

Class that encapsulates the logic to decide whether to annotate a model file and its related files or not.

Constant Summary collapse

SKIP_ANNOTATION_PREFIX =
'# -\*- SkipSchemaAnnotations'

Instance Method Summary collapse

Constructor Details

#initialize(file, options) ⇒ AnnotationDecider

Returns a new instance of AnnotationDecider.



9
10
11
12
# File 'lib/annotate_rb/model_annotator/annotation_decider.rb', line 9

def initialize(file, options)
  @file = file
  @options = options
end

Instance Method Details

#annotate?Boolean

Returns:

  • (Boolean)


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
# File 'lib/annotate_rb/model_annotator/annotation_decider.rb', line 14

def annotate?
  return false if file_contains_skip_annotation

  begin
    klass = ModelClassGetter.call(@file, @options)

    return false unless klass.respond_to?(:descends_from_active_record?)

    # Skip annotating STI classes
    if @options[:exclude_sti_subclasses] && !klass.descends_from_active_record?
      return false
    end

    return false if klass.abstract_class?
    return false unless klass.table_exists?

    return true
  rescue BadModelFileError => e
    unless @options[:ignore_unknown_models]
      warn "Unable to process #{@file}: #{e.message}"
      warn "\t#{e.backtrace.join("\n\t")}" if @options[:trace]
    end
  rescue => e
    warn "Unable to process #{@file}: #{e.message}"
    warn "\t#{e.backtrace.join("\n\t")}" if @options[:trace]
  end

  false
end