Class: RuboCopMethodOrder::MethodNodeCollection

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

Overview

Hold collection of public instance methods that has custom sorted order.

Direct Known Subclasses

PublicMethodNodeCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMethodNodeCollection

Returns a new instance of MethodNodeCollection.



8
9
10
# File 'lib/rubocop_method_order/method_node_collection.rb', line 8

def initialize
  @nodes = []
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



6
7
8
# File 'lib/rubocop_method_order/method_node_collection.rb', line 6

def nodes
  @nodes
end

Instance Method Details

#offensesObject



12
13
14
15
16
17
18
19
# File 'lib/rubocop_method_order/method_node_collection.rb', line 12

def offenses
  nodes.reject { |node| definition_order_correct?(node) }.map do |node|
    {
      node: node,
      other_node: previous_node_from_definition_order(node)
    }
  end
end

#push(method_node) ⇒ Object



21
22
23
24
# File 'lib/rubocop_method_order/method_node_collection.rb', line 21

def push(method_node)
  @nodes << method_node
  self
end

#replacementsObject

Build a hash for every node that is not at the correct, final position which includes any nodes that need to be moved. Used for autocorrecting.



28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop_method_order/method_node_collection.rb', line 28

def replacements
  nodes.reject { |node| method_order_correct?(node) }.each_with_object({}) do |node, obj|
    node_to_replace = nodes[expected_method_index(node)]

    obj[node] = {
      node => node_to_replace,
      node_to_replace => nodes[expected_method_index(node_to_replace)]
    }
  end
end