class BerkeleyLibrary::TIND::Export::Exporter

Superclass of exporters for different formats

Attributes

collection[R]

@return [String] the collection name

exportable_only[R]

@return [Boolean] whether to include only exportable fields

Public Class Methods

new(collection, exportable_only: true) click to toggle source

Initializes a new exporter

@param collection [String] The collection name @param exportable_only [Boolean] whether to include only exportable fields

# File lib/berkeley_library/tind/export/exporter.rb, line 29
def initialize(collection, exportable_only: true)
  @collection = collection
  @exportable_only = exportable_only
end

Public Instance Methods

any_results?() click to toggle source

Returns true if the collection can be exported, false otherwise. Note that this requires reading the collection data from the TIND server; failures will be fast but success may be slow. (On the other hand, the retrieved collection data is cached, so the subsequent export will not need to retrieve it again.)

# File lib/berkeley_library/tind/export/exporter.rb, line 55
def any_results?
  !_export_table.empty?
end
export(out = nil) click to toggle source

Exports the collection @param out [IO, String, Pathname, nil] the IO or file path to write the

exported data to, or nil to return a string

rubocop:disable Lint/UnusedMethodArgument

# File lib/berkeley_library/tind/export/exporter.rb, line 41
def export(out = nil)
  # This is a stub, used for documentation
  raise NoMethodError, "#{self.class} does not implement `export`"
end
respond_to?(*args) click to toggle source

Object overrides

Calls superclass method
# File lib/berkeley_library/tind/export/exporter.rb, line 62
def respond_to?(*args)
  return false if instance_of?(Exporter) && (args && args.first.to_s == 'export')

  super
end

Protected Instance Methods

export_table() click to toggle source

Returns a table of all records in the specified collection

@return [Export::Table] the table @raise NoResultsError if no search results were returned for the collection

# File lib/berkeley_library/tind/export/exporter.rb, line 78
def export_table
  # TODO: something more clever. Search.has_results?
  return _export_table unless _export_table.empty?

  raise no_results_error
end

Private Instance Methods

_export_table() click to toggle source
# File lib/berkeley_library/tind/export/exporter.rb, line 91
def _export_table
  @_export_table ||= begin
    logger.info("Reading collection #{collection.inspect}")
    results = API::Search.new(collection: collection).each_result(freeze: true)

    logger.info('Creating export table')
    # noinspection RubyYardParamTypeMatch
    Export::Table.from_records(results, freeze: true, exportable_only: exportable_only)
  end
end
no_results_error() click to toggle source
# File lib/berkeley_library/tind/export/exporter.rb, line 87
def no_results_error
  NoResultsError.new("No records returned for collection #{collection.inspect}")
end