-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #733 from pulibrary/i678-keywords
Add keyword configuration option to OCLC newly added
- Loading branch information
Showing
5 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
module Oclc | ||
module LcCallSlips | ||
# This class is responsible for searching a given | ||
# Marc::DataField for the keywords that a selector | ||
# is interested in. | ||
class KeywordField | ||
def initialize(field:, keywords:) | ||
@field = field | ||
@keywords = keywords | ||
end | ||
|
||
def match? | ||
keyword_field? && field_contains_keywords? | ||
end | ||
|
||
private | ||
|
||
attr_reader :field, :keywords | ||
|
||
def keyword_field? | ||
field.is_a?(MARC::DataField) && field.tag.match?(/^[12578]/) | ||
end | ||
|
||
def field_contains_keywords? | ||
field.any? { |subfield| subfield_contains_keywords?(subfield) } | ||
end | ||
|
||
def subfield_contains_keywords?(subfield) | ||
words_in_subfield = subfield.value.split(' ') | ||
words_in_subfield.any? { |found_word| word_is_keyword? found_word } | ||
end | ||
|
||
def word_is_keyword?(word) | ||
keywords.any? do |desired_keyword| | ||
# Add ^ and $ to make sure that we match the whole world, | ||
# then turn the * wildcard into .* | ||
desired_keyword_as_regexp = Regexp.new('^' + desired_keyword.gsub('*', '.*') + '$', 'i') | ||
word.match? desired_keyword_as_regexp | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
RSpec.describe Oclc::LcCallSlips::KeywordField do | ||
shared_examples 'a match' do | ||
it 'match? returns true' do | ||
expect(described_class.new(field:, keywords:).match?).to eq(true) | ||
end | ||
end | ||
shared_examples 'not a match' do | ||
it 'match? returns false' do | ||
expect(described_class.new(field:, keywords:).match?).to eq(false) | ||
end | ||
end | ||
context 'when field is a control field' do | ||
let(:field) { MARC::ControlField.new('001', 'SCSB-10482146') } | ||
let(:keywords) { ['cantaloup*'] } | ||
it_behaves_like 'not a match' | ||
end | ||
|
||
context 'when field is a 245' do | ||
let(:field) do | ||
MARC::DataField.new('245', '0', '0', | ||
MARC::Subfield.new('a', 'Cantaloups')) | ||
end | ||
let(:keywords) { ['cantaloup*'] } | ||
it_behaves_like 'a match' | ||
end | ||
|
||
context 'when field is a 246' do | ||
let(:field) do | ||
MARC::DataField.new('246', '0', '0', | ||
MARC::Subfield.new('a', 'Cantaloupe culture')) | ||
end | ||
let(:keywords) { ['cantaloup*'] } | ||
it_behaves_like 'a match' | ||
end | ||
|
||
context 'when field is a 260' do | ||
let(:field) do | ||
MARC::DataField.new('260', '0', '0', | ||
MARC::Subfield.new('b', 'The Rocky Ford Cantaloupe Seed Breeders\' Association')) | ||
end | ||
let(:keywords) { ['cantaloup*'] } | ||
it_behaves_like 'a match' | ||
context 'when keyword includes multiple wildcards' do | ||
let(:keywords) { ['*nt*loup*'] } | ||
it_behaves_like 'a match' | ||
end | ||
context 'when the first matching keyword comes late in the array' do | ||
let(:keywords) { ['sediment', 'metamorphosis', 'geolog*', 'igneous', 'rock*', 'sandstone'] } | ||
it_behaves_like 'a match' | ||
end | ||
end | ||
|
||
context 'when field is a 300' do | ||
let(:field) do | ||
MARC::DataField.new('300', '0', '0', | ||
MARC::Subfield.new('a', '1 online resource (2 pages), digital, PDF file')) | ||
end | ||
let(:keywords) { ['online'] } | ||
it_behaves_like 'not a match' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters