Class: RuboCopMethodOrder::MethodCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop_method_order/method_collector.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(should_skip_method:) ⇒ MethodCollector

Returns a new instance of MethodCollector.



15
16
17
18
19
# 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

Instance Attribute Details

#all_method_nodesObject (readonly)

Returns the value of attribute all_method_nodes.



12
13
14
# File 'lib/rubocop_method_order/method_collector.rb', line 12

def all_method_nodes
  @all_method_nodes
end

#nodes_by_scopeObject (readonly)

Returns the value of attribute nodes_by_scope.



12
13
14
# File 'lib/rubocop_method_order/method_collector.rb', line 12

def nodes_by_scope
  @nodes_by_scope
end

Instance Method Details

#collect(def_node, scope_name = 'global') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# 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