Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Commit 3307c7b

Browse files
committed
Before printing barcodes, convert to the right format depending on how
it was originally stored.
1 parent fbd9344 commit 3307c7b

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

app/models/asset.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ def study_name
174174
return ''
175175
end
176176

177-
def machine_barcode?
177+
def print_machine_barcode?
178178
facts.where(predicate: 'barcodeFormat', object: 'machine_barcode').count > 0
179179
end
180180

181181
def barcode_formatted_for_printing
182-
if machine_barcode?
183-
mbarcode = SBCF::SangerBarcode.from_human(barcode).machine_barcode
182+
if print_machine_barcode?
183+
mbarcode = TokenUtil.machine_barcode(barcode)
184184
return mbarcode if mbarcode
185185
end
186186
barcode
@@ -202,7 +202,7 @@ def printable_object(username = 'unknown')
202202
return {:label => {
203203
:barcode => barcode_formatted_for_printing,
204204
:barcode2d => barcode_formatted_for_printing,
205-
:top_line => barcode,
205+
:top_line => TokenUtil.human_barcode(barcode),
206206
:middle_line => kit_type,
207207
:bottom_line => info_line
208208
}

lib/token_util.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ def self.fluidx_barcode_prefix
77
'F'
88
end
99

10+
def self.MACHINE_BARCODE
11+
/^\d*$/
12+
end
13+
14+
def self.machine_barcode?(barcode)
15+
return false if barcode.nil?
16+
barcode.to_s.match?(TokenUtil.MACHINE_BARCODE)
17+
end
18+
19+
def self.human_barcode?(barcode)
20+
return false if barcode.nil?
21+
!barcode.to_s.match?(TokenUtil.MACHINE_BARCODE)
22+
end
23+
24+
def self.machine_barcode(barcode)
25+
return barcode.to_s if machine_barcode?(barcode)
26+
SBCF::SangerBarcode.from_human(barcode).machine_barcode.to_s
27+
end
28+
29+
def self.human_barcode(barcode)
30+
return SBCF::SangerBarcode.from_machine(barcode).human_barcode if machine_barcode?(barcode)
31+
barcode
32+
end
33+
1034
def self.WILDCARD_REGEXP
1135
/\?\w*/
1236
end

spec/lib/token_util_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,52 @@
9797
end
9898
end
9999
end
100+
101+
context '#machine_barcode' do
102+
it 'returns the barcode to_s if the barcode is already machine barcode' do
103+
expect(TokenUtil.machine_barcode(3981337734769)).to eq("3981337734769")
104+
end
105+
it 'returns the barcode if the barcode is already machine barcode' do
106+
expect(TokenUtil.machine_barcode("3981337734769")).to eq("3981337734769")
107+
end
108+
it 'returns the converted barcode if is not a machine barcode' do
109+
expect(TokenUtil.machine_barcode("NT1337734L")).to eq("3981337734769")
110+
end
111+
end
112+
113+
context '#human_barcode' do
114+
it 'returns the human barcode if the barcode is already human barcode' do
115+
expect(TokenUtil.human_barcode("D12345678")).to eq("D12345678")
116+
end
117+
it 'returns the converted barcode from numeric machine barcode' do
118+
expect(TokenUtil.human_barcode(3981337734769)).to eq("NT1337734L")
119+
end
120+
it 'returns the converted barcode from string machine barcode' do
121+
expect(TokenUtil.human_barcode("3981337734769")).to eq("NT1337734L")
122+
end
123+
end
124+
125+
context '#machine_barcode?' do
126+
it 'returns true if the barcode is machine barcode numeric' do
127+
expect(TokenUtil.machine_barcode?(12345678)).to eq(true)
128+
end
129+
it 'returns true if the barcode is machine barcode string' do
130+
expect(TokenUtil.machine_barcode?("12345678")).to eq(true)
131+
end
132+
it 'returns false any other case' do
133+
expect(TokenUtil.machine_barcode?("D12345678")).to eq(false)
134+
end
135+
end
136+
137+
context '#human_barcode?' do
138+
it 'returns true if the barcode is human barcode' do
139+
expect(TokenUtil.human_barcode?("D12345678")).to eq(true)
140+
end
141+
it 'returns false if machine barcode numeric' do
142+
expect(TokenUtil.human_barcode?(12345678)).to eq(false)
143+
end
144+
it 'returns false if machine barcode string' do
145+
expect(TokenUtil.human_barcode?("12345678")).to eq(false)
146+
end
147+
end
100148
end

spec/models/asset_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
end
2727
end
2828

29-
context '#machine_barcode?' do
29+
context '#print_machine_barcode?' do
3030
let(:asset) { create(:asset)}
3131
it 'returns true when the asset has a barcode format of machine barcode' do
3232
asset.facts << create(:fact, predicate: 'barcodeFormat', object: 'machine_barcode',
3333
literal: true)
34-
expect(asset.machine_barcode?).to be_truthy
34+
expect(asset.print_machine_barcode?).to be_truthy
3535
end
3636
it 'returns false when the asset does not have a setting' do
37-
expect(asset.machine_barcode?).to be_falsy
37+
expect(asset.print_machine_barcode?).to be_falsy
3838
end
3939
end
4040

@@ -55,7 +55,7 @@
5555
end
5656
context 'when we can generate the machine barcode' do
5757
it 'returns the machine barcode' do
58-
expect(asset.barcode_formatted_for_printing).to eq(machine_barcode)
58+
expect(asset.barcode_formatted_for_printing).to eq(machine_barcode.to_s)
5959
end
6060
end
6161
end
@@ -95,10 +95,10 @@
9595
expect(asset.printable_object[:label][:top_line]).to eq(human_barcode)
9696
end
9797
it 'prints the machine barcode as barcode' do
98-
expect(asset.printable_object[:label][:barcode]).to eq(machine_barcode)
98+
expect(asset.printable_object[:label][:barcode]).to eq(machine_barcode.to_s)
9999
end
100100
it 'prints the machine barcode as barcode 2d' do
101-
expect(asset.printable_object[:label][:barcode2d]).to eq(machine_barcode)
101+
expect(asset.printable_object[:label][:barcode2d]).to eq(machine_barcode.to_s)
102102
end
103103
end
104104
end

0 commit comments

Comments
 (0)