Class: DataShift::CsvExporter

Inherits:
ExporterBase show all
Includes:
ColumnPacker, Logging
Defined in:
lib/exporters/csv_exporter.rb

Instance Attribute Summary

Attributes included from Delimiters

#attribute_list_end, #attribute_list_start, #csv_delimiter, #key_value_sep, #text_delim

Attributes inherited from ExporterBase

#configuration, #file_name

Instance Method Summary collapse

Methods included from ColumnPacker

#escape_for_csv, #escape_text_delim, #record_to_column, #record_to_csv

Methods included from Delimiters

#column_delim, #column_delim=, #eol, #multi_assoc_delim, #multi_assoc_delim=, #multi_facet_delim, #multi_value_delim, #multi_value_delim=, #name_value_delim, #name_value_delim=, #setmulti_facet_delim

Methods included from Logging

#logdir, #logdir=, #logger, #verbose

Constructor Details

#initializeCsvExporter

Returns a new instance of CsvExporter.



16
17
18
19
20
# File 'lib/exporters/csv_exporter.rb', line 16

def initialize
  super

  @csv_delimiter = ','
end

Instance Method Details

#export(file_name, export_records, options = {}) ⇒ Object

Create CSV file from set of ActiveRecord objects

Options :

:csv_delim => Char to use to delim columns, useful when data contain embedded ‘,’

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/exporters/csv_exporter.rb', line 28

def export(file_name, export_records, options = {})

  @file_name = file_name

  records = [*export_records]

  unless records && !records.empty?
    logger.warn('No objects supplied for export')
    return
  end

  first = records[0]

  raise ArgumentError.new('Please supply set of ActiveRecord objects to export') unless first.is_a?(ActiveRecord::Base)

  @csv_delimiter = options[:csv_delim] if options[:csv_delim]

  headers = Headers.klass_to_headers(first.class)

  logger.debug "Writing out CSV Export. Columns delimited by [#{csv_delimiter}]"

  remove_list = DataShift::Transformation::Remove.new.remove_list

  CSV.open(file_name, 'w', col_sep: csv_delimiter ) do |csv|
    csv << headers.sources

    records.each do |r|
      next unless r.is_a?(ActiveRecord::Base)
      csv.ar_to_row(r, remove_list)
    end
  end

  logger.info "CSV export completed for #{records.size} records"
end

#export_with_associations(file_name, klass, records, options = {}) ⇒ Object

Create CSV file from list of ActiveRecord objects

Options :

:csv_delim => Char to use to delim columns, useful when data contain embedded ‘,’



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/exporters/csv_exporter.rb', line 69

def export_with_associations(file_name, klass, records, options = {})

  state = DataShift::Configuration.call.with

  DataShift::Configuration.call.with = :all

  @file_name = file_name

  @csv_delimiter = options[:csv_delim] if(options[:csv_delim])

  headers = Headers.klass_to_headers(klass)

  schema = DataFlowSchema.new

  model_methods = schema.klass_to_model_methods( klass )

  logger.debug "Writing out CSV Export for #{klass} with Associations. Columns delimited by [#{csv_delimiter}]"

  CSV.open(file_name, 'w', col_sep: csv_delimiter ) do |csv|
    csv << headers.sources

    records.each do |record|
      row = []

      model_methods.each do |model_method|
        row << if model_method.association_type?
                 record_to_column( record.send(model_method.operator) )
               else
                 escape_for_csv( record.send(model_method.operator) )
               end
      end
      csv.add_row(row)
    end
  end

ensure
  DataShift::Configuration.call.with = state
end