-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunked_writer.rb
57 lines (49 loc) · 1.11 KB
/
chunked_writer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class ChunkedWriter
def self.open(*args, **options)
writer = new(*args, **options)
yield(writer)
ensure
writer.close
end
attr_reader :filename, :file_count, :line_count, :options, :current_file, :limit, :lineno
def initialize(filename, limit=1000, **options)
@filename = filename
@file_count = 0
@line_count = 0
@options = options
@limit = limit
@lineno = 0
end
def <<(row)
if line_count >= limit
increment_file
end
current_file << row
@line_count += 1
@lineno += 1
end
def close
close_current_file
end
def current_file
@current_file ||= CSV.open(make_filename, "wb", options)
end
def make_filename
"%<base>s%<number>02d%<ext>s" % {
base: File.basename(filename, ".*"),
number: file_count,
ext: File.extname(filename)
}
end
def increment_file
close_current_file
@file_count += 1
@current_file = nil
@line_count = 0
end
def close_current_file
current_file.close
$stderr.puts "Wrote #{line_count} rows to #{make_filename}"
@current_file = nil
end
end