Skip to content

Commit

Permalink
Index holdings data to mhld_struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed May 16, 2023
1 parent 74bdb4a commit 03312f4
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 0 deletions.
69 changes: 69 additions & 0 deletions lib/traject/config/sirsi_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,75 @@ def library_has(field)
[field['a'], field['z']].compact.join(' ')
end

to_field 'mhld_struct' do |record, accumulator, _context|
location_groups = Traject::MarcExtractor.new('852:853:863:866:867:868').collect_matching_lines(record) { |field, _spec, _extractor| field }.slice_before { |field| field.tag == '852' }

result = location_groups.each_with_object({}) do |fields, hash|
next unless fields.first.tag == '852'

fields_by_tag = fields.group_by(&:tag)

marc852 = fields.first

library = marc852.subfields.select { |sf| sf.code == 'b' }.map(&:value).compact.join(' ')
library = nil if library.empty?
location = marc852.subfields.select { |sf| sf.code == 'c' }.map(&:value).compact.join(' ')
location = nil if location.empty?

comments = marc852.subfields.select { |sf| sf.code == '3' || sf.code == 'z' }.map(&:value).compact
next if comments.any? { |c| c =~ /all holdings transferred/i }

# Reset to process new 852
mhld_field = MhldField.new

next if skipped_locations[location] || missing_locations[location]

mhld_result = {}

comment = comments.reject(&:empty?).join(' ')

marc853s = fields_by_tag.fetch('853', []).each_with_object({}) do |field, h|
link_seq_num = field.select { |sf| sf.code == '8' }.map(&:value).first.to_i
h[link_seq_num] = field
end

most_recent_marc863 = fields_by_tag.fetch('863', []).select { |field| field['8'].present? }.max_by do |field|
link_num, seq_num = field['8'].split('.', 2).map(&:to_i)
[link_num, seq_num || -1]
end

mhld_result[:holdings] = (fields_by_tag['866'] || []).map do |field|
{ statement: field['a'], note: field['z'] }.reject { |_k, v| v.blank? }
end

mhld_result[:supplements] = (fields_by_tag['867'] || []).map do |field|
{ statement: field['a'], note: field['z'] }.reject { |_k, v| v.blank? }
end

mhld_result[:index] = (fields_by_tag['868'] || []).map do |field|
{ statement: field['a'], note: field['z'] }.reject { |_k, v| v.blank? }
end

mhld_result[:note] = comment if comment.present?

if most_recent_marc863 && (marc852['='] || mhld_result[:holdings].none? || mhld_result[:holdings].any? { |statement| statement[:statement].end_with? '-' } || mhld_result[:supplements] || mhld_result[:index])
most_recent_marc863_link_num = most_recent_marc863['8']&.split('.')&.first.to_i

if most_recent_marc863_link_num != 0
pattern = marc853s[most_recent_marc863_link_num]
mhld_result[:latest_received] = mhld_field.get863display_value(pattern, most_recent_marc863)
end
end

next if mhld_result.all? { |k, v| v.blank? }

hash[library] ||= {}
hash[library][location] ||= mhld_result
end

accumulator << result unless result.blank?
end

to_field 'bookplates_display' do |record, accumulator|
Traject::MarcExtractor.new('979').collect_matching_lines(record) do |field, _spec, _extractor|
file = field['c']
Expand Down
164 changes: 164 additions & 0 deletions spec/lib/traject/config/holdings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
end
describe 'mhld_display' do
let(:field) { 'mhld_display' }
let(:struct_field) { 'mhld_struct' }
describe 'real data' do
let(:fixture_name) { '2499.marc' }

Expand All @@ -44,13 +45,35 @@
'MUSIC -|- STACKS -|- -|- v.1 -|- ',
'MUSIC -|- STACKS -|- -|- v.2 -|- '
]

