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].freeze

Class Method Summary collapse

Instance Method Summary collapse

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

.build_sanger_barcode(attributes, format:) ⇒ Object


51
52
53
54
55
# File 'app/models/barcode.rb', line 51

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


47
48
49
# File 'app/models/barcode.rb', line 47

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

.build_sanger_ean13(attributes) ⇒ Object


43
44
45
# File 'app/models/barcode.rb', line 43

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

.exists_for_format?(barcode_format, search_barcode) ⇒ Boolean

Returns:

  • (Boolean)

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

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


58
59
60
61
62
# File 'app/models/barcode.rb', line 58

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

.extract_barcodes(barcodes) ⇒ Object


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

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


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

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.


93
94
95
96
# File 'app/models/barcode.rb', line 93

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

#handlerObject


84
85
86
# File 'app/models/barcode.rb', line 84

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

#handler_class_nameObject


88
89
90
# File 'app/models/barcode.rb', line 88

def handler_class_name
  format.classify
end

#sanger_barcode?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'app/models/barcode.rb', line 98

def sanger_barcode?
  sanger_ean13? || sanger_code39?
end