Class: SequencingKitBoxBarcodeValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Includes:
HasFilters
Defined in:
app/validators/sequencing_kit_box_barcode_validator.rb

Overview

Validator for sequencing kit box barcodes Validates the sequencing kit box barcodes for Revio runs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasFilters

#exclude_marked_for_destruction

Constructor Details

#initialize(options) ⇒ SequencingKitBoxBarcodeValidator

Returns a new instance of SequencingKitBoxBarcodeValidator.

Parameters:

  • options (Hash)

Options Hash (options):

  • :max_number_of_plates (Integer)


12
13
14
15
# File 'app/validators/sequencing_kit_box_barcode_validator.rb', line 12

def initialize(options)
  super
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'app/validators/sequencing_kit_box_barcode_validator.rb', line 8

def options
  @options
end

Instance Method Details

#validate(record) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/validators/sequencing_kit_box_barcode_validator.rb', line 17

def validate(record)
  existing_plates = Pacbio::Plate.where(
    sequencing_kit_box_barcode: record.sequencing_kit_box_barcode
  ).where.not(id: record.id)

  # return unless there are existing sequencing kit barcodes
  return if existing_plates.blank?

  validations = %i[validate_sequencing_kit_box_barcode_count
                   validate_sequencing_kit_box_barcode_positions]
  validations.each do |validation|
    next if record.errors.present?

    send(validation, record, existing_plates)
  end
end

#validate_sequencing_kit_box_barcode_count(record, existing_plates) ⇒ Object

For a given sequencing kit box barcode, check if the number of plates is less than the max number of plates allowed

Parameters:



38
39
40
41
42
43
44
45
# File 'app/validators/sequencing_kit_box_barcode_validator.rb', line 38

def validate_sequencing_kit_box_barcode_count(record, existing_plates)
  return unless existing_plates.count >= options[:max_number_of_plates]

  record
    .errors.add(:plates,
                'sequencing kit box barcode has already been used on ' \
                "#{options[:max_number_of_plates]} plates")
end

#validate_sequencing_kit_box_barcode_positions(record, existing_plates) ⇒ Object

For a given sequencing kit box barcode, check if the positions have already been used

Parameters:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/validators/sequencing_kit_box_barcode_validator.rb', line 50

def validate_sequencing_kit_box_barcode_positions(record, existing_plates)
  # filter wells based on exclusions
  record_wells = filtered(record.wells)
  # Get the common positions between the existing plates and the record wells
  common_positions = existing_plates.map(&:wells).flatten.map(&:position) &
                     record_wells.map(&:position)
  return unless common_positions.any?

  positions = common_positions.join(',')
  record.errors
        .add(:plates,
             "#{positions} have already been used for plate #{record.sequencing_kit_box_barcode}")
end