Module: Bulkrax::ImporterExporterBehavior
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/bulkrax/importer_exporter_behavior.rb
Instance Method Summary collapse
-
#file? ⇒ Boolean
Is this a file?.
- #increment_counters(index, collection: false, file_set: false, work: false) ⇒ Object
- #key_without_numbers(key) ⇒ Object
- #keys_without_numbers(keys) ⇒ Object
- #last_imported_at ⇒ Object
- #next_import_at ⇒ Object
- #parser ⇒ Object
- #parser_class ⇒ Object
-
#zip? ⇒ Boolean
Is this a zip file?.
Instance Method Details
#file? ⇒ Boolean
Is this a file?
47 48 49 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 47 def file? parser_fields&.[]('import_file_path') && File.file?(parser_fields['import_file_path']) end |
#increment_counters(index, collection: false, file_set: false, work: false) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 23 def increment_counters(index, collection: false, file_set: false, work: 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? elsif work # 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 |
#key_without_numbers(key) ⇒ Object
42 43 44 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 42 def key_without_numbers(key) key.gsub(/_\d+/, '').sub(/^\d+_/, '') end |
#keys_without_numbers(keys) ⇒ Object
38 39 40 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 38 def keys_without_numbers(keys) keys.map { |key| key_without_numbers(key) } end |
#last_imported_at ⇒ Object
15 16 17 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 15 def last_imported_at @last_imported_at ||= self.importer_runs.last&.created_at end |
#next_import_at ⇒ Object
19 20 21 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 19 def next_import_at (last_imported_at || Time.current) + frequency.to_seconds if schedulable? && last_imported_at.present? end |
#parser ⇒ Object
7 8 9 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 7 def parser @parser ||= parser_class.new(self) end |
#parser_class ⇒ Object
11 12 13 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 11 def parser_class self.parser_klass.constantize end |
#zip? ⇒ Boolean
Is this a zip file?
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/concerns/bulkrax/importer_exporter_behavior.rb', line 52 def zip? filename = parser_fields&.[]('import_file_path') return false unless filename return false unless File.file?(filename) returning_value = false File.open(filename) do |file| mime_type = ::Marcel::MimeType.for(file) returning_value = mime_type.include?('application/zip') || mime_type.include?('application/gzip') end returning_value end |