Module: Barcode::Barcodeable

Included in:
Plate, Tube, TubeRack
Defined in:
app/models/barcode/barcodeable.rb

Overview

Anything that has a barcode is considered barcodeable.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
# File 'app/models/barcode/barcodeable.rb', line 5

def self.included(base)
  base.class_eval do
    # Default prefix is the fallback prefix if no purpose is available.
    class_attribute :default_prefix
    delegate :ean13_barcode, :machine_barcode, :human_barcode, to: :primary_barcode, allow_nil: true
  end
end

Instance Method Details

#any_barcode_matching?(other_barcode) ⇒ Boolean

***** Read-only utility methods *****

Returns:

  • (Boolean)


20
21
22
# File 'app/models/barcode/barcodeable.rb', line 20

def any_barcode_matching?(other_barcode)
  barcodes.any? { |barcode| barcode =~ other_barcode }
end

#barcode_formatObject



28
29
30
# File 'app/models/barcode/barcodeable.rb', line 28

def barcode_format
  primary_barcode.format
end

#barcode_numberObject



24
25
26
# File 'app/models/barcode/barcodeable.rb', line 24

def barcode_number
  primary_barcode&.number&.to_s
end

#cgap_barcodeObject



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

def cgap_barcode
  barcodes.detect(&:cgap?)&.machine_barcode
end

#cgap_barcode=(barcode) ⇒ Object



77
78
79
# File 'app/models/barcode/barcodeable.rb', line 77

def cgap_barcode=(barcode)
  barcodes.cgap.first_or_initialize.barcode = barcode
end

#external_barcodeObject



81
82
83
# File 'app/models/barcode/barcodeable.rb', line 81

def external_barcode
  barcodes.detect(&:external?)&.machine_barcode
end

#external_barcode=(barcode) ⇒ Object



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

def external_barcode=(barcode)
  barcodes.external.first_or_initialize.barcode = barcode
end

#external_identifierObject



36
37
38
# File 'app/models/barcode/barcodeable.rb', line 36

def external_identifier
  human_barcode
end

#fluidigm_barcodeObject



65
66
67
# File 'app/models/barcode/barcodeable.rb', line 65

def fluidigm_barcode
  barcodes.detect(&:fluidigm?)&.machine_barcode
end

#fluidigm_barcode=(barcode) ⇒ Object



69
70
71
# File 'app/models/barcode/barcodeable.rb', line 69

def fluidigm_barcode=(barcode)
  barcodes.fluidigm.first_or_initialize.barcode = barcode
end

#foreign_barcode=(barcode) ⇒ Object

Detects the format of the foreign barcode string passed in Adds it to the list of barcodes, making it the primary (most recent) barcode Throws exceptions if there are validation issues



92
93
94
95
96
97
98
99
100
# File 'app/models/barcode/barcodeable.rb', line 92

def foreign_barcode=(barcode)
  barcode_format = Barcode.matching_barcode_format(barcode)

  raise "Cannot determine format for foreign barcode #{barcode}" if barcode_format.blank?

  raise "Foreign Barcode: #{barcode} is already in use!" if Barcode.exists_for_format?(barcode_format, barcode)

  barcodes << Barcode.new(format: barcode_format, barcode: barcode)
end

#generate_barcodeObject

Assumes presence of a method called sanger_barcode= on the class this is included within. It’s not implemented on TubeRack!



15
16
17
# File 'app/models/barcode/barcodeable.rb', line 15

def generate_barcode
  self.sanger_barcode = { prefix: default_prefix, number: AssetBarcode.new_barcode } unless primary_barcode
end

#infinium_barcodeObject

***** Getter and setter methods for foreign barcodes *****



57
58
59
# File 'app/models/barcode/barcodeable.rb', line 57

def infinium_barcode
  barcodes.detect(&:infinium?)&.machine_barcode
end

#infinium_barcode=(barcode) ⇒ Object



61
62
63
# File 'app/models/barcode/barcodeable.rb', line 61

def infinium_barcode=(barcode)
  barcodes.infinium.first_or_initialize.barcode = barcode
end

#prefixObject



32
33
34
# File 'app/models/barcode/barcodeable.rb', line 32

def prefix
  primary_barcode&.barcode_prefix
end

#primary_barcodeObject



48
49
50
51
52
# File 'app/models/barcode/barcodeable.rb', line 48

def primary_barcode
  # If we've already loaded the barcodes, then their order is indeterminate
  # rather than re-fetching them, we sort in Ruby.
  barcodes.loaded? ? barcodes.max_by(&:id) : barcodes.last
end

#printable_targetObject



40
41
42
# File 'app/models/barcode/barcodeable.rb', line 40

def printable_target
  self
end

#sanger_barcodeObject



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

def sanger_barcode
  barcodes.detect(&:sanger_barcode?)
end