Class: Cure::Export::Exporter
- Inherits:
-
Object
- Object
- Cure::Export::Exporter
- Includes:
- Configuration, Helpers::FileHelpers, Log
- Defined in:
- lib/cure/export/exporter.rb
Instance Attribute Summary collapse
-
#output_dir ⇒ Object
readonly
Returns the value of attribute output_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #export(output_dir, file_name, rows, columns) ⇒ Object
- #export_results(result) ⇒ Object
-
#initialize(output_dir) ⇒ Exporter
constructor
A new instance of Exporter.
- #write_to_file(file_path, file_name, file_extension, contents) ⇒ Object
Methods included from Log
#log_debug, #log_error, #log_info, #log_warn
Methods included from Configuration
#config, #create_config, #register_config
Methods included from Helpers::FileHelpers
#clean_dir, #read_file, #with_file, #with_temp_dir
Constructor Details
#initialize(output_dir) ⇒ Exporter
Returns a new instance of Exporter.
22 23 24 |
# File 'lib/cure/export/exporter.rb', line 22 def initialize(output_dir) @output_dir = output_dir end |
Instance Attribute Details
#output_dir ⇒ Object (readonly)
Returns the value of attribute output_dir.
20 21 22 |
# File 'lib/cure/export/exporter.rb', line 20 def output_dir @output_dir end |
Class Method Details
Instance Method Details
#export(output_dir, file_name, rows, columns) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cure/export/exporter.rb', line 44 def export(output_dir, file_name, rows, columns) file_name = "#{file_name}-#{Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S%-z")}" log_info("Exporting file to [#{output_dir}/#{file_name}] with #{rows.length} rows") file_contents = [] file_contents << columns.join(",") rows.each do |row| file_contents << row.join(",") end write_to_file( output_dir, file_name, "csv", file_contents.join("\n") ) end |
#export_results(result) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cure/export/exporter.rb', line 27 def export_results(result) export_ranges = config.template.dispatch.named_ranges export_ranges.each do |range| named_range = range["named_range"] unless result.has_key?(named_range) raise "Missing named range - #{range} from candidates [#{result.keys.join(", ")}]" end data = result[named_range] column_headers = data.column_headers.keys export(@output_dir, range["file_name"], data.transformed_rows, column_headers) end end |
#write_to_file(file_path, file_name, file_extension, contents) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/cure/export/exporter.rb', line 64 def write_to_file(file_path, file_name, file_extension, contents) file_location = "#{file_path}/#{file_name}" clean_dir(file_path) with_file(file_location, file_extension) do |file| file.write(contents) end end |