Class: CSVStepImporter::Chunk
- Defined in:
- lib/csv_step_importer/chunk.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#first_row ⇒ Object
Returns the value of attribute first_row.
-
#rows ⇒ Object
Returns the value of attribute rows.
Attributes inherited from Node
Instance Method Summary collapse
- #add_rows(rows:, row_class:) ⇒ Object
-
#initialize(rows: [], row_class: CSVStepImporter::Row, processor_classes: nil, first_row: 0, **attributes) ⇒ Chunk
constructor
A new instance of Chunk.
Methods inherited from Node
#add_children, #build_env, #create_or_update, #run_validations!, #validate_children
Methods inherited from Base
#ancestors, #assign_attributes, #create_or_update, #inspect, #persisted?, #save, #save!, set, #to_s, #update
Constructor Details
#initialize(rows: [], row_class: CSVStepImporter::Row, processor_classes: nil, first_row: 0, **attributes) ⇒ Chunk
Returns a new instance of Chunk.
9 10 11 12 13 14 15 16 17 |
# File 'lib/csv_step_importer/chunk.rb', line 9 def initialize(rows: [], row_class: CSVStepImporter::Row, processor_classes: nil, first_row: 0, **attributes) super **attributes self.cache = {} self.first_row = first_row add_rows rows: rows, row_class: row_class add_children processor_classes end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
7 8 9 |
# File 'lib/csv_step_importer/chunk.rb', line 7 def cache @cache end |
#first_row ⇒ Object
Returns the value of attribute first_row.
7 8 9 |
# File 'lib/csv_step_importer/chunk.rb', line 7 def first_row @first_row end |
#rows ⇒ Object
Returns the value of attribute rows.
7 8 9 |
# File 'lib/csv_step_importer/chunk.rb', line 7 def rows @rows end |
Instance Method Details
#add_rows(rows:, row_class:) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/csv_step_importer/chunk.rb', line 19 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 = self.first_row - 1 rows = rows.collect do |row| row_class.new(parent: row_parent_node, row_number: row_number += 1, **row) end.find_all(&:include_row?) end @rows = rows row_parent_node.add_children rows add_children row_parent_node end |