Class: Runoff::FileWriter
- Inherits:
-
Object
- Object
- Runoff::FileWriter
- Defined in:
- lib/runoff/file_writer.rb
Overview
Public: Writes data received from the database to text files.
Instance Method Summary collapse
-
#archive ⇒ Object
Public: Saves all the exported files in a Zip archive.
-
#initialize(options) ⇒ FileWriter
constructor
Public: Initializes a FileWriter object.
-
#write(messages) ⇒ Object
Public: Writes a single row of data to a text file.
Constructor Details
#initialize(options) ⇒ FileWriter
Public: Initializes a FileWriter object.
options - A Hash with commandline options.
10 11 12 13 14 15 |
# File 'lib/runoff/file_writer.rb', line 10 def initialize() @export_path = Location.get_export_path @adapter = Object.const_get("Runoff::Adapters::#{[:adapter]}").new @current_file_name = nil @buffer = [] end |
Instance Method Details
#archive ⇒ Object
Public: Saves all the exported files in a Zip archive.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/runoff/file_writer.rb', line 38 def archive archive_name = "#{@export_path}_#{Time.now.to_i}.zip" Zip::File.open archive_name, Zip::File::CREATE do |archive| Dir[File.join(@export_path, '**', '**')].each do |file| archive.add File.basename(file), file end end FileUtils.rm_rf @export_path # Delete the folder. end |
#write(messages) ⇒ Object
Public: Writes a single row of data to a text file.
messages - An Array of data received from the database.
Examples
write [{ chatname: "#first_user/$second_user;d3d86c6b0e3b8320" ... }, ...]
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/runoff/file_writer.rb', line 24 def write() .each_with_index do |m, i| file_name = @adapter.get_file_name m[Runoff::COLUMNS[1]] dump unless @current_file_name.nil? || @current_file_name == file_name @current_file_name = file_name @buffer << @adapter.build_entry(m) dump if i == (.count - 1) end end |