123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Performance::RedundantMerge::EachWithObjectInspector

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, NodePattern::Macros
Inherits: Object
Defined in: lib/rubocop/cop/performance/redundant_merge.rb

Overview

A utility class for checking the use of values within an each_with_object call.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(node, receiver) ⇒ EachWithObjectInspector

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_merge.rb', line 159

def initialize(node, receiver)
  @node = node
  @receiver = unwind(receiver)
end

Instance Attribute Details

#eligible_receiver?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_merge.rb', line 174

def eligible_receiver?
  receiver.respond_to?(:lvar_type?) && receiver.lvar_type?
end

#node (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_merge.rb', line 172

attr_reader :node, :receiver

#receiver (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_merge.rb', line 172

attr_reader :node, :receiver

#value_used?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_merge.rb', line 164

def value_used?
  return false unless eligible_receiver? && second_argument

  receiver.loc.name.source == second_argument.loc.name.source
end

Instance Method Details

#second_argument (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_merge.rb', line 178

def second_argument
  parent = node.parent
  parent = parent.parent if parent.begin_type?

  @second_argument ||= each_with_object_node(parent)
end

#unwind(receiver) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/redundant_merge.rb', line 185

def unwind(receiver)
  receiver, = *receiver while receiver.respond_to?(:send_type?) && receiver.send_type?
  receiver
end