-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_by_vendor.rb
More file actions
executable file
·72 lines (56 loc) · 1.46 KB
/
split_by_vendor.rb
File metadata and controls
executable file
·72 lines (56 loc) · 1.46 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
require 'csv'
require 'optparse'
require 'fileutils'
class FileSplitter
attr_reader :file, :prefix, :dir
def initialize(file, prefix:, directory:)
@file = file
@prefix = prefix
@dir = directory
end
def call
ensure_directory!
rows = load_file
rows.group_by { |row| row['Vendor'] }.each do |vendor, rows|
write_rows(vendor, rows)
end
end
private
def load_file
CSV.read(file, headers: true).map(&:to_h)
end
def write_rows(vendor, rows)
fname = generate_filename(vendor)
puts "Writing #{rows.count} rows to #{fname} ..."
CSV.open(fname, "w", write_headers: true, headers: rows.first.keys) do |csv|
rows.each { csv << _1 }
end
end
def generate_filename(vendor)
File.join(dir, [
prefix,
vendor.gsub(/[[:punct:]]/, '-').gsub(/\s+/, '-').gsub(/--*/, '-').downcase + ".csv"
].join("-"))
end
def ensure_directory!
FileUtils.mkdir_p(dir) unless dir.nil? || dir == ''
end
end
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.on("--prefix=PREFIX", "filenames prefix") do |prefix|
options[:prefix] = prefix
end
opts.on("--dir=DIR", "directory to put everythings") do |dir|
options[:dir] = dir
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end
parser.parse!
file = ARGV[0]
splitter = FileSplitter.new(file, prefix: options[:prefix], directory: options[:dir]).call