Class: RuboCop::Cop::Ditty::CallServicesDirectly

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/ditty/call_services_directly.rb

Overview

This cop enforces the use of ‘Service.method` instead of `Service.instance.method`. Calling the singleton instance has been deprecated for services.

Examples:

# bad
::Ditty::Services::Logger.instance.info 'This is a log message'

# good
::Ditty::Services::Logger.info 'This is a log message'

Constant Summary collapse

MSG =
'Do not use `.instance` on services. Call the method directly instead'

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



33
34
35
36
37
38
# File 'lib/rubocop/cop/ditty/call_services_directly.rb', line 33

def autocorrect(node)
  lambda do |corrector|
    internal = node.children.first.source
    corrector.replace(node.loc.expression, internal)
  end
end

#on_send(node) ⇒ Object



27
28
29
30
31
# File 'lib/rubocop/cop/ditty/call_services_directly.rb', line 27

def on_send(node)
  return unless service_instance_call?(node)

  add_offense(node)
end