class RuboCop::Cop::Style::AccessorMethodName

This cop makes sure that accessor methods are named properly.

@example

# bad
def set_attribute(value) ...

# good
def attribute=(value)

# bad
def get_attribute ...

# good
def attribute ...

Private Instance Methods

bad_reader_name?(method_name, args) click to toggle source
# File lib/rubocop/cop/style/accessor_method_name.rb, line 35
def bad_reader_name?(method_name, args)
  method_name.start_with?('get_') && args.to_a.empty?
end
bad_writer_name?(method_name, args) click to toggle source
# File lib/rubocop/cop/style/accessor_method_name.rb, line 39
def bad_writer_name?(method_name, args)
  method_name.start_with?('set_') && args.to_a.one?
end
on_method_def(node, method_name, args, _body) click to toggle source
# File lib/rubocop/cop/style/accessor_method_name.rb, line 25
def on_method_def(node, method_name, args, _body)
  if bad_reader_name?(method_name.to_s, args)
    add_offense(node, :name,
                'Do not prefix reader method names with `get_`.')
  elsif bad_writer_name?(method_name.to_s, args)
    add_offense(node, :name,
                'Do not prefix writer method names with `set_`.')
  end
end