Class: RuboCop::Cop::Obsession::Rails::ServicePerformMethod

Inherits:
ServiceName
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/obsession/rails/service_perform_method.rb

Overview

This cop checks for services whose single public method is not named perform.

Services and jobs with only one public method should have their method named perform for consistency. The choice of perform as a name is inspired from ActiveJob and makes it easier to make services and jobs interchangeable.

Examples:

# bad
class ImportCompany
  def import
    ...
  end
end

# bad
class ImportCompany
  def execute
    ...
  end
end

# good
class ImportCompany
  def perform
    ...
  end
end

Constant Summary collapse

MSG =
'Single public method of Service should be called `perform`'

Constants inherited from ServiceName

RuboCop::Cop::Obsession::Rails::ServiceName::IGNORED_PUBLIC_METHODS

Instance Method Summary collapse

Methods included from Helpers

#rails_callback?, #verb?

Instance Method Details

#on_class(class_node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/obsession/rails/service_perform_method.rb', line 41

def on_class(class_node)
  public_methods = public_methods(class_node)
  return if public_methods.length != 1
  method = public_methods.first

  if method.method_name != :perform
    add_offense(method) do |corrector|
      corrector.replace(method, method.source.sub(method.method_name.to_s, 'perform'))
    end
  end
end