Class: RuboCop::Cop::Operandi::MissingPrivateKeyword

Inherits:
Base
  • Object
show all
Defined in:
lib/operandi/rubocop/cop/operandi/missing_private_keyword.rb

Overview

Ensures that step methods are defined as private. Step methods are implementation details and should not be part of the public API.

Examples:

# bad
class MyService < ApplicationService
  step :process
  step :notify

  def process
    # This should be private
  end

  def notify
    # This should be private
  end
end

# good
class MyService < ApplicationService
  step :process
  step :notify

  private

  def process
    # Now private
  end

  def notify
    # Now private
  end
end

Constant Summary collapse

MSG =
"Step method `%<name>s` should be private."

Instance Method Summary collapse

Instance Method Details

#after_class(_node) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/operandi/rubocop/cop/operandi/missing_private_keyword.rb', line 69

def after_class(_node)
  return unless @public_step_methods&.any?

  @public_step_methods.each do |node|
    add_offense(node, message: format(MSG, name: node.method_name))
  end
end

#on_class(_node) ⇒ Object



43
44
45
46
47
# File 'lib/operandi/rubocop/cop/operandi/missing_private_keyword.rb', line 43

def on_class(_node)
  @step_names = []
  @private_section_started = false
  @public_step_methods = []
end

#on_def(node) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/operandi/rubocop/cop/operandi/missing_private_keyword.rb', line 61

def on_def(node)
  return unless @step_names&.include?(node.method_name)
  return if @private_section_started

  @public_step_methods ||= []
  @public_step_methods << node
end

#on_send(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/operandi/rubocop/cop/operandi/missing_private_keyword.rb', line 49

def on_send(node)
  if step_call?(node)
    step_name = node.arguments.first&.value
    @step_names ||= []
    @step_names << step_name if step_name
  elsif private_declaration?(node)
    @private_section_started = true
  elsif public_declaration?(node)
    @private_section_started = false
  end
end