class RuboCop::Cop::Layout::AccessModifierIndentation

Modifiers should be indented as deep as method definitions, or as deep as the class/module keyword, depending on configuration.

@example

# EnforcedStyle: indent (default)

# bad
class Plumbus
private
  def smooth; end
end

# good
class Plumbus
  private
  def smooth; end
end

# EnforcedStyle: outdent

# bad
class Plumbus
  private
  def smooth; end
end

# good
class Plumbus
private
  def smooth; end
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 58
def on_block(node)
  return unless node.class_constructor?

  check_body(node.body, node)
end
on_class(node) click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 43
def on_class(node)
  _name, _base_class, body = *node
  check_body(body, node)
end
on_module(node) click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 53
def on_module(node)
  _name, body = *node
  check_body(body, node)
end
on_sclass(node) click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 48
def on_sclass(node)
  _name, body = *node
  check_body(body, node)
end

Private Instance Methods

check_body(body, node) click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 66
def check_body(body, node)
  return if body.nil? # Empty class etc.

  modifiers = body.each_child_node(:send).select(&:access_modifier?)
  class_column = node.source_range.column

  modifiers.each { |modifier| check_modifier(modifier, class_column) }
end
check_modifier(send_node, class_start_col) click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 75
def check_modifier(send_node, class_start_col)
  access_modifier_start_col = send_node.source_range.column
  offset = access_modifier_start_col - class_start_col

  @column_delta = expected_indent_offset - offset
  if @column_delta.zero?
    correct_style_detected
  else
    add_offense(send_node) do
      if offset == unexpected_indent_offset
        opposite_style_detected
      else
        unrecognized_style_detected
      end
    end
  end
end
expected_indent_offset() click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 97
def expected_indent_offset
  style == :outdent ? 0 : configured_indentation_width
end
message(node) click to toggle source
# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 93
def message(node)
  format(MSG, style.capitalize, node.loc.selector.source)
end
unexpected_indent_offset() click to toggle source

An offset that is not expected, but correct if the configuration is changed.

# File lib/rubocop/cop/layout/access_modifier_indentation.rb, line 103
def unexpected_indent_offset
  configured_indentation_width - expected_indent_offset
end