class RuboCopMethodOrder::MethodCollector

This object collects every method definition found during a cop run and will attempt to split them by scope and context. If a method is disabled by a Rubocop comment it is not added to the collector. A method is also only collected once.

Attributes

all_method_nodes[R]
nodes_by_scope[R]

Public Class Methods

new(should_skip_method:) click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 15
def initialize(should_skip_method:)
  @all_method_nodes = []
  @should_skip_method = should_skip_method
  @nodes_by_scope = {}
end

Public Instance Methods

collect(def_node, scope_name = 'global') click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 21
def collect(def_node, scope_name = 'global')
  return if @should_skip_method.call(def_node)
  return if @all_method_nodes.include?(def_node)

  @all_method_nodes << def_node

  unless @nodes_by_scope.key?(scope_name)
    @nodes_by_scope[scope_name] = new_node_collection(scope_name)
  end
  @nodes_by_scope[scope_name].push(def_node)
end
collect_nodes_from_class(class_node) click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 33
def collect_nodes_from_class(class_node)
  base_scope_name = "CLASS:#{scope_for_node(class_node)}"

  mode = :public
  child_nodes_from_container(class_node).each do |node|
    if node.type == :def
      collect(node, "#{base_scope_name}:#{mode}_methods")
    elsif node.type == :defs
      collect(node, "#{base_scope_name}:class_methods")
    end

    mode = node.method_name if scope_change_node?(node)
  end
end
collect_nodes_from_module(module_node) click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 48
def collect_nodes_from_module(module_node)
  base_scope_name = "MODULE:#{scope_for_node(module_node)}"

  child_nodes_from_container(module_node).each do |node|
    if node.type == :def
      collect(node, "#{base_scope_name}:public_methods")
    elsif node.type == :defs
      collect(node, "#{base_scope_name}:class_methods")
    end
  end
end

Private Instance Methods

child_nodes_from_container(node) click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 62
def child_nodes_from_container(node)
  begin_node = node.child_nodes.select { |x| x&.type == :begin }.first
  begin_node.nil? ? node.child_nodes : begin_node.child_nodes
end
new_node_collection(scope_name) click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 67
def new_node_collection(scope_name)
  if scope_name.match?(/:public_methods$/)
    PublicMethodNodeCollection.new
  else
    MethodNodeCollection.new
  end
end
scope_change_node?(node) click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 75
def scope_change_node?(node)
  node.type == :send && %i[private protected].include?(node.method_name)
end
scope_for_node(node) click to toggle source
# File lib/rubocop_method_order/method_collector.rb, line 79
def scope_for_node(node)
  "#{node.source_range.begin_pos}-#{node.source_range.end_pos}"
end