Class: Hyrax::FileSetCsvService

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

Overview

Generates CSV from a FileSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, terms = nil, multi_value_separator = '|') ⇒ FileSetCsvService

Returns a new instance of FileSetCsvService.

Parameters:

  • file (SolrDocument)

    solr document that will be examined to generate the CSVs

  • terms (Array) (defaults to: nil)

    list of terms that will be output in CSV form defaults if nil to list below

  • multi_value_separator (String) (defaults to: '|')

    separator for terms that have more than one value defaults to ‘|’



19
20
21
22
23
24
# File 'app/services/hyrax/file_set_csv_service.rb', line 19

def initialize(file, terms = nil, multi_value_separator = '|')
  @file_set = file
  @terms = terms
  @terms ||= [:id, :title, :depositor, :creator, :visibility, :resource_type, :license, :file_format]
  @multi_value_separator = multi_value_separator
end

Instance Attribute Details

#file_setFileSet (readonly)

file that will be examined to generate the CSVs

Returns:

  • (FileSet)

    the current value of file_set



11
12
13
# File 'app/services/hyrax/file_set_csv_service.rb', line 11

def file_set
  @file_set
end

#multi_value_separatorString (readonly)

separator for terms that have more than one value

Returns:

  • (String)

    the current value of multi_value_separator



11
12
13
# File 'app/services/hyrax/file_set_csv_service.rb', line 11

def multi_value_separator
  @multi_value_separator
end

#termsArray (readonly)

list of terms that will be output in CSV form

Returns:

  • (Array)

    the current value of terms



11
12
13
# File 'app/services/hyrax/file_set_csv_service.rb', line 11

def terms
  @terms
end

Instance Method Details

#csvObject

provide csv version of the GenericFile



27
28
29
30
31
32
33
34
35
# File 'app/services/hyrax/file_set_csv_service.rb', line 27

def csv
  ::CSV.generate do |csv|
    csv << terms.map do |term|
      values = file_set.send(term)
      values = values.respond_to?(:to_a) ? values.to_a : [values] # make sure we have an array
      values.join(multi_value_separator)
    end
  end
end

#csv_headerObject

provide csv header line for a GenericFile



38
39
40
41
42
# File 'app/services/hyrax/file_set_csv_service.rb', line 38

def csv_header
  ::CSV.generate do |csv|
    csv << terms
  end
end