expect(JSON.parse(select_by_id('2499')[struct_field].first)).to eq({
'MUSIC' => {
'STACKS' => {
'holdings' => [
{ 'statement' => 'v.1' },
{ 'statement' => 'v.2' }
],
'index' => [],
'supplements' => []
}
}
})
end
context do
let(:fixture_name) { '9012.marc' }
it do
expect(select_by_id('9012')[field]).to eq [
'SAL3 -|- STACKS -|- -|- 1948,1965-1967,1974-1975 -|- '
]
expect(JSON.parse(select_by_id('9012')[struct_field].first)).to eq({
'SAL3' => {
'STACKS' => {
'holdings' => [{ 'statement' => '1948,1965-1967,1974-1975' }],
'index' => [],
'supplements' => []
}
}
})
end
end
context do
Expand All @@ -59,6 +82,15 @@
expect(select_by_id('1572')[field]).to eq [
'SAL3 -|- STACKS -|- -|- Heft 1-2 <v.568-569 in series> -|- '
]
expect(JSON.parse(select_by_id('1572')[struct_field].first)).to eq({
'SAL3' => {
'STACKS' => {
'holdings' => [{ 'statement' => 'Heft 1-2 <v.568-569 in series>' }],
'index' => [],
'supplements' => []
}
}
})
end
end
context do
Expand Down Expand Up @@ -100,6 +132,19 @@
end
it do
expect(result[field]).to eq ['MATH-CS -|- SHELBYTITL -|- -|- v.25(1984)- -|- v.54:no.1 (2013:1_TRIMESTRE)']

expect(JSON.parse(result[struct_field].first)).to eq({
'MATH-CS' => {
'SHELBYTITL' => {
'holdings' => [
{ 'statement' => 'v.25(1984)-' }
],
'index' => [],
'supplements' => [],
'latest_received' => 'v.54:no.1 (2013:1_TRIMESTRE)'
}
}
})
end
end
describe 'latest received' do
Expand Down Expand Up @@ -133,6 +178,17 @@
end
it do
expect(result[field]).to eq ['lib -|- loc -|- -|- -|- v.106:pt.3:no.482 (2010:WIN)']

expect(JSON.parse(result[struct_field].first)).to eq({
'lib' => {
'loc' => {
'holdings' => [],
'index' => [],
'supplements' => [],
'latest_received' => 'v.106:pt.3:no.482 (2010:WIN)'
}
}
})
end
context do
let(:record) do
Expand All @@ -155,6 +211,16 @@
end
it do
expect(result[field]).to eq ['lib -|- loc -|- -|- -|- v.105 (2009)']
expect(JSON.parse(result[struct_field].first)).to eq({
'lib' => {
'loc' => {
'holdings' => [],
'index' => [],
'supplements' => [],
'latest_received' => 'v.105 (2009)'
}
}
})
end
end
context do
Expand Down Expand Up @@ -186,6 +252,17 @@
end
it do
expect(result[field]).to eq ['lib -|- loc -|- -|- -|- v.205:no.10 (2011:March 9)']

expect(JSON.parse(result[struct_field].first)).to eq({
'lib' => {
'loc' => {
'holdings' => [],
'index' => [],
'supplements' => [],
'latest_received' => 'v.205:no.10 (2011:March 9)'
}
}
})
end
end
end
Expand Down Expand Up @@ -217,6 +294,20 @@
expect(result[field][0]).to eq 'lib -|- loc -|- -|- pt.1-4 <v.3,16,27-28 in series> -|- '
expect(result[field][1]).to eq 'lib -|- loc -|- -|- pt.2 <v.16 in series> -|- '
expect(result[field][2]).to eq 'lib -|- loc -|- -|- pt.5 -|- '

expect(JSON.parse(result[struct_field].first)).to eq({
'lib' => {
'loc' => {
'holdings' => [
{ 'statement' => 'pt.1-4', 'note' => '<v.3,16,27-28 in series>' },
{ 'statement' => 'pt.2', 'note' => '<v.16 in series>' },
{ 'statement' => 'pt.5' }
],
'index' => [],
'supplements' => []
}
}
})
end
end
context '867subz' do
Expand Down Expand Up @@ -244,6 +335,21 @@
expect(result[field][0]).to eq 'lib -|- loc -|- -|- v.188 -|- '
expect(result[field][1]).to eq 'lib -|- loc -|- -|- Supplement: first subz -|- '
expect(result[field][2]).to eq 'lib -|- loc -|- -|- Supplement: second -|- '

