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 ‘|’



16
17
18
19
20
21
# File 'app/services/hyrax/file_set_csv_service.rb', line 16

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



8
9
10
# File 'app/services/hyrax/file_set_csv_service.rb', line 8

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



8
9
10
# File 'app/services/hyrax/file_set_csv_service.rb', line 8

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



8
9
10
# File 'app/services/hyrax/file_set_csv_service.rb', line 8

def terms
  @terms
end

Instance Method Details

#csvObject

provide csv version of the GenericFile



24
25
26
27
28
29
30
31
32
# File 'app/services/hyrax/file_set_csv_service.rb', line 24

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



35
36
37
38
39
# File 'app/services/hyrax/file_set_csv_service.rb', line 35

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