Class: Barcode

Inherits:
ApplicationRecord show all
Extended by:
SBCF::LegacyMethods
Defined in:
app/models/barcode.rb

Overview

A barcode is an identifier for a piece of labware which is attached via a printed label. Barcodes may either be generated by Sequencescape, or may get supplied externally. In some cases labware may have more than one barcode assigned.

Author:

  • [grl]

Defined Under Namespace

Modules: Barcodeable, FormatHandlers

Constant Summary collapse

FOREIGN_BARCODE_FORMATS =

Barcode formats which may be submitted via sample manifests

%i[
  cgap
  fluidx_barcode
  fluidigm
  uk_biocentre_v1
  uk_biocentre_v2
  uk_biocentre_unid
  alderly_park_v1
  alderly_park_v2
  uk_biocentre_v3
  cgap_plate
  cgap_rack
  glasgow
  cambridge_a_z
  heron_tailed
  randox
  uk_biocentre_v4
  cambridge_a_z_v2
  glasgow_v2
  eagle
  cambridge_a_z_eagle
  glasgow_eagle
  uk_biocentre_eagle
  alderley_park_eagle
  randox_eagle
  randox_v2
  glasgow_v3
  uk_biocentre_v5
  health_services_laboratories_v1
  uk_biocentre_v6
  brants_bridge
  leamington_spa
  newcastle
  brants_bridge_v2
  uk_biocentre_v7
  east_london_genes_and_health
  leamington_spa_v2
  east_london_genes_and_health_v2
  sequencescape22
  plymouth_v2
  leamington_spa_v3
  brants_bridge_v3
  ibd_response
  rvi
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Class Method Details

.build_sanger_barcode(attributes, format:) ⇒ Object



155
156
157
158
159
# File 'app/models/barcode.rb', line 155

def self.build_sanger_barcode(attributes, format:)
  # We need to symbolize our hash keys to allow them to get passed in to named arguments.
  safe_attributes = attributes.slice(:number, :prefix, :human_barcode, :machine_barcode).symbolize_keys
  new(format: format, barcode: SBCF::SangerBarcode.new(**safe_attributes).human_barcode)
end

.build_sanger_code39(attributes) ⇒ Object



147
148
149
# File 'app/models/barcode.rb', line 147

def self.build_sanger_code39(attributes)
  build_sanger_barcode(attributes, format: :sanger_code39)
end

.build_sanger_ean13(attributes) ⇒ Object



143
144
145
# File 'app/models/barcode.rb', line 143

def self.build_sanger_ean13(attributes)
  build_sanger_barcode(attributes, format: :sanger_ean13)
end

.build_sequencescape22(attributes) ⇒ Object



151
152
153
# File 'app/models/barcode.rb', line 151

def self.build_sequencescape22(attributes)
  new(format: :sequencescape22, barcode: attributes[:barcode])
end

.exists_for_format?(barcode_format, search_barcode) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
# File 'app/models/barcode.rb', line 176

def self.exists_for_format?(barcode_format, search_barcode)
  Barcode.exists?(format: barcode_format, barcode: search_barcode)
end

.extract_barcode(barcode) ⇒ Object

Extract barcode from user input



162
163
164
165
166
# File 'app/models/barcode.rb', line 162

def self.extract_barcode(barcode)
  [barcode.to_s].tap { |barcodes| barcodes << SBCF::SangerBarcode.from_user_input(barcode.to_s).human_barcode }
    .compact
    .uniq
end

.extract_barcodes(barcodes) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'app/models/barcode.rb', line 180

def self.extract_barcodes(barcodes)
  barcodes
    .flatten
    .each_with_object([]) do |source_bc, store|
      next if source_bc.blank?

      store.concat(Barcode.extract_barcode(source_bc))
    end
end

.matching_barcode_format(possible_barcode) ⇒ Object

Returns the barcode format matching the supplied barcode



169
170
171
172
173
174
# File 'app/models/barcode.rb', line 169

def self.matching_barcode_format(possible_barcode)
  FOREIGN_BARCODE_FORMATS.detect do |cur_format|
    bc = Barcode.new(format: cur_format, barcode: possible_barcode)
    bc.handler.valid?
  end
end

Instance Method Details

#barcode=(new_barcode) ⇒ Object

If the barcode changes, we’ll need a new handler. This allows handlers themselves to be immutable.



203
204
205
206
# File 'app/models/barcode.rb', line 203

def barcode=(new_barcode)
  @handler = nil
  super
end

#child_barcodesObject



212
213
214
# File 'app/models/barcode.rb', line 212

def child_barcodes
  Barcode.where('barcode LIKE ?', "#{barcode}-%")
end

#handlerObject



194
195
196
# File 'app/models/barcode.rb', line 194

def handler
  @handler ||= handler_class.new(barcode)
end

#handler_class_nameObject



198
199
200
# File 'app/models/barcode.rb', line 198

def handler_class_name
  format.classify
end

#sanger_barcode?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'app/models/barcode.rb', line 208

def sanger_barcode?
  sanger_ean13? || sanger_code39?
end

#sequencescape22?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'app/models/barcode.rb', line 190

def sequencescape22?
  format == 'sequencescape22'
end