Skip to content

Commit

Permalink
refactor: extracts Subject from Subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Jul 12, 2024
1 parent 3d0b1eb commit 4054fcc
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 180 deletions.
25 changes: 3 additions & 22 deletions umich_catalog_indexing/lib/common/subjects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Subjects
end
end

require_relative "subjects/subject"
require_relative "subjects/lc_subject"
require_relative "subjects/non_lc_subject"
require_relative "subjects/normalize"
Expand Down Expand Up @@ -38,32 +39,12 @@ class Subjects

REMEDIATOR = Remediator.new

class << self
def subject_field?(field)
SUBJECT_FIELDS.include?(field.tag)
end

# Delegate LC determination to the class itself.
def lc_subject_field?(field)
LCSubject.lc_subject_field?(field)
end

# Pass off a new subject to the appropriate class
def for(field)
if lc_subject_field?(field)
LCSubject.from_field(field)
else
NonLCSubject.new(field)
end
end
end

def initialize(record)
@record = record
end

def subject_fields
sfields = @record.select { |field| self.class.subject_field?(field) }
sfields = @record.select { |field| Subject.subject_field?(field) }
sfields + sfields.flat_map { |field| _linked_fields_for(field) }.compact
end

Expand All @@ -72,7 +53,7 @@ def subject_fields
# @return [Array<MARC::DataField>] A (possibly empty) array of LC subject fields and their
# linked counterparts, if any
def lc_subject_fields
sfields = @record.select { |field| self.class.lc_subject_field?(field) }
sfields = @record.select { |field| Subject.lc_subject_field?(field) }
sfields + sfields.flat_map { |field| _linked_fields_for(field) }.compact
end

Expand Down
21 changes: 20 additions & 1 deletion umich_catalog_indexing/lib/common/subjects/subject.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
module Common
class Subjects
class Subject
module Subject
class << self
def subject_field?(field)
SUBJECT_FIELDS.include?(field.tag)
end

# Delegate LC determination to the class itself.
def lc_subject_field?(field)
LCSubject.lc_subject_field?(field)
end

# Pass off a new subject to the appropriate class
def new(field)
if lc_subject_field?(field)
LCSubject.from_field(field)
else
NonLCSubject.new(field)
end
end
end
end
end
end
8 changes: 4 additions & 4 deletions umich_catalog_indexing/lib/traject/macros/common/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ def lcsh_subjects
->(record, accumulator, context) do
# subject_fields = Common::Subject.lc_subject_fields(record)
subject_fields = context.clipboard[:subject].lc_subject_fields
subjects = subject_fields.map { |f| Common::Subjects.for(f) }
subjects = subject_fields.map { |f| Common::Subjects::Subject.new(f) }
accumulator.replace subjects.map { |s| s.subject_string(" -- ") }
end
end

def remediated_lcsh_subjects
->(record, accumulator, context) do
subject_fields = context.clipboard[:subject].remediated_lc_subject_fields
subjects = subject_fields.map { |f| Common::Subjects.for(f) }
subjects = subject_fields.map { |f| Common::Subjects::Subject.new(f) }
accumulator.replace subjects.map { |s| s.subject_string(" -- ") }
end
end

def non_lcsh_subjects
->(record, accumulator, context) do
subject_fields = context.clipboard[:subject].non_lc_subject_fields
subjects = subject_fields.map { |f| Common::Subjects.for(f) }
subjects = subject_fields.map { |f| Common::Subjects::Subject.new(f) }
accumulator.replace subjects.map { |s| s.subject_string(" -- ") }
end
end

def subject_browse_subjects
->(record, accumulator, context) do
subject_fields = context.clipboard[:subject].subject_browse_fields
subjects = subject_fields.map { |f| Common::Subjects.for(f) }
subjects = subject_fields.map { |f| Common::Subjects::Subject.new(f) }
accumulator.replace subjects.map { |s| s.subject_string }
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
["c", "c"])
end
subject do
Common::Subjects.for(non_lc_subject_field)
Common::Subjects::Subject.new(non_lc_subject_field)
end
context "lc_subject_field?" do
it "is false" do
Expand Down
37 changes: 37 additions & 0 deletions umich_catalog_indexing/spec/common/subjects/subject_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "common/subjects"
RSpec.describe Common::Subjects::Subject do
def get_record(path)
MARC::XMLReader.new(path).first
end
let(:record) do
get_record("./spec/fixtures/unauthorized_immigrants.xml")
end
let(:subject_fields) do
Common::Subjects.new(record).subject_fields
end
let(:lc_subject_field) do
subject_fields.first
end
let(:non_lc_subject_field) do
subject_fields[2]
end

context ".subject_field?" do
it "returns true for appropriate subject field" do
expect(described_class.subject_field?(lc_subject_field)).to eq(true)
end
it "returns false for field with incorrect tag" do
not_subject = instance_double(MARC::DataField, tag: "800")
expect(described_class.subject_field?(not_subject)).to eq(false)
end
end

context ".new" do
it "returns an object that knows it's an LCSubject" do
expect(described_class.new(lc_subject_field).lc_subject_field?).to eq(true)
end
it "returns an object that knows it's an Non LCSubject" do
expect(described_class.new(non_lc_subject_field).lc_subject_field?).to eq(false)
end
end
end
Loading

0 comments on commit 4054fcc

Please sign in to comment.