Class: CdmMigrator::CsvExportService
- Inherits:
-
Object
- Object
- CdmMigrator::CsvExportService
- Defined in:
- app/services/csv_export_service.rb
Instance Method Summary collapse
- #csv_for(work_ids) ⇒ Object
-
#initialize(work_types) ⇒ CsvExportService
constructor
A new instance of CsvExportService.
-
#row_for(document) ⇒ Array <String>
-
the csv row for the given document.
-
-
#rows_for(work_ids) ⇒ Array <Array>
for a work or file set and corresponds to a csv row.
- #write_to_csv(work_ids, filepath) ⇒ Object
Constructor Details
permalink #initialize(work_types) ⇒ CsvExportService
Returns a new instance of CsvExportService.
9 10 11 |
# File 'app/services/csv_export_service.rb', line 9 def initialize(work_types) @work_types = work_types end |
Instance Method Details
permalink #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 |
permalink #row_for(document) ⇒ Array <String>
Returns - the csv row for the given document.
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 |
permalink #rows_for(work_ids) ⇒ Array <Array>
for a work or file set and corresponds to a csv row.
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 |
permalink #write_to_csv(work_ids, filepath) ⇒ Object
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 |