Module: WorkerTools::CsvOutput
- Defined in:
- lib/worker_tools/csv_output.rb
Instance Method Summary collapse
- #csv_ouput_ensure_target_folder ⇒ Object
- #csv_output_col_sep ⇒ Object
- #csv_output_column_headers ⇒ Object
- #csv_output_encoding ⇒ Object
- #csv_output_entries ⇒ Object
- #csv_output_insert_headers(csv) ⇒ Object
-
#csv_output_row_values(entry) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#csv_output_target ⇒ Object
if defined, this file will be written to this destination (regardless of whether the model saves the file as well).
- #csv_output_target_file_name ⇒ Object
- #csv_output_tmp_file ⇒ Object
- #csv_output_write_file ⇒ Object
- #csv_output_write_target ⇒ Object
-
#cvs_output_target_folder ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
Instance Method Details
#csv_ouput_ensure_target_folder ⇒ Object
49 50 51 |
# File 'lib/worker_tools/csv_output.rb', line 49 def csv_ouput_ensure_target_folder FileUtils.mkdir_p(cvs_output_target_folder) unless File.directory?(cvs_output_target_folder) end |
#csv_output_col_sep ⇒ Object
57 58 59 |
# File 'lib/worker_tools/csv_output.rb', line 57 def csv_output_col_sep ';' end |
#csv_output_column_headers ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/worker_tools/csv_output.rb', line 16 def csv_output_column_headers # These columns are used to set the headers, also # to set the row values depending on your implementation. # # To ignore them set it to _false_ # # Ex: # @csv_output_column_headers ||= { # foo: 'Foo Header', # bar: 'Bar Header' # } raise "csv_output_column_headers has to be defined in #{self}" end |
#csv_output_encoding ⇒ Object
61 62 63 |
# File 'lib/worker_tools/csv_output.rb', line 61 def csv_output_encoding Encoding::UTF_8 end |
#csv_output_entries ⇒ Object
12 13 14 |
# File 'lib/worker_tools/csv_output.rb', line 12 def csv_output_entries raise "csv_output_entries has to be defined in #{self}" end |
#csv_output_insert_headers(csv) ⇒ Object
65 66 67 |
# File 'lib/worker_tools/csv_output.rb', line 65 def csv_output_insert_headers(csv) csv << csv_output_column_headers.values if csv_output_column_headers end |
#csv_output_row_values(entry) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
31 32 33 34 35 36 37 38 |
# File 'lib/worker_tools/csv_output.rb', line 31 def csv_output_row_values(entry) # Ex: # { # foo: entry.foo, # bar: entry.bar # }.values_at(*csv_output_column_headers.keys) raise "csv_output_row_values has to be defined in #{self}" end |
#csv_output_target ⇒ Object
if defined, this file will be written to this destination (regardless of whether the model saves the file as well)
7 8 9 10 |
# File 'lib/worker_tools/csv_output.rb', line 7 def csv_output_target # Ex: Rails.root.join('shared', 'foo', 'bar.csv') false end |
#csv_output_target_file_name ⇒ Object
45 46 47 |
# File 'lib/worker_tools/csv_output.rb', line 45 def csv_output_target_file_name File.basename(csv_output_target) end |
#csv_output_tmp_file ⇒ Object
53 54 55 |
# File 'lib/worker_tools/csv_output.rb', line 53 def csv_output_tmp_file @csv_output_tmp_file ||= Tempfile.new(['output', '.csv']) end |
#csv_output_write_file ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/worker_tools/csv_output.rb', line 69 def csv_output_write_file CSV.open(csv_output_tmp_file, 'wb', col_sep: csv_output_col_sep, encoding: csv_output_encoding) do |csv| csv_output_insert_headers(csv) csv_output_entries.each { |entry| csv << csv_output_row_values(entry) } end csv_output_write_target if csv_output_target end |
#csv_output_write_target ⇒ Object
77 78 79 80 |
# File 'lib/worker_tools/csv_output.rb', line 77 def csv_output_write_target csv_ouput_ensure_target_folder FileUtils.cp(csv_output_tmp_file.path, csv_output_target) end |
#cvs_output_target_folder ⇒ Object
rubocop:enable Lint/UnusedMethodArgument
41 42 43 |
# File 'lib/worker_tools/csv_output.rb', line 41 def cvs_output_target_folder File.dirname(csv_output_target) end |