Class: RuboCop::Cop::Cuseum::PublicMethodNamedCall

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/cuseum/public_method_named_call.rb

Overview

Ensures that any public method in service classes is named "call".

Examples:

# good - no public methods
class UserService < BaseService
  private

  def process_user
    # ...
  end
end

# good - public method named "call"
class UserService < BaseService
  def call
    # ...
  end

  private

  def process_user
    # ...
  end
end

# bad - public method with wrong name
class UserService < BaseService
  def execute  # ← should be "call"
    # ...
  end
end

# bad - multiple public methods, some with wrong names
class UserService < BaseService
  def call
    # ...
  end

  def execute  # ← should be "call" or private
    # ...
  end
end

Constant Summary collapse

MSG =
'Public methods in service classes should be named "call".'

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/cuseum/public_method_named_call.rb', line 53

def on_class(node)
  public_methods = extract_public_methods(node)

  public_methods.each do |method_node|
    method_name = method_node.method_name.to_s
    unless method_name == "call"
      add_offense(method_node, message: MSG)
    end
  end
end