Class: CSVStepImporter::Chunk

Inherits:
Node
  • Object
show all
Defined in:
lib/csv_step_importer/chunk.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #env, #parent

Instance Method Summary collapse

Methods inherited from Node

#add_children, #create_or_update, #validate_children

Methods inherited from Base

#assign_attributes, #create_or_update, #inspect, #persisted?, #save, #save!, #to_s, #update

Constructor Details

#initialize(rows: [], row_class: CSVStepImporter::Row, processor_classes: nil, **attributes) ⇒ Chunk

Returns a new instance of Chunk.



7
8
9
10
11
12
13
# File 'lib/csv_step_importer/chunk.rb', line 7

def initialize(rows: [], row_class: CSVStepImporter::Row, processor_classes: nil, **attributes)
  super **attributes

  self.cache = {}
  add_rows rows: rows, row_class: row_class
  add_children processor_classes
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



5
6
7
# File 'lib/csv_step_importer/chunk.rb', line 5

def cache
  @cache
end

#rowsObject

Returns the value of attribute rows.



5
6
7
# File 'lib/csv_step_importer/chunk.rb', line 5

def rows
  @rows
end

Instance Method Details

#add_rows(rows:, row_class:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/csv_step_importer/chunk.rb', line 15

def add_rows(rows:, row_class:)
  row_parent_node = CSVStepImporter::Node.new parent: self

  unless rows.empty? || rows.first.is_a?(row_class)
    row_number = 0
    rows = rows.collect do |row|
      row_class.new(parent: row_parent_node, row_number: row_number += 1, **row)
    end
  end

  @rows = rows

  row_parent_node.add_children rows
  add_children row_parent_node
end