Class: Reek::Smells::SmellDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/smells/smell_detector.rb

Constant Summary collapse

EXCLUDE_KEY =

The name of the config field that lists the names of code contexts that should not be checked. Add this field to the config for each smell that should ignore this code element.

'exclude'
ENABLED_KEY =

The name fo the config field that specifies whether a smell is enabled. Set to true or false.

'enabled'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SmellDetector

Returns a new instance of SmellDetector.



42
43
44
45
# File 'lib/reek/smells/smell_detector.rb', line 42

def initialize(config)
  @enabled = config[ENABLED_KEY]
  @exceptions = config[EXCLUDE_KEY]
end

Class Method Details

.class_nameObject



22
23
24
# File 'lib/reek/smells/smell_detector.rb', line 22

def self.class_name
  self.name.split(/::/)[-1]
end

.contextsObject

:nodoc:



26
27
28
# File 'lib/reek/smells/smell_detector.rb', line 26

def self.contexts      # :nodoc:
  [:defn, :defs]
end

.default_configObject



30
31
32
33
34
35
# File 'lib/reek/smells/smell_detector.rb', line 30

def self.default_config
  {
    ENABLED_KEY => true,
    EXCLUDE_KEY => []
  }
end

.listen(hooks, config) ⇒ Object



37
38
39
40
# File 'lib/reek/smells/smell_detector.rb', line 37

def self.listen(hooks, config)
  detector = new(config[class_name])
  contexts.each { |ctx| hooks[ctx] << detector }
end

Instance Method Details

#examine(context, report) ⇒ Object



47
48
49
50
51
# File 'lib/reek/smells/smell_detector.rb', line 47

def examine(context, report)
  before = report.size
  examine_context(context, report) if @enabled and !exception?(context)
  report.length > before
end

#examine_context(context, report) ⇒ Object



53
54
# File 'lib/reek/smells/smell_detector.rb', line 53

def examine_context(context, report)
end

#exception?(context) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/reek/smells/smell_detector.rb', line 56

def exception?(context)
  return false if @exceptions.nil? or @exceptions.length == 0
  context.matches?(@exceptions)
end

#smell_nameObject



61
62
63
# File 'lib/reek/smells/smell_detector.rb', line 61

def smell_name
  self.class.name_words.join(' ')
end