Class: ImproveYourCode::SmellDetectors::BaseDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/improve_your_code/smell_detectors/base_detector.rb

Constant Summary collapse

EXCLUDE_KEY =
'exclude'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:) ⇒ BaseDetector

Returns a new instance of BaseDetector.



14
15
16
17
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 14

def initialize(context:)
  @config = SmellConfiguration.new(self.class.default_config)
  @context = context
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 10

def config
  @config
end

Class Method Details

.configuration_keysObject



108
109
110
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 108

def configuration_keys
  Set.new(default_config.keys.map(&:to_sym))
end

.contextsObject



80
81
82
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 80

def contexts
  %i[def defs]
end

.default_configObject



84
85
86
87
88
89
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 84

def default_config
  {
    SmellConfiguration::ENABLED_KEY => true,
    EXCLUDE_KEY => []
  }
end

.descendantsObject



95
96
97
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 95

def descendants
  @descendants ||= []
end

.inherited(subclass) ⇒ Object



91
92
93
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 91

def inherited(subclass)
  descendants << subclass
end

.smell_typeObject



76
77
78
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 76

def smell_type
  @smell_type ||= name.split(/::/).last
end

.to_detector(detector_name) ⇒ Object



104
105
106
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 104

def to_detector(detector_name)
  SmellDetectors.const_get detector_name
end

.todo_configuration_for(smells) ⇒ Object



19
20
21
22
23
24
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 19

def self.todo_configuration_for(smells)
  default_exclusions = default_config.fetch EXCLUDE_KEY
  exclusions = default_exclusions + smells.map(&:context)

  { smell_type => { EXCLUDE_KEY => exclusions.uniq } }
end

.valid_detector?(detector) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 99

def valid_detector?(detector)
  descendants.map { |descendant| descendant.to_s.split('::').last }
    .include?(detector)
end

Instance Method Details

#runObject



26
27
28
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 26

def run
  sniff
end

#smell_typeObject



30
31
32
# File 'lib/improve_your_code/smell_detectors/base_detector.rb', line 30

def smell_type
  self.class.smell_type
end