Class: RuboCop::Cop::Ezcater::PrivateAttr

Inherits:
RuboCop::Cop show all
Defined in:
lib/rubocop/cop/ezcater/private_attr.rb

Overview

This cop checks for the use of ‘attr_accessor`, `attr_reader`, `attr_writer` and `alias_method` after the access modifiers `private` and `protected` and requires the use of methods from the private_attr gem instead.

Examples:


# bad

class C
  private

  attr_accessor :foo
  attr_reader :bar
  attr_writer :baz
  alias_method :foobar, foo
end

# good

class C
  private_attr_accessor :foo
  private_attr_reader :bar
  private_attr_writer :baz
  private_alias_method :foobar, :foo

  private

  def shy
    puts "hi"
  end
end

Constant Summary collapse

ATTR_METHODS =
%i[attr_accessor
attr_reader
attr_writer].freeze
ACCESS_AFFECTED_METHODS =
(ATTR_METHODS + %i[alias_method]).to_set.freeze
MSG =
"Use `%s_%s` instead".freeze

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



47
48
49
# File 'lib/rubocop/cop/ezcater/private_attr.rb', line 47

def on_class(node)
  check_node(node.children[2]) # class body
end

#on_module(node) ⇒ Object



51
52
53
# File 'lib/rubocop/cop/ezcater/private_attr.rb', line 51

def on_module(node)
  check_node(node.children[1]) # module body
end