Skip to content

Commit

Permalink
Merge pull request #13 from UNC-Libraries/discard-subfields-lacking-c…
Browse files Browse the repository at this point in the history
…odes

subfield_arry discards subfields with no subfield code
  • Loading branch information
kspurgin authored Nov 28, 2018
2 parents c9866a9 + 4002162 commit a8eb7ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
9 changes: 5 additions & 4 deletions lib/sierra_postgres_utilities/helpers/varfields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ def extract_subfields(field_content, desired_subfields, trim_punct: false,
def subfield_arry(field_content, implicit_sfa: true)
field_content = add_explicit_sf_a(field_content) if implicit_sfa
arry = field_content.split('|')

# delete anything prior to the first subfield delimiter (which often
# but not always means deleting an empty string), then delete
# any/other empty strings
arry.shift
# if field_content is literally "|" (no subfield code), arry will be empty
# array; doing arry[1..-1].map... (without shifting) would throw an error
# when arry is empty.
arry.delete(''.freeze)
arry.map { |x| [x[0], x[1..-1]] }
end

end
end
end
2 changes: 1 addition & 1 deletion lib/sierra_postgres_utilities/sierradb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def self.write_xlsx(outfile, results, headers)
worksheet.Range("A#{i}:#{end_col}#{i}").value = result.values
end
# save and close excel
outfilepath = File.join(Dir.pwd, outfile).gsub(/\//, '\\\\')
outfilepath = outfile.gsub(/\//, '\\\\')
File.delete(outfilepath) if File.exist?(outfilepath)
workbook.saveas(outfilepath)
excel.quit
Expand Down
13 changes: 9 additions & 4 deletions spec/helpers/varfields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ class SpecDummy
end
end

it 'returns empty array if field_content is literal "|"' do
expect(dummy.subfield_arry('|')).to eq([])
end

context 'when subfield lacks subfield code' do
it 'discards that subfield' do
expect(dummy.subfield_arry('|adata||balso data')).
to eq([['a', 'data'], ['b', 'also data']])
end

it 'returns empty array if no subfields left' do
expect(dummy.subfield_arry('|')).to eq([])
end
end
end
end

0 comments on commit a8eb7ed

Please sign in to comment.