Class: Cure::Export::ChunkCsvProcessor

Inherits:
BaseProcessor show all
Includes:
Helpers::FileHelpers
Defined in:
lib/cure/export/base_processor.rb

Instance Attribute Summary collapse

Attributes inherited from BaseProcessor

#named_range

Instance Method Summary collapse

Methods included from Helpers::FileHelpers

#clean_dir, #open_file, #read_file, #with_file, #with_temp_dir

Methods inherited from BaseProcessor

#initialize, #process_row

Methods included from Log

#log_debug, #log_error, #log_info, #log_trace, #log_warn

Constructor Details

This class inherits a constructor from Cure::Export::BaseProcessor

Instance Attribute Details

#chunk_sizeObject (readonly)

Returns the value of attribute chunk_size.



114
115
116
# File 'lib/cure/export/base_processor.rb', line 114

def chunk_size
  @chunk_size
end

#current_csv_fileObject (readonly)

Returns the value of attribute current_csv_file.



114
115
116
# File 'lib/cure/export/base_processor.rb', line 114

def current_csv_file
  @current_csv_file
end

#directoryObject (readonly)

Returns the value of attribute directory.



114
115
116
# File 'lib/cure/export/base_processor.rb', line 114

def directory
  @directory
end

#file_name_prefixObject (readonly)

Returns the value of attribute file_name_prefix.



114
115
116
# File 'lib/cure/export/base_processor.rb', line 114

def file_name_prefix
  @file_name_prefix
end

#include_headersObject (readonly)

Returns the value of attribute include_headers.



114
115
116
# File 'lib/cure/export/base_processor.rb', line 114

def include_headers
  @include_headers
end

#row_countObject (readonly)

Returns the value of attribute row_count.



114
115
116
# File 'lib/cure/export/base_processor.rb', line 114

def row_count
  @row_count
end

Instance Method Details

#chunked_file_handler {|@current_csv_file| ... } ⇒ Object

Yields:



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cure/export/base_processor.rb', line 161

def chunked_file_handler(&block)
  raise "No block" unless block

  if @processed.zero? || (@processed % @chunk_size).zero?
    @current_csv_file&.close

    @current_chunk += 1
    log_info "Writing file to #{current_file_path}"
    @current_csv_file = File.open(current_file_path, "w")
  end

  yield @current_csv_file
end

#cleanupObject



147
148
149
150
# File 'lib/cure/export/base_processor.rb', line 147

def cleanup
ensure
  @current_csv_file.close
end

#current_file_pathObject



175
176
177
# File 'lib/cure/export/base_processor.rb', line 175

def current_file_path
  "#{@output_dir}/#{@current_chunk}-#{@file_name_prefix}.csv"
end

#extract_optsObject



152
153
154
155
156
157
158
159
# File 'lib/cure/export/base_processor.rb', line 152

def extract_opts
  # TODO: Add offset? pick a slice?
  @output_dir = @opts[:directory]
  @file_name_prefix = @opts[:file_name_prefix]
  @directory = @opts[:directory]
  @chunk_size = @opts[:chunk_size]
  @include_headers = @opts.fetch(:include_headers, true)
end

#process(row) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/cure/export/base_processor.rb', line 121

def process(row)
  chunked_file_handler do |csv_file|
    if @processed.zero? || (@processed % @chunk_size).zero? || (@processed % @chunk_size).zero?
      csv_file.write(row.keys.to_csv)
    end

    csv_file.write(row.values.to_csv)
  end
end

#setupObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cure/export/base_processor.rb', line 131

def setup
  log_info "Exporting [#{@named_range}] to CSV..."

  extract_opts

  log_info("Exporting file to [#{@output_dir}/#{@file_name_prefix}]")

  clean_dir(@output_dir)

  dir = File.dirname("#{@output_dir}/#{@file_name_prefix}")
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  @processed = 0
  @current_chunk = 0
end