Class: DataExport::Exporter
- Inherits:
-
Object
- Object
- DataExport::Exporter
- Defined in:
- lib/export_data.rb
Overview
Exporter class for handling data exports to CSV
Class Method Summary collapse
-
.export_to_csv(objects, file_path) ⇒ void
Exports data to a CSV file.
-
.export_to_text(objects, file_path) ⇒ void
Exports data to a text file.
-
.export_to_xls(objects, file_path) ⇒ void
Exports data to an XLS (Excel) file.
Class Method Details
.export_to_csv(objects, file_path) ⇒ void
If the ‘objects` array is empty, this method will output a message and not create a file.
This method returns an undefined value.
Exports data to a CSV file.
This method takes an array of objects and exports them to a CSV file specified by the ‘file_path`. It expects that each object’s ‘attributes` method returns a hash of attribute names and values. The method first writes the header row using the attribute names from the first object, and then writes the data rows using the attribute values of each object.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/export_data.rb', line 35 def self.export_to_csv(objects, file_path) begin CSV.open(file_path, 'w') do |csv| if objects.empty? puts 'No objects to export.' # Or handle this case as needed return end # Write the header row using the first object's attributes csv << objects.first.attributes.keys # Write the object's attribute values as data rows objects.each do |obj| csv << obj.attributes.values end end rescue CSV::MalformedCSVError => e puts "An error occurred while exporting to CSV: #{e.}" rescue Errno::ENOENT => e puts "An error occurred while exporting to CSV: #{e.}" end end |
.export_to_text(objects, file_path) ⇒ void
If the ‘objects` array is empty, this method will output a message and not create a file.
This method returns an undefined value.
Exports data to a text file.
This method takes an array of objects and exports them to a text file specified by the ‘file_path`. It expects that each object’s ‘attributes` method returns an array of attribute values. Each attribute value is joined using a tab character (’t‘) as a delimiter to create tab-separated values in the text file.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/export_data.rb', line 79 def self.export_to_text(objects, file_path) begin File.open(file_path, 'w') do |file| if objects.empty? puts 'No objects to export.' # Or handle this case as needed return end # Write the object's attribute values as text lines objects.each do |obj| file.puts(obj.attributes.values.join("\t")) # Use '\t' as a delimiter for tab-separated values end end rescue Errno::ENOENT => e puts "An error occurred while exporting to text: #{e.}" end end |
.export_to_xls(objects, file_path) ⇒ void
If the ‘objects` array is empty, this method will create an empty XLS file.
This method returns an undefined value.
Exports data to an XLS (Excel) file.
This method takes an array of objects and exports them to an XLS file specified by the ‘file_path`. It expects that each object’s ‘attributes` method returns a hash of attribute names and values. The method creates an XLS workbook with a single worksheet and populates it with data from the objects.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/export_data.rb', line 119 def self.export_to_xls(objects, file_path) begin Axlsx::Package.new do |p| p.workbook.add_worksheet(name: 'Data') do |sheet| if objects.empty? puts 'No objects to export.' # Or handle this case as needed else # Write the header row using the attribute names from the first object sheet.add_row(objects.first.attributes.keys) # Write the object's attribute values as data rows objects.each do |obj| sheet.add_row(obj.attributes.values) end end end p.serialize(file_path) end rescue StandardError => e puts "An error occurred while exporting to XLS: #{e.}" end end |