diff --git a/lib/marc_cleanup/variable_fields.rb b/lib/marc_cleanup/variable_fields.rb index e43af9c..8782c03 100644 --- a/lib/marc_cleanup/variable_fields.rb +++ b/lib/marc_cleanup/variable_fields.rb @@ -127,78 +127,35 @@ def f041_errors?(record) f041.each do |field| field.subfields.each do |subfield| val = subfield.value - return true if (val.size > 3) && (val.size % 3 == 0) + return true if (val.size > 3) && (val.size % 3).zero? end end false end # http://www.loc.gov/standards/valuelist/marcauthen.html - def auth_codes_042 + def auth_codes_f042 %w[ - anuc - croatica - dc - dhca - dlr - gamma - gils - gnd1 - gnd2 - gnd3 - gnd4 - gnd5 - gnd6 - gnd7 - gndz - isds/c - issnuk - lacderived - lc - lcac - lccopycat - lccopycat-nm - lcd - lcderive - lchlas - lcllh - lcnccp - lcnitrate - lcnuc - lcode - msc - natgaz - nbr - nlc - nlmcopyc - norbibl - nsdp - nst - ntccf - nznb - pcc - premarc - reveal - sanb - scipio - toknb - ukblcatcopy - ukblderived - ukblproject - ukblsr - ukscp - xisds/c - xissnuk - xlc - xnlc - xnsdp + anuc croatica dc dhca dlr + gamma gils gnd1 gnd2 gnd3 gnd4 gnd5 gnd6 gnd7 gndz isds/c issnuk + lacderived lc lcac lccopycat lccopycat-nm lcd lcderive + lchlas lcllh lcnccp lcnitrate lcnuc lcode + msc natgaz nbr nlc nlmcopyc norbibl nsdp nst ntccf nznb + pcc premarc reveal sanb scipio toknb + ukblcatcopy ukblderived ukblproject ukblsr ukscp + xisds/c xissnuk xlc xnlc xnsdp ] end def auth_code_error?(record) return false unless record['042'] + return true if record.fields('042').size > 1 - auth_codes_042.include?(record['042']['a']) ? false : true + record['042'].subfields.each do |subfield| + next if subfield.code != 'a' + return true unless auth_codes_f042.include?(subfield.value) + end + false end def invalid_indicators?(record) @@ -265,8 +222,7 @@ def extra_spaces?(record) end def multiple_no_040?(record) - f040 = record.fields('040') - f040.size != 1 + record.fields('040').size != 1 end def multiple_no_040b?(record) @@ -277,18 +233,19 @@ def multiple_no_040b?(record) b040 = f040.subfields.select { |subfield| subfield.code == 'b' } return true if b040.size != 1 - b040 = b040.first.value - b040.gsub!(/[ ]/, '') - b040 == '' + b040.first.value.match?(/^\s*$/) end - def f046_subfield_errors?(record) + def f046_errors?(record) + subf_codes = %w[b c d e] + subf_a_values = %w[r s p t x q n i k r m t x n] f046 = record.fields('046') return false if f046.empty? f046.each do |field| - subf_codes = field.subfields.map { |subfield| subfield.code } - return true if field['a'].nil? && (subf_codes & %w[b c d e]).size > 0 + codes = field.subfields.map(&:code) + return true if field['a'] && !subf_a_values.include?(field['a']) + return true if field['a'].nil? && (subf_codes & codes).size.positive? end false end @@ -559,15 +516,14 @@ def uri_escape(record) ### Make the 040 $b 'eng' if it doesn't have a value def fix_040b(record) - f040 = record.fields('040') - return record unless f040.size == 1 + return record unless record.fields('040').size == 1 - f040 = f040.first + f040 = record['040'] field_index = record.fields.index(f040) b040 = f040.subfields.select { |subfield| subfield.code == 'b' } return record unless b040.empty? - subf_codes = f040.subfields.map { |subfield| subfield.code } + subf_codes = f040.subfields.map(&:code) subf_index = if f040['a'] (subf_codes.index { |i| i == 'a' }) + 1 else @@ -579,7 +535,7 @@ def fix_040b(record) end ### Split up subfields that contain multiple 3-letter language codes - def fix_041(record) + def fix_f041(record) f041 = record.fields('041') return record if f041.empty? @@ -589,11 +545,13 @@ def fix_041(record) field.subfields.each do |subfield| code = subfield.code val = subfield.value - next unless val.size % 3 == 0 - - langs = val.scan(/.../) - langs.each do |lang| - new_field.append(MARC::Subfield.new(code, lang)) + if (val.size % 3).zero? + langs = val.scan(/.../) + langs.each do |lang| + new_field.append(MARC::Subfield.new(code, lang)) + end + else + new_field.append(MARC::Subfield.new(code, val)) end end record.fields[f_index] = new_field diff --git a/spec/variable_fields/040_spec.rb b/spec/variable_fields/040_spec.rb deleted file mode 100644 index 3b17e1b..0000000 --- a/spec/variable_fields/040_spec.rb +++ /dev/null @@ -1,105 +0,0 @@ -require 'nokogiri' -require 'marc' -require 'byebug' -require 'marc_cleanup' - -RSpec.describe 'field_040' do - let(:record) { MARC::Record.new_from_hash('fields' => fields, 'leader' => leader) } - let(:leader) { '01104naa a2200289 i 4500' } - - describe 'multiple_no_040?' do - let(:fields) do - [ - { '001' => '9970534203506421' }, - { '040' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'a' => 'DLC' }] } }, - { '040' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'a' => 'DLC' }] } } - ] - end - it 'checks if a record has multiple or no 040 fields' do - expect(MarcCleanup.multiple_no_040?(record)).to eq true - end - end - - describe 'multiple_no_040b?' do - let(:fields) do - [ - { '001' => '9970534203506421' }, - { '040' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'b' => 'e n g' }]} } - ] - end - it 'checks if a record has multiple or no 040 fields' do - expect(MarcCleanup.multiple_no_040b?(record)).to eq false - end - end - - describe 'fix_040b' do - - context "when there is no subfield a" do - let(:fields) do - [ - { '001' => '9970534203506421' }, - { '040' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'c' => 'DLC' }]} } - ] - end - it 'corrects the 040b' do - expect(MarcCleanup.fix_040b(record)['040']['b']).to eq 'eng' - end - end - - context "when there is one subfield a" do - let(:fields) do - [ - { '001' => '9970534203506421' }, - { '040' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'a' => 'DLC' }]} } - ] - end - it 'corrects the 040b' do - expect(MarcCleanup.fix_040b(record)['040']['b']).to eq 'eng' - end - end - end - - describe 'missing_040c?' do - - context 'when no there is no subfield c' do - let(:fields) do - [ - { '001' => '9970534203506421' }, - { '040' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'a' => 'DLC' }, - { 'b' => 'eng' }]} } - ] - end - it 'checks for subfield c in field 040' do - expect(MarcCleanup.missing_040c?(record)).to eq true - end - end - - context 'when no there is no subfield c' do - let(:fields) do - [ - { '001' => '9970534203506421' }, - { '040' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'a' => 'DLC' }, - { 'b' => 'eng' }, - { 'c' => 'DLC' }]} } - ] - end - it 'checks for subfield c in field 040' do - expect(MarcCleanup.missing_040c?(record)).to eq false - end - end - end -end diff --git a/spec/variable_fields/04x_spec.rb b/spec/variable_fields/04x_spec.rb new file mode 100644 index 0000000..81d52e9 --- /dev/null +++ b/spec/variable_fields/04x_spec.rb @@ -0,0 +1,309 @@ +# frozen_string_literal: true + +require 'marc_cleanup' + +RSpec.describe 'field 040 methods' do + let(:record) { MARC::Record.new_from_hash('fields' => fields, 'leader' => leader) } + let(:leader) { '01104naa a2200289 i 4500' } + + describe 'multiple_no_040?' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'DLC' }] } }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'DLC' }] } } + ] + end + it 'checks if a record has multiple or no 040 fields' do + expect(MarcCleanup.multiple_no_040?(record)).to eq true + end + end + + describe 'multiple_no_040b?' do + context 'one 040b' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'b' => 'eng' }] } } + ] + end + it 'does not trigger an error' do + expect(MarcCleanup.multiple_no_040b?(record)).to eq false + end + end + + context 'one 040b with spaces only' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'b' => ' ' }] } } + ] + end + it 'triggers an error' do + expect(MarcCleanup.multiple_no_040b?(record)).to eq true + end + end + + context 'multiple 040b in one field' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'b' => 'eng' }, { 'b' => 'spa' }] } } + ] + end + it 'triggers an error' do + expect(MarcCleanup.multiple_no_040b?(record)).to eq true + end + end + end + + describe 'fix_040b' do + context 'when there is no subfield a' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'c' => 'DLC' }] } } + ] + end + it 'corrects the 040b' do + expect(MarcCleanup.fix_040b(record)['040']['b']).to eq 'eng' + end + end + + context 'when there is one subfield a' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'DLC' }] } } + ] + end + it 'corrects the 040b' do + expect(MarcCleanup.fix_040b(record)['040']['b']).to eq 'eng' + end + end + end + + describe 'missing_040c?' do + context 'when no there is no subfield c' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'DLC' }, + { 'b' => 'eng' }] } } + ] + end + it 'checks for subfield c in field 040' do + expect(MarcCleanup.missing_040c?(record)).to eq true + end + end + + context 'when no there is no subfield c' do + let(:fields) do + [ + { '001' => '9970534203506421' }, + { '040' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'DLC' }, + { 'b' => 'eng' }, + { 'c' => 'DLC' }] } } + ] + end + it 'checks for subfield c in field 040' do + expect(MarcCleanup.missing_040c?(record)).to eq false + end + end + end +end + +RSpec.describe 'field 041 methods' do + let(:record) { MARC::Record.new_from_hash('fields' => fields, 'leader' => leader) } + let(:leader) { '01104naa a2200289 i 4500' } + + describe 'f041_errors?' do + let(:fields) do + [ + { '041' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'eng' }] } } + ] + end + it 'has no errors for a valid 041' do + expect(MarcCleanup.f041_errors?(record)).to eq false + end + end + + describe 'fix_f041' do + context 'has incomplete language code in subfield' do + let(:fields) do + [ + { '041' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'b' => 'engit' }] } } + ] + end + it 'does not modify the 041 field' do + modified_record = MarcCleanup.fix_f041(record) + expect(modified_record['041']['b']).to eq 'engit' + end + end + + context 'has multiple complete language codes in subfield' do + let(:fields) do + [ + { '041' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'b' => 'engitager' }] } } + ] + end + it 'splits the codes into separate subfields' do + modified_record = MarcCleanup.fix_f041(record) + f041subfields = modified_record['041'].subfields + mapped_subfields = f041subfields.map do |subfield| + { code: subfield.code, value: subfield.value } + end + expect(mapped_subfields).to eq [ + { code: 'b', value: 'eng' }, + { code: 'b', value: 'ita' }, + { code: 'b', value: 'ger' } + ] + end + end + end +end + +RSpec.describe 'field 042 methods' do + let(:record) { MARC::Record.new_from_hash('fields' => fields, 'leader' => leader) } + let(:leader) { '01104naa a2200289 i 4500' } + + describe 'auth_code_error?' do + context 'has no 042 field' do + let(:fields) do + [ + { '245' => { 'indicator1' => '0', + 'indicator2' => '0', + 'subfields' => [{ 'a' => 'This record has no 042' }] } } + ] + end + it 'does not trigger an error' do + expect(MarcCleanup.auth_code_error?(record)).to eq false + end + end + + context 'has multiple 042 fields' do + let(:fields) do + [ + { '042' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'sanb' }] } }, + { '042' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'pcc' }] } } + ] + end + it 'triggers an error' do + expect(MarcCleanup.auth_code_error?(record)).to eq true + end + end + + context 'has one valid auth_code and one invalid auth_code' do + let(:fields) do + [ + { '042' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'sanb' }, { 'a' => 'pcl' }] } } + ] + end + it 'triggers an error' do + expect(MarcCleanup.auth_code_error?(record)).to eq true + end + end + + context 'has one valid auth_code' do + let(:fields) do + [ + { '042' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'sanb' }] } } + ] + end + it 'does not trigger an error' do + expect(MarcCleanup.auth_code_error?(record)).to eq false + end + end + end +end + +RSpec.describe 'field 046 methods' do + let(:record) { MARC::Record.new_from_hash('fields' => fields, 'leader' => leader) } + let(:leader) { '01104naa a2200289 i 4500' } + + describe 'f046_errors?' do + context 'has no 046 field' do + let(:fields) do + [ + { '245' => { 'indicator1' => '0', + 'indicator2' => '0', + 'subfields' => [{ 'a' => 'This record has no 046' }] } } + ] + end + it 'does not trigger an error' do + expect(MarcCleanup.f046_errors?(record)).to eq false + end + end + + context 'subfield b with no subfield a' do + let(:fields) do + [ + { '046' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'b' => '1937' }] } } + ] + end + it 'triggers an error' do + expect(MarcCleanup.f046_errors?(record)).to eq true + end + end + + context 'subfield b with invalid subfield a value' do + let(:fields) do + [ + { '046' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'a' }, { 'b' => '1937' }] } } + ] + end + it 'triggers an error' do + expect(MarcCleanup.f046_errors?(record)).to eq true + end + end + + context 'subfield b with valid subfield a value' do + let(:fields) do + [ + { '046' => { 'indicator1' => ' ', + 'indicator2' => ' ', + 'subfields' => [{ 'a' => 'r' }, { 'b' => '1937' }] } } + ] + end + it 'does not trigger an error' do + expect(MarcCleanup.f046_errors?(record)).to eq false + end + end + end +end diff --git a/spec/variable_fields/0xx_spec.rb b/spec/variable_fields/0xx_spec.rb deleted file mode 100644 index 1a7f9be..0000000 --- a/spec/variable_fields/0xx_spec.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'nokogiri' -require 'marc' -require 'byebug' -require 'marc_cleanup' - -RSpec.describe 'test 0xx methods' do - describe 'f041_errors?' do - let(:fields) do - [ - { '041' => { 'indicator1' => ' ', - 'indicator2' => ' ', - 'subfields' => [{ 'a' => 'eng' }] } } - ] - end - let(:record) { MARC::Record.new_from_hash('fields' => fields) } - it 'has no errors for a valid 041' do - expect(MarcCleanup.f041_errors?(record)).to eq false - end - end -end