Class: RuboCop::Cop::Cuseum::SinglePublicMethodService

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

Overview

Ensures that service classes in app/services have at most one public method.

Examples:

# good - zero public methods
class UserService < BaseService
  private

  def process_user
    # ...
  end
end

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

  private

  def process_user
    # ...
  end
end

# bad - multiple public methods
class UserService < BaseService
  def call
    # ...
  end

  def execute  # ← should be private
    # ...
  end
end

Constant Summary collapse

MSG =
"Service classes should have at most one public method."

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/cuseum/single_public_method_service.rb', line 46

def on_class(node)
  public_methods = extract_public_methods(node)

  # Allow 0 or 1 public methods, flag 2 or more
  return if public_methods.size <= 1

  public_methods.each_with_index do |method_node, index|
    next if index.zero? # Allow the first public method

    add_offense(method_node, message: MSG)
  end
end