Class: PlateVolume

Inherits:
ApplicationRecord show all
Extended by:
DbFile::Uploader
Defined in:
app/models/plate_volume.rb

Constant Summary collapse

ASSAY_TYPE =
'Volume Check'.freeze
ASSAY_VERSION =
'1.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DbFile::Uploader

extended, has_uploaded

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Class Method Details

.all_plate_volume_file_names(folder) ⇒ Object


72
73
74
# File 'app/models/plate_volume.rb', line 72

def all_plate_volume_file_names(folder)
  Dir.entries(folder).reject { |f| File.directory?(File.join(folder, f)) }
end

.find_for_filename(filename) ⇒ Object


92
93
94
95
# File 'app/models/plate_volume.rb', line 92

def find_for_filename(filename)
  find_by(uploaded_file_name: filename) or
    ->(filename, file) { PlateVolume.create!(uploaded_file_name: filename, updated_at: file.stat.mtime, uploaded: file) }
end

.handle_volume(filename, file) ⇒ Object


77
78
79
80
81
82
83
# File 'app/models/plate_volume.rb', line 77

def handle_volume(filename, file)
  ActiveRecord::Base.transaction do
    find_for_filename(sanitized_filename(file)).call(filename, file)
  end
rescue => e
  Rails.logger.warn("Error processing volume file #{filename}: #{e.message}")
end

.process_all_volume_check_files(folder = configatron.plate_volume_files) ⇒ Object


64
65
66
67
68
69
70
# File 'app/models/plate_volume.rb', line 64

def process_all_volume_check_files(folder = configatron.plate_volume_files)
  all_plate_volume_file_names(folder).each do |filename|
    File.open(File.join(folder, filename), 'r') do |file|
      catch(:no_source_plate) { handle_volume(filename, file) }
    end
  end
end

.sanitized_filename(file) ⇒ Object


86
87
88
89
90
# File 'app/models/plate_volume.rb', line 86

def sanitized_filename(file)
  # We need to use the Carrierwave sanitized filename for lookup, else files with spaces are repetedly processed
  # Later versions of carrierwave expose this sanitization better, but for now we are forced to create an object
  CarrierWave::SanitizedFile.new(file).filename
end

Instance Method Details

#call(filename, file) ⇒ Object


18
19
20
21
22
23
24
# File 'app/models/plate_volume.rb', line 18

def call(filename, file)
  return unless update_required?(file.stat.mtime)

  db_files.map(&:destroy)
  reload
  update!(uploaded_file_name: filename, updated_at: file.stat.mtime, uploaded: file)
end

#update_required?(modified_timestamp) ⇒ Boolean

Is an update required given the timestamp specified

Returns:

  • (Boolean)

14
15
16
# File 'app/models/plate_volume.rb', line 14

def update_required?(modified_timestamp)
  updated_at.to_i < modified_timestamp.to_i
end