Class: BusinessCatalyst::CSV::FileSplitter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @base_name = base_name
  @max_rows_per_file = options.fetch(:max_rows_per_file) { 10_000 }
  @header_row = options.fetch(:header_row) { nil }
  @verbose = options.fetch(:verbose) { false }
  @logger = options.fetch(:logger) { nil }

  @current_row = 0
  @total_rows = 0

  @all_files = []
  @current_file = nil
end

Instance Attribute Details

#all_filesObject (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_nameObject

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_fileObject (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_rowObject (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_rowObject

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

#loggerObject

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_fileObject

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_rowsObject (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

#verboseObject

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

#closeObject



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

#startObject



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_rowObject

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