Class: Asset::Finder

Inherits:
Object
  • Object
show all
Defined in:
app/models/asset/finder.rb

Overview

Taked barcodes and locations and returns matching assets eg DN123456P:A1,A2,A3 (Wells A1,A2,A3) DN123456P:1,2,3 (Columns 1,2,3) DN123456P:A,B (Rows A,B) DN123456P (Entire Plate) Imported from SubmissionCreator with minor adaptations to support tubes. Could do with refactoring and performance improvements

Constant Summary collapse

InvalidInputException =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(barcode_locations) ⇒ Finder

Returns a new instance of Finder.



15
16
17
# File 'app/models/asset/finder.rb', line 15

def initialize(barcode_locations)
  @barcodes_wells = barcode_locations.map { |bc_well| bc_well.split(':') }
end

Instance Attribute Details

#barcodes_wellsObject (readonly)

Returns the value of attribute barcodes_wells.



13
14
15
# File 'app/models/asset/finder.rb', line 13

def barcodes_wells
  @barcodes_wells
end

#processed_barcodesObject (readonly)

Returns the value of attribute processed_barcodes.



13
14
15
# File 'app/models/asset/finder.rb', line 13

def processed_barcodes
  @processed_barcodes
end

Instance Method Details

#barcodesObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



58
59
60
# File 'app/models/asset/finder.rb', line 58

def barcodes
  barcodes_wells.map(&:first).uniq
end

#find_wells_in_array(plate, well_array) ⇒ Object

rubocop:todo Metrics/MethodLength, Metrics/AbcSize



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/asset/finder.rb', line 31

def find_wells_in_array(plate, well_array) # rubocop:todo Metrics/CyclomaticComplexity
  return plate.wells.in_column_major_order.with_aliquots.distinct if well_array.empty?

  well_array.flat_map do |map_description|
    case map_description
    when /^[a-z,A-Z][0-9]+$/
      # A well
      well = plate.find_well_by_name(map_description)
      if well.nil? || well.aliquots.empty?
        raise InvalidInputException, "Well #{map_description} on #{plate.human_barcode} does not exist or is empty."
      end

      well
    when /^[a-z,A-Z]$/
      # A row
      plate.wells.with_aliquots.in_plate_row(map_description, plate.size).distinct
    when /^[0-9]+$/
      # A column
      plate.wells.with_aliquots.in_plate_column(map_description, plate.size).distinct
    else
      raise InvalidInputException, "#{map_description} is not a valid well location"
    end
  end
end

#resolveObject



19
20
21
22
23
24
25
26
27
28
# File 'app/models/asset/finder.rb', line 19

def resolve
  barcodes_wells.flat_map do |labware_barcode, well_locations|
    labware = Labware.find_by_barcode(labware_barcode)
    raise InvalidInputException, "No labware found for barcode #{labware_barcode}." if labware.nil?

    well_array = (well_locations || '').split(',').map(&:strip).compact_blank

    labware.respond_to?(:wells) ? find_wells_in_array(labware, well_array) : labware
  end
end