Class: EXEL::Processors::SplitProcessor

Inherits:
Object
  • Object
show all
Includes:
EXEL::ProcessorHelper
Defined in:
lib/exel/processors/split_processor.rb

Constant Summary collapse

DEFAULT_CHUNK_SIZE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EXEL::ProcessorHelper

#ensure_transaction_duration, #file_size_in_mb, #log, #log_error, #log_exception, #log_info, #log_prefix, #log_prefix_with, #log_process, #log_transaction, #tag, #timestamp

Constructor Details

#initialize(context) ⇒ SplitProcessor

Returns a new instance of SplitProcessor.



14
15
16
17
18
19
20
21
# File 'lib/exel/processors/split_processor.rb', line 14

def initialize(context)
  @buffer = []
  @tempfile_count = 0
  @context = context
  @file = context[:resource]

  log_prefix_with '[SplitProcessor]'
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



10
11
12
# File 'lib/exel/processors/split_processor.rb', line 10

def block
  @block
end

#file_nameObject

Returns the value of attribute file_name.



10
11
12
# File 'lib/exel/processors/split_processor.rb', line 10

def file_name
  @file_name
end

Instance Method Details

#generate_chunk(content) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/exel/processors/split_processor.rb', line 40

def generate_chunk(content)
  @tempfile_count += 1
  chunk = Tempfile.new([chunk_filename, '.csv'])
  chunk.write(content)
  chunk.rewind

  log_info "Generated chunk # #{@tempfile_count} for file #{filename(@file)} in #{chunk.path}"
  chunk
end

#process(callback) ⇒ Object



23
24
25
26
27
28
# File 'lib/exel/processors/split_processor.rb', line 23

def process(callback)
  log_process do
    process_file(callback)
    finish(callback)
  end
end

#process_line(line, callback) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/exel/processors/split_processor.rb', line 30

def process_line(line, callback)
  if line == :eof
    flush_buffer(callback)
  else
    @buffer << CSV.generate_line(line)

    flush_buffer(callback) if buffer_full?
  end
end