Class: CdmMigrator::CsvExportService

Inherits:
Object
  • Object
show all
Defined in:
app/services/csv_export_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(work_types) ⇒ CsvExportService

Returns a new instance of CsvExportService.

Parameters:

  • - (Array <Class>)

    the available work types, passed in from the controller, such as GenericWork

[View source]

9
10
11
# File 'app/services/csv_export_service.rb', line 9

def initialize(work_types)
  @work_types = work_types
end

Instance Method Details

#csv_for(work_ids) ⇒ Object

[View source]

19
20
21
# File 'app/services/csv_export_service.rb', line 19

def csv_for(work_ids)
  rows_for(work_ids).map(&:to_csv).join
end

#row_for(document) ⇒ Array <String>

Returns - the csv row for the given document.

Parameters:

  • - (SolrDocument)

    Any model that has the properties listed in #included_fields (e.g. GenericWork, FileSet)

Returns:

  • (Array <String>)
    • the csv row for the given document

[View source]

40
41
42
43
44
45
46
47
# File 'app/services/csv_export_service.rb', line 40

def row_for(document)
  line_hash = {}
  line_hash['type'] = document._source[:has_model_ssim].first
  included_fields.each do |field|
    line_hash[field] = create_cell document, field
  end
  line_hash.values_at(*csv_headers).map { |cell| cell.blank? ? '' : cell }
end

#rows_for(work_ids) ⇒ Array <Array>

for a work or file set and corresponds to a csv row.

Parameters:

  • - (Array <String>)

    the work ids (for GenericWork or other work type) to export metadata for

Returns:

  • (Array <Array>)
    • An array of arrays where each nested array contains the metadata

[View source]

26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/csv_export_service.rb', line 26

def rows_for(work_ids)
  csv_array = [csv_headers]
  work_ids.each_with_object(csv_array).each do |work_id|
    doc = ::SolrDocument.find work_id
    csv_array << row_for(doc)
    doc._source[:file_set_ids_ssim].each do |file_id|
      file_doc = ::SolrDocument.find file_id
      csv_array << row_for(file_doc)
    end
  end
end

#write_to_csv(work_ids, filepath) ⇒ Object

Parameters:

  • - (Array <String>)

    the work ids (for GenericWork or other work type) to export metadata for

  • - (String)

    where to save the csv file to (filepath)

[View source]

15
16
17
# File 'app/services/csv_export_service.rb', line 15

def write_to_csv(work_ids, filepath)
  File.open(filepath, 'w') { |file| file.write(rows_for(work_ids).map(&:to_csv).join) }
end