Class: RuboCop::Cop::Layout::OrderedMethods

Inherits:
RuboCop::Cop show all
Includes:
IgnoredMethods, RangeHelp
Defined in:
lib/rubocop/cop/layout/ordered_methods.rb

Overview

Examples:

EnforcedStyle: alphabetical (default)

# Check that methods are defined alphabetically.

# bad
def self.b; end
def self.a; end

def b; end
def a; end

private

def d; end
def c; end

# good
def self.a; end
def self.b; end

def a; end
def b; end

private

def c; end
def d; end

Constant Summary collapse

COMPARISONS =
{
  'alphabetical' => lambda do |left_method, right_method|
    (left_method.method_name <=> right_method.method_name) != 1
  end
}.freeze
ERR_INVALID_COMPARISON =
'Invalid "Comparison" config for ' \
"#{cop_name}. Expected one of: #{COMPARISONS.keys.join(', ')}".freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/layout/ordered_methods.rb', line 45

def autocorrect(node)
  @corrector.correct(node, @previous_node)
end

#on_begin(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubocop/cop/layout/ordered_methods.rb', line 49

def on_begin(node)
  cache(node)
  consecutive_methods(@siblings) do |previous, current|
    unless ordered?(previous, current)
      @previous_node = previous
      add_offense(
        current,
        message: 'Methods should be sorted in ' \
          "#{cop_config['EnforcedStyle']} order."
      )
    end
  end
end