Class: RuboCop::Cop::Rails::BulkChangeTable::AlterMethodsRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/rails/bulk_change_table.rb

Overview

Record combinable alter methods and register offensive nodes.

Instance Method Summary collapse

Constructor Details

#initializeAlterMethodsRecorder

Returns a new instance of AlterMethodsRecorder.



235
236
237
238
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 235

def initialize
  @nodes = []
  @offensive_nodes = []
end

Instance Method Details

#flushObject



254
255
256
257
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 254

def flush
  @offensive_nodes << @nodes.first if @nodes.size > 1
  @nodes = []
end

#offensive_nodesObject



259
260
261
262
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 259

def offensive_nodes
  flush
  @offensive_nodes
end

#process(new_node) ⇒ Object

Parameters:

  • new_node (RuboCop::AST::SendNode)


241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 241

def process(new_node)
  # arguments: [{(sym :table)(str "table")} ...]
  table_node = new_node.first_argument
  if table_node.is_a? RuboCop::AST::BasicLiteralNode
    flush unless @nodes.all? do |node|
      node.first_argument.value.to_s == table_node.value.to_s
    end
    @nodes << new_node
  else
    flush
  end
end