expect(JSON.parse(result[struct_field].first)).to eq({
'lib' => {
'loc' => {
'holdings' => [
{ 'statement' => 'v.188' }
],
'index' => [],
'supplements' => [
{ 'statement' => 'first', 'note' => 'subz' },
{ 'statement' => 'second' }
]
}
}
})
end
end
context '868subz' do
Expand Down Expand Up @@ -271,6 +377,21 @@
expect(result[field][0]).to eq 'lib -|- loc -|- -|- v.188 -|- '
expect(result[field][1]).to eq 'lib -|- loc -|- -|- Index: first subz -|- '
expect(result[field][2]).to eq 'lib -|- loc -|- -|- Index: second -|- '

expect(JSON.parse(result[struct_field].first)).to eq({
'lib' => {
'loc' => {
'holdings' => [
{ 'statement' => 'v.188' }
],
'index' => [
{ 'statement' => 'first', 'note' => 'subz' },
{ 'statement' => 'second' }
],
'supplements' => []
}
}
})
end
end
end
Expand Down Expand Up @@ -321,6 +442,19 @@
it do
expect(result[field].length).to eq 1
expect(result[field]).to eq ['lib -|- loc -|- -|- 2003,2006- -|- 2011:pt.2']

expect(JSON.parse(result[struct_field].first)).to eq({
'lib' => {
'loc' => {
'holdings' => [
{ 'statement' => '2003,2006-' }
],
'index' => [],
'supplements' => [],
'latest_received' => '2011:pt.2'
}
}
})
end
end
describe 'malformed 863$8' do
Expand Down Expand Up @@ -363,6 +497,7 @@
let(:fixture_name) { 'mhldDisplay852only.mrc' }
it do
expect(select_by_id('3974376')[field]).to be_nil
expect(select_by_id('3974376')[struct_field]).to be_nil
end
end
describe 'skip skipped locations' do
Expand All @@ -371,6 +506,7 @@
let(:fixture_name) { 'mhldDisplay.mrc' }
it do
expect(select_by_id('SkippedLocs')[field]).to be_blank
expect(select_by_id('SkippedLocs')[struct_field]).to be_blank
end
end
describe 'number of separators' do
Expand All @@ -387,12 +523,40 @@
it do
expect(select_by_id('keep868ind0')[field]).to include 'GREEN -|- CURRENTPER -|- keep 868 -|- v.194(2006)- -|- '
expect(select_by_id('keep868ind0')[field]).to include 'GREEN -|- CURRENTPER -|- -|- Index: keep me (868) -|- '

expect(JSON.parse(select_by_id('keep868ind0')[struct_field].first)).to eq({
'GREEN' => {
'CURRENTPER' => {
'holdings' => [
{ 'statement' => 'v.194(2006)-' }
],
'index' => [
{ 'statement' => 'keep me (868)' }
],
'supplements' => [],
'note' => 'keep 868'
}
}
})
end
end
describe '852 w/ 867' do
let(:fixture_name) { 'mhldDisplay867.mrc' }
it do
expect(select_by_id('keep867ind0')[field]).to include 'GREEN -|- CURRENTPER -|- keep 867 -|- Supplement: keep me (867) -|- '

expect(JSON.parse(select_by_id('keep867ind0')[struct_field].first)).to eq({
'GREEN' => {
'CURRENTPER' => {
'holdings' => [],
'index' => [],
'supplements' => [
{ 'statement' => 'keep me (867)' }
],
'note' => 'keep 867'
}
}
})
end
end
end
Expand Down

0 comments on commit 03312f4

Please sign in to comment.