Class: BusinessCatalyst::CSV::FileSplitter
- Inherits:
-
Object
- Object
- BusinessCatalyst::CSV::FileSplitter
- Defined in:
- lib/business_catalyst/csv/file_splitter.rb
Overview
Business Catalyst can only import 10k product rows at a time. Use this class to help split up files.
Usage
headers = MyRowClass.headers
splitter = FileSplitter.new("output_base_name", max_rows_per_file: 10_000, header_row: headers)
splitter.start do |splitter|
product_rows.each do |row|
splitter.start_row
splitter.current_file << row.to_a
end
end
If you want to watch for when a new file is opened and maybe do some logging, you can use the #on_file_change method:
splitter.on_file_change do |splitter|
puts "opened #{splitter.current_file.path}"
end
Instance Attribute Summary collapse
-
#all_files ⇒ Object
(also: #file_names)
readonly
Returns the value of attribute all_files.
-
#base_name ⇒ Object
Returns the value of attribute base_name.
-
#current_file ⇒ Object
readonly
Returns the value of attribute current_file.
-
#current_row ⇒ Object
readonly
Returns the value of attribute current_row.
-
#header_row ⇒ Object
Returns the value of attribute header_row.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#max_rows_per_file ⇒ Object
Returns the value of attribute max_rows_per_file.
-
#total_rows ⇒ Object
readonly
Returns the value of attribute total_rows.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(base_name, options = {}) ⇒ FileSplitter
constructor
A new instance of FileSplitter.
- #on_file_change(&block) ⇒ Object
- #start ⇒ Object
-
#start_row ⇒ Object
Call before writing new row.
Constructor Details
#initialize(base_name, options = {}) ⇒ FileSplitter
Returns a new instance of FileSplitter.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 36 def initialize(base_name, = {}) @base_name = base_name @max_rows_per_file = .fetch(:max_rows_per_file) { 10_000 } @header_row = .fetch(:header_row) { nil } @verbose = .fetch(:verbose) { false } @logger = .fetch(:logger) { nil } @current_row = 0 @total_rows = 0 @all_files = [] @current_file = nil end |
Instance Attribute Details
#all_files ⇒ Object (readonly) Also known as: file_names
Returns the value of attribute all_files.
32 33 34 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 32 def all_files @all_files end |
#base_name ⇒ Object
Returns the value of attribute base_name.
31 32 33 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 31 def base_name @base_name end |
#current_file ⇒ Object (readonly)
Returns the value of attribute current_file.
32 33 34 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 32 def current_file @current_file end |
#current_row ⇒ Object (readonly)
Returns the value of attribute current_row.
32 33 34 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 32 def current_row @current_row end |
#header_row ⇒ Object
Returns the value of attribute header_row.
31 32 33 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 31 def header_row @header_row end |
#logger ⇒ Object
Returns the value of attribute logger.
31 32 33 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 31 def logger @logger end |
#max_rows_per_file ⇒ Object
Returns the value of attribute max_rows_per_file.
31 32 33 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 31 def max_rows_per_file @max_rows_per_file end |
#total_rows ⇒ Object (readonly)
Returns the value of attribute total_rows.
32 33 34 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 32 def total_rows @total_rows end |
#verbose ⇒ Object
Returns the value of attribute verbose.
31 32 33 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 31 def verbose @verbose end |
Instance Method Details
#close ⇒ Object
74 75 76 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 74 def close current_file.close if current_file end |
#on_file_change(&block) ⇒ Object
70 71 72 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 70 def on_file_change(&block) @on_file_change = Proc.new(&block) end |
#start ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 50 def start begin increment_file(1, max_rows_per_file) # will be off by one unless we set manually yield self ensure close end rename_last_file end |
#start_row ⇒ Object
Call before writing new row
61 62 63 64 65 66 67 68 |
# File 'lib/business_catalyst/csv/file_splitter.rb', line 61 def start_row @current_row += 1 @total_rows += 1 if @current_row > max_rows_per_file increment_file @current_row = 1 end end |