class RuboCop::Cop::Naming::AccessorMethodName
end
def set_value
# accepted, incorrect arity for setter
end
def get_value(attr)
# accepted, incorrect arity for getter
end
def attribute
# good
end
def get_attribute
# bad
end
def attribute=(value)
# good
end
def set_attribute(value)
# bad
@example
one.
registered, and setters (‘set_attribute(value)`) must have exactly
arity. Getters (`get_attribute`) must have no arguments to be
NOTE: Offenses are only registered for methods with the expected
to both instance and class methods.
This cop makes sure that accessor methods are named properly. Applies
def bad_reader_name?(node)
def bad_reader_name?(node) node.method_name.to_s.start_with?('get_') && !node.arguments? end
def bad_writer_name?(node)
def bad_writer_name?(node) node.method_name.to_s.start_with?('set_') && node.arguments.one? end
def message(node)
def message(node) if bad_reader_name?(node) MSG_READER elsif bad_writer_name?(node) MSG_WRITER end end
def on_def(node)
def on_def(node) return unless bad_reader_name?(node) || bad_writer_name?(node) message = message(node) add_offense(node.loc.name, message: message) end