Class: Ducalis::OnlyDefs

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
RuboCop::Cop::DefNode
Defined in:
lib/ducalis/cops/only_defs.rb

Constant Summary collapse

OFFENSE =
<<-MESSAGE.gsub(/^ +\|\s/, '').strip
  | Prefer object instances to class methods because class methods resist refactoring. Begin with an object instance, even if it doesn’t have state or multiple methods right away. If you come back to change it later, you will be more likely to refactor. If it never changes, the difference between the class method approach and the instance is negligible, and you certainly won’t be any worse off.
MESSAGE
DETAILS =
<<-MESSAGE.gsub(/^ +\|\s/, '').strip
  | Related article: https://codeclimate.com/blog/why-ruby-class-methods-resist-refactoring/
MESSAGE

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/ducalis/cops/only_defs.rb', line 17

def on_class(node)
  _name, inheritance, body = *node
  return if !inheritance.nil? || body.nil?
  return unless !instance_methods_definitions?(body) &&
                class_methods_defintions?(body)

  add_offense(node, :expression, OFFENSE)
end