Commit 07190d8d authored by Jacob Schatz's avatar Jacob Schatz Committed by Micaël Bergeron

Add file list and move file naming to a method

parent f6774011
...@@ -36,7 +36,7 @@ module Pseudonymity ...@@ -36,7 +36,7 @@ module Pseudonymity
def tables_to_csv def tables_to_csv
tables = config["tables"] tables = config["tables"]
@csv_output = config["output"]["csv"].chomp("\/") @csv_output = config["output"]["csv"].chomp("\g/")
if not File.directory?(@csv_output) if not File.directory?(@csv_output)
puts "No such directory #{@csv_output}" puts "No such directory #{@csv_output}"
return return
...@@ -49,14 +49,20 @@ module Pseudonymity ...@@ -49,14 +49,20 @@ module Pseudonymity
file_list_to_json file_list_to_json
end end
def get_and_log_file_name(ext, prefix=nil, filename=nil)
file_timestamp = filename || "#{prefix}_#{Time.now.to_i}"
file_timestamp = "#{file_timestamp}.#{ext}"
@output_files << file_timestamp
"#{@csv_output}/#{file_timestamp}"
end
def schema_to_yml def schema_to_yml
file_path = "#{@csv_output}/schema_#{Time.now.to_i}.yml" file_path = get_and_log_file_name("yml", "schema")
@output_files << file_path
File.open(file_path, 'w') { |file| file.write(@schema.to_yaml) } File.open(file_path, 'w') { |file| file.write(@schema.to_yaml) }
end end
def file_list_to_json def file_list_to_json
file_path = "#{@csv_output}/file_list.json" file_path = get_and_log_file_name("json", nil, "file_list")
File.open(file_path, 'w') { |file| file.write(@output_files.to_json) } File.open(file_path, 'w') { |file| file.write(@output_files.to_json) }
end end
...@@ -85,7 +91,7 @@ module Pseudonymity ...@@ -85,7 +91,7 @@ module Pseudonymity
end end
def write_to_csv_file(title, contents) def write_to_csv_file(title, contents)
file_path = "#{@csv_output}/#{title}_#{Time.now.to_i}.csv" file_path = get_and_log_file_name("csv", title)
column_names = contents.first.keys column_names = contents.first.keys
contents = CSV.generate do | csv | contents = CSV.generate do | csv |
csv << column_names csv << column_names
...@@ -94,7 +100,6 @@ module Pseudonymity ...@@ -94,7 +100,6 @@ module Pseudonymity
end end
end end
File.open(file_path, 'w') { |file| file.write(contents) } File.open(file_path, 'w') { |file| file.write(contents) }
@output_files << file_path
return file_path return file_path
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment