class RuboCop::Cop::Style::DocumentationMethod

Checks for missing documentation comment for public methods. It can optionally be configured to also require documentation for non-public methods.

NOTE: This cop allows ‘initialize` method because `initialize` is a special method called from `new`. In some programming languages they are called constructor to distinguish it from method.

@example

# bad

class Foo
  def bar
    puts baz
  end
end

module Foo
  def bar
    puts baz
  end
end

def foo.bar
  puts baz
end

# good

class Foo
  # Documentation
  def bar
    puts baz
  end
end

module Foo
  # Documentation
  def bar
    puts baz
  end
end

# Documentation
def foo.bar
  puts baz
end

@example RequireForNonPublicMethods: false (default)

# good
class Foo
  protected
  def do_something
  end
end

class Foo
  private
  def do_something
  end
end

@example RequireForNonPublicMethods: true

# bad
class Foo
  protected
  def do_something
  end
end

class Foo
  private
  def do_something
  end
end

# good
class Foo
  protected
  # Documentation
  def do_something
  end
end

class Foo
  private
  # Documentation
  def do_something
  end
end

@example AllowedMethods: [‘method_missing’, ‘respond_to_missing?’]

# good
class Foo
  def method_missing(name, *args)
  end

  def respond_to_missing?(symbol, include_private)
  end
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/style/documentation_method.rb, line 120
def on_def(node)
  return if node.method?(:initialize)

  parent = node.parent
  modifier_node?(parent) ? check(parent) : check(node)
end
Also aliased as: on_defs
on_defs(node)
Alias for: on_def

Private Instance Methods

allowed_methods() click to toggle source
# File lib/rubocop/cop/style/documentation_method.rb, line 146
def allowed_methods
  @allowed_methods ||= cop_config.fetch('AllowedMethods', []).map(&:to_sym)
end
check(node) click to toggle source
# File lib/rubocop/cop/style/documentation_method.rb, line 130
def check(node)
  return if non_public?(node) && !require_for_non_public_methods?
  return if documentation_comment?(node)
  return if method_allowed?(node)

  add_offense(node)
end
method_allowed?(node) click to toggle source
# File lib/rubocop/cop/style/documentation_method.rb, line 142
def method_allowed?(node)
  allowed_methods.include?(node.method_name)
end
require_for_non_public_methods?() click to toggle source
# File lib/rubocop/cop/style/documentation_method.rb, line 138
def require_for_non_public_methods?
  cop_config['RequireForNonPublicMethods']
end