Class: DataExport::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/export_data.rb

Overview

Exporter class for handling data exports to CSV

Class Method Summary collapse

Class Method Details

.export_to_csv(objects, file_path) ⇒ void

Note:

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.

Examples:

Export user data to a CSV file:

objects = [user1, user2]
DataExport::Exporter.export_to_csv(objects, 'users.csv')

Parameters:

  • objects (Array)

    The objects to export to a CSV file.

  • file_path (String)

    The path to the CSV file to be created.

Raises:

  • (CSV::MalformedCSVError)

    If an error occurs while writing to the CSV file, an error message will be displayed.

  • (Errno::ENOENT)

    If an error occurs while creating or writing to the file, an error message will be displayed.



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.message}"
  rescue Errno::ENOENT => e
    puts "An error occurred while exporting to CSV: #{e.message}"
  end
end

.export_to_text(objects, file_path) ⇒ void

Note:

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.

Examples:

Export user data to a text file:

objects = [user1, user2]
DataExport::Exporter.export_to_text(objects, 'users.txt')

Parameters:

  • objects (Array)

    The objects to export to a text file.

  • file_path (String)

    The path to the text file to be created.

Raises:

  • (Errno::ENOENT)

    If an error occurs while creating or writing to the file, an error message will be displayed.



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.message}"
  end
end

.export_to_xls(objects, file_path) ⇒ void

Note:

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.

Examples:

Export user data to an XLS file:

objects = [user1, user2]
DataExport::Exporter.export_to_xls(objects, 'users.xlsx')

Parameters:

  • objects (Array)

    The objects to export to an XLS file.

  • file_path (String)

    The path to the XLS file to be created.

Raises:

  • (StandardError)

    If an error occurs while writing to the XLS file, an error message will be displayed.



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.message}"
  end
end