Class: SBCF::SangerBarcode

Inherits:
Object
  • Object
show all
Extended by:
Builders
Defined in:
lib/sanger_barcode_format/sanger_barcode.rb

Overview

Barcodes generated by Sequencescape and other applications have two representations, a human readable form, and an ean13 representation. The ean13 is regularly used to generate physical barcodes, and is returned by the various barcode scanners. Meanwhile the human readable form is often used when items are searched for manually, particularly when there is no physical access to the item itself.

Barcodes are comprised of three components:

- A prefix: represented by two uppercase letters in the human readable form,
and encoded as three digits in the ean13. Please be aware that some encoded
prefixes begin with zero, which can get stripped under some circumstances.
Prefixes generally identify the type of item which has received the barcode
and are registered in an external database.
- A number: A number between 0 and 9999999 inclusive. The human form is not
typically zero padded.
- A checksum: In addition to the ean13 of the machine printed version,
barcodes also contain a single character / two digit internal checksum used to
identify input errors of the human readable form.

In addition, printed forms will contain the standard EAN checksum digit. This checksum is included in the output of this gem and does not need to be recalculated .

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Builders

from_human, from_machine, from_prefix_and_number

Constructor Details

#initialize(prefix: nil, number: nil, checksum: nil, machine_barcode: nil, human_barcode: nil, checksum_required: false) ⇒ SangerBarcodable::SangerBarcode

Create a new barcode object. Either:

  • Provide a prefix and number

  • Provide a machine barcode (Ean13)

  • Provide a human readable barcode (Eg. DN12345) If checksum required is set to true will raise ChecksumRequired if the internal checksum character is missing.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 43

def initialize(
  prefix: nil, number: nil, checksum: nil,
  machine_barcode: nil,
  human_barcode: nil, checksum_required: false
)
  argument_error! unless [(prefix && number), machine_barcode, human_barcode].one?
  self.prefix = prefix
  self.number = number
  self.checksum = checksum
  self.machine_barcode = machine_barcode.to_i if machine_barcode
  @checksum_required = checksum_required
  self.human_barcode = human_barcode if human_barcode
end

Instance Attribute Details

#checksumSBCF::Checksum

Returns the internally used checksum



88
89
90
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 88

def checksum
  @checksum
end

#numberObject

Returns the value of attribute number



27
28
29
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 27

def number
  @number
end

#prefixObject

Returns the value of attribute prefix



27
28
29
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 27

def prefix
  @prefix
end

Instance Method Details

#check_eanBool

Checks that the EAN digit of the provided machine barcode is correct In practice this should be enforced by the scanners, so should only be false is the data were input manually



109
110
111
112
113
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 109

def check_ean
  # the EAN checksum is calculated so that the EAN of the code with checksum added is 0
  # except the new column (the checksum) start with a different weight (so the previous column keep the same weight)
  @machine_barcode.nil? || calculate_ean(@machine_barcode, 1).zero?
end

#human_barcodeString

Returns the human readable barcode, or nil idif no barcode could be generated



74
75
76
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 74

def human_barcode
  @human_barcode ||= calculate_human_barcode
end

#human_barcode!String

As with human_barcode, but raises InvalidBarcode if no barcode could be generated



81
82
83
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 81

def human_barcode!
  human_barcode || raise(InvalidBarcode, "Could not convert #{@machine_barcode} to human_barcode")
end

#machine_barcodeInt

Returns the machine readable ean13, or nil if no barcode could be generated



60
61
62
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 60

def machine_barcode
  @machine_barcode ||= calculate_machine_barcode
end

#machine_barcode!Int

As with machine_barcode but raises InvalidBarcode if no barcode could be generated



67
68
69
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 67

def machine_barcode!
  machine_barcode || raise(InvalidBarcode, "Could not convert #{@human_barcode} to machine_barcode")
end

#valid?Bool

Checks is the data provided generate a valid barcode

  • The number is in the correct range

  • Any provided checksum is correct

  • Any provided EAN is correct



97
98
99
100
101
102
# File 'lib/sanger_barcode_format/sanger_barcode.rb', line 97

def valid?
  number &&
    calculate_checksum == checksum &&
    check_ean &&
    prefix
end