Sha256: a6a53212bcf073220e0e350022b2138542af97bd756bf2a9e2d1a96b60e713f3

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Bulkrax
  module ImporterExporterBehavior
    extend ActiveSupport::Concern

    def parser
      @parser ||= parser_class.new(self)
    end

    def parser_class
      self.parser_klass.constantize
    end

    def last_imported_at
      @last_imported_at ||= self.importer_runs.last&.created_at
    end

    def next_import_at
      (last_imported_at || Time.current) + frequency.to_seconds if schedulable? && last_imported_at.present?
    end

    def increment_counters(index, collection: false, file_set: false)
      # Only set the totals if they were not set on initialization
      importer_run = ImporterRun.find(current_run.id) # make sure fresh
      if collection
        importer_run.total_collection_entries = index + 1 unless parser.collections_total.positive?
      elsif file_set
        importer_run.total_file_set_entries = index + 1 unless parser.file_sets_total.positive?
      else
        # TODO: differentiate between work and collection counts for exporters
        importer_run.total_work_entries = index + 1 unless limit.to_i.positive? || parser.total.positive?
      end
      importer_run.enqueued_records += 1
      importer_run.save!
    end

    def keys_without_numbers(keys)
      keys.map { |key| key_without_numbers(key) }
    end

    def key_without_numbers(key)
      key.gsub(/_\d+/, '').sub(/^\d+_/, '')
    end

    # Is this a file?
    def file?
      parser_fields&.[]('import_file_path') && File.file?(parser_fields['import_file_path'])
    end

    # Is this a zip file?
    def zip?
      parser_fields&.[]('import_file_path') && MIME::Types.type_for(parser_fields['import_file_path']).include?('application/zip')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bulkrax-2.3.0 app/models/concerns/bulkrax/importer_exporter_behavior.rb
bulkrax-2.2.4 app/models/concerns/bulkrax/importer_exporter_behavior.rb
bulkrax-2.2.3 app/models/concerns/bulkrax/importer_exporter_behavior.rb