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'
DEFAULT_EXCLUDE_SET =

The default value for the EXCLUDE_KEY if it isn’t specified in any configuration file.

[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = self.class.default_config) ⇒ SmellDetector

Returns a new instance of SmellDetector.



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

def initialize(config = self.class.default_config)
  @config = SmellConfiguration.new(config)
  @smells_found = Set.new
  @masked = false
end

Class Method Details

.class_nameObject



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

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

.contextsObject

:nodoc:



29
30
31
# File 'lib/reek/smells/smell_detector.rb', line 29

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

.create(config) ⇒ Object



40
41
42
# File 'lib/reek/smells/smell_detector.rb', line 40

def create(config)
  new(config[class_name])
end

.default_configObject



33
34
35
36
37
38
# File 'lib/reek/smells/smell_detector.rb', line 33

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

.listen(hooks, config) ⇒ Object



44
45
46
47
48
# File 'lib/reek/smells/smell_detector.rb', line 44

def listen(hooks, config)
  detector = create(config)
  detector.listen_to(hooks)
  detector
end

Instance Method Details

#configure(config) ⇒ Object



66
67
68
69
70
# File 'lib/reek/smells/smell_detector.rb', line 66

def configure(config)
  my_part = config[self.class.name.split(/::/)[-1]]
  return unless my_part
  configure_with(my_part)
end

#configure_with(config) ⇒ Object



72
73
74
# File 'lib/reek/smells/smell_detector.rb', line 72

def configure_with(config)
  @config.hash.adopt!(config)
end

#copyObject



76
77
78
# File 'lib/reek/smells/smell_detector.rb', line 76

def copy
  self.class.new(@config.hash.deep_copy)
end

#enabled?Boolean

SMELL: Getter (only used in 1 test)

Returns:

  • (Boolean)


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

def enabled?
  @config.enabled?
end

#examine(context) ⇒ Object



87
88
89
90
91
# File 'lib/reek/smells/smell_detector.rb', line 87

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

#examine_context(context) ⇒ Object



93
94
# File 'lib/reek/smells/smell_detector.rb', line 93

def examine_context(context)
end

#exception?(context) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/reek/smells/smell_detector.rb', line 96

def exception?(context)
  context.matches?(value(EXCLUDE_KEY, context, DEFAULT_EXCLUDE_SET))
end

#found(scope, warning) ⇒ Object



100
101
102
103
104
# File 'lib/reek/smells/smell_detector.rb', line 100

def found(scope, warning)
  smell = SmellWarning.new(self, scope, warning, @masked)
  @smells_found << smell
  smell
end

#has_smell?(patterns) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
# File 'lib/reek/smells/smell_detector.rb', line 106

def has_smell?(patterns)
  return false if @masked
  @smells_found.each { |warning| return true if warning.contains_all?(patterns) }
  false
end

#listen_to(hooks) ⇒ Object



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

def listen_to(hooks)
  self.class.contexts.each { |ctx| hooks[ctx] << self }
end

#num_smellsObject



116
117
118
# File 'lib/reek/smells/smell_detector.rb', line 116

def num_smells
  @masked ? 0 : @smells_found.length
end

#report_on(report) ⇒ Object



112
113
114
# File 'lib/reek/smells/smell_detector.rb', line 112

def report_on(report)
  @smells_found.each { |smell| smell.report_on(report) }
end

#smell_nameObject



124
125
126
# File 'lib/reek/smells/smell_detector.rb', line 124

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

#smelly?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/reek/smells/smell_detector.rb', line 120

def smelly?
  (not @masked) and (@smells_found.length > 0)
end

#supersede_with(config) ⇒ Object



80
81
82
83
84
85
# File 'lib/reek/smells/smell_detector.rb', line 80

def supersede_with(config)
  clone = self.copy
  @masked = true
  clone.configure_with(config)
  clone
end

#value(key, ctx, fall_back) ⇒ Object



128
129
130
# File 'lib/reek/smells/smell_detector.rb', line 128

def value(key, ctx, fall_back)
  @config.value(key, ctx, fall_back)
end