Class: RuboCop::Cop::Style::DisallowDelegate

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/style/disallow_delegate.rb

Overview

Forbids automatic delegation — delegate, delegate_missing_to, def_delegator/def_delegators (Forwardable) and DelegateClass. A delegation macro is a call, not a definition, so grep 'def foo' and jump-to-definition find nothing; the community name for the smell is Fowler's Middle Man. Write the method, or delete the wrapper and let the caller ask the object that knows.

Examples:

# bad
delegate :commission, to: :user_commission

# good
def commission
  user_commission.commission
end

Constant Summary collapse

MSG =
'Do not use automatic delegation. Write the method explicitly instead.'
RESTRICT_ON_SEND =
%i[delegate delegate_missing_to def_delegator def_delegators DelegateClass].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



29
30
31
32
33
# File 'lib/rubocop/cop/style/disallow_delegate.rb', line 29

def on_send(node)
  return unless node.receiver.nil?

  add_offense(node)
end