Class: Skeptic::Rules::MethodsPerClass

Inherits:
Object
  • Object
show all
Includes:
SexpVisitor
Defined in:
lib/skeptic/rules/methods_per_class.rb

Constant Summary collapse

DESCRIPTION =
'Limit the number of methods per class'

Instance Method Summary collapse

Methods included from SexpVisitor

included

Constructor Details

#initialize(limit) ⇒ MethodsPerClass

Returns a new instance of MethodsPerClass.



8
9
10
11
# File 'lib/skeptic/rules/methods_per_class.rb', line 8

def initialize(limit)
  @methods = Hash.new { |hash, key| hash[key] = [] }
  @limit   = limit
end

Instance Method Details

#apply_to(code, tokens, tree) ⇒ Object



13
14
15
16
# File 'lib/skeptic/rules/methods_per_class.rb', line 13

def apply_to(code, tokens, tree)
  visit tree
  self
end

#methods_in(class_name) ⇒ Object



18
19
20
# File 'lib/skeptic/rules/methods_per_class.rb', line 18

def methods_in(class_name)
  @methods[class_name].length
end

#nameObject



33
34
35
# File 'lib/skeptic/rules/methods_per_class.rb', line 33

def name
  "Number of methods per class (#@limit)"
end

#violationsObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/skeptic/rules/methods_per_class.rb', line 22

def violations
  violators = @methods.keys.select { |name| @methods[name].length > @limit }

  violators.map do |class_name|
    method_names = @methods[class_name].map { |name| "##{name}" }
    count        = method_names.length

    "#{class_name} has #{count} methods: #{method_names.join(', ')}"
  end
end