Class: RuboCop::Cop::Kaizo::AgentNounClassName

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/kaizo/agent_noun_class_name.rb

Overview

Checks for classes named as agent nouns ("doers") rather than the domain concepts they model. A class whose name ends in er or or (Manager, Processor, Handler) -- or in any configured ForbiddenSuffixes such as Service -- usually signals procedural behavior that wants a clearer domain name or a different home.

Names ending in an AllowedSuffixes entry are exempt; the match is by suffix, so Controller clears both Controller and UsersController. ForbiddenSuffixes always flags, even when also matched by AllowedSuffixes -- which is how you drop a default exemption.

class definitions and Struct.new/Data.define/Class.new constant assignments are both checked. There is no autocorrection: renaming a class is a design decision.

Examples:

# bad
class PaymentProcessor
end

# good
class Payment
end
# good - ends in an allowed suffix
class UsersController
end

Constant Summary collapse

MSG =
"Avoid the doer-style class name `%<name>s`. " \
"Prefer a name for the concept it models over the action it performs.".freeze
AGENT_NOUN =
/(?:er|or)\z/i

Instance Method Summary collapse

Instance Method Details

#class_builder_assignment(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/rubocop/cop/kaizo/agent_noun_class_name.rb', line 39

def_node_matcher :class_builder_assignment, <<~PATTERN
  (casgn _ $_ {
    (send (const _ {:Struct :Data :Class}) {:new :define} ...)
    (block (send (const _ {:Struct :Data :Class}) {:new :define} ...) ...)
  })
PATTERN

#on_casgn(node) ⇒ Object



50
51
52
53
54
# File 'lib/rubocop/cop/kaizo/agent_noun_class_name.rb', line 50

def on_casgn(node)
  return unless (name = class_builder_assignment(node))

  check_name(name.to_s, node.loc.name)
end

#on_class(node) ⇒ Object



46
47
48
# File 'lib/rubocop/cop/kaizo/agent_noun_class_name.rb', line 46

def on_class(node)
  check_name(node.identifier.short_name.to_s, node.loc.name)
end