Class: PickList::RecordCache::ByLabware

Inherits:
PickList::RecordCache show all
Defined in:
app/models/pick_list/record_cache.rb

Overview

Pick-list cache where the source_receptacles are indicated indirectly via source_labware_id: or source_labware_barcode: A pick will be generated for all aliquot containing receptacles in the labware

Constant Summary collapse

KEYS =
%i[source_labware_id source_labware_barcode study_id project_id].freeze

Constants inherited from PickList::RecordCache

LABWARE_KEYS, RECEPTACLE_KEYS

Instance Method Summary collapse

Methods inherited from PickList::RecordCache

#add, #initialize

Constructor Details

This class inherits a constructor from PickList::RecordCache

Instance Method Details

#convert(entry) ⇒ Object


38
39
40
41
42
43
44
45
46
# File 'app/models/pick_list/record_cache.rb', line 38

def convert(entry)
  source_labware_id, source_labware_barcode = entry.values_at(:source_labware_id, :source_labware_barcode)
  source_receptacles = receptacles_for(source_labware_id, source_labware_barcode)
  study_id, project_id = entry.values_at(:study_id, :project_id)
  other_keys = entry.except(*LABWARE_KEYS).to_hash
  source_receptacles.map do |source_receptacle|
    other_keys.merge(source_receptacle: source_receptacle, study: study(study_id), project: project(project_id))
  end
end

#receptacles_for(source_labware_id, source_labware_barcode) ⇒ Object


78
79
80
81
82
83
84
85
86
# File 'app/models/pick_list/record_cache.rb', line 78

def receptacles_for(source_labware_id, source_labware_barcode)
  if source_labware_barcode
    source_receptacle_by_labware_barcode(source_labware_barcode)
  elsif source_labware_id
    source_receptacle_by_labware_id(source_labware_id)
  else
    raise JSONAPI::Exceptions::BadRequest, 'No labware specified'
  end
end

#source_receptacle_by_labware_barcode(barcode) ⇒ Object

This is a little complicated. We can easily find receptacles by the labware barcode, but the problem is the labware itself may have more than one barcode associated with it. If we then group by barcode, which one do we use? The select here adds a found_barcode attribute to the returned record, which matches the barcode we originally looked up the well by. As we're about to use the exact same barcode to pull the information back out the cache, we're fine.


66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/pick_list/record_cache.rb', line 66

def source_receptacle_by_labware_barcode(barcode)
  @source_receptacle_by_labware_barcode ||=
    Receptacle
      .preload(:studies, :projects)
      .with_contents
      .with_barcode(stored(:source_labware_barcode))
      .select_table
      .select('barcodes.barcode AS found_barcode')
      .group_by(&:found_barcode)
  @source_receptacle_by_labware_barcode[barcode] || missing_resource('labware barcodes', barcode)
end

#source_receptacle_by_labware_id(id) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'app/models/pick_list/record_cache.rb', line 48

def source_receptacle_by_labware_id(id)
  @source_receptacle_by_labware_id ||=
    Receptacle
      .preload(:studies, :projects)
      .with_contents
      .where(labware_id: stored(:source_labware_id))
      .group_by(&:labware_id)
  @source_receptacle_by_labware_id[id] || missing_resource('labware ids', id)
end