Skip to content

Commit

Permalink
docs: comments in subjects and remediated subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Aug 19, 2024
1 parent 3f97f98 commit 10841e9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/authority_browse/remediated_subjects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module AuthorityBrowse
class RemediatedSubjects
include Enumerable

# List of RemediatedSubjects::Entriees
# @param file_path [String] Path to config file with remediated subjects
# info
def initialize(file_path = S.remediated_subjects_file)
xml_lines = File.readlines(file_path)
@entries = xml_lines.map do |line|
Expand All @@ -14,6 +17,8 @@ def each(&block)
end

class Entry
# An Authority Record Entry
# @param xml [String] Authority Record MARCXML String
def initialize(xml)
@record = MARC::XMLReader.new(StringIO.new(xml)).first
end
Expand All @@ -26,6 +31,8 @@ def preferred_term
@preferred_term ||= Term::Preferred.new(@record["150"])
end

# Returns the cross references found in the 450 and 550 fields
# @return [Array<Term>] An Array of xref terms
def xrefs
@record.fields(["450", "550"]).map do |field|
[Term::SeeInstead, Term::Broader, Term::Narrower].find do |kind|
Expand All @@ -34,6 +41,9 @@ def xrefs
end.compact
end

# Adds the preferred term and xrefs to the subjects and subjects_xrefs
# db tables
# @return [Nil]
def add_to_db
preferred_term.add_to_db(id)
xrefs.each do |xref|
Expand All @@ -43,20 +53,29 @@ def add_to_db
end

class Term
# @param field [MARC::DataField] A subject term field
def initialize(field)
@field = field
end

# What kind of field it is. It's used for setting the xref_kind in the subjects_xrefs table.
def kind
raise NotImplementedError
end

# This is the first step in adding the xref to term to the database. It's
# overwritten for a PreferredTerm. The check for id and match_text is to
# make sure the id isn't already in the db. If the id given is the match
# text that means the term isn't in the db.
#
# @param preferred_term_id [[TODO:type]] [TODO:description]
def add_to_db(preferred_term_id)
if id == match_text
AuthorityBrowse.db[:subjects].insert(id: id, label: label, match_text: match_text, deprecated: false)
end
end

# @return [String]
def label
@field.subfields
.filter_map do |x|
Expand All @@ -65,15 +84,20 @@ def label
.join("--")
end

# @return [String]
def match_text
AuthorityBrowse::Normalize.match_text(label)
end

# @return [String]
def id
AuthorityBrowse.db[:subjects]&.first(match_text: match_text)&.dig(:id) || match_text
end

class Preferred < Term
# Adds the preferred term to the db
#
# @return nil
def add_to_db(id)
AuthorityBrowse.db[:subjects].insert(id: id, label: label, match_text: match_text, deprecated: false)
end
Expand All @@ -88,6 +112,8 @@ def kind
"see_instead"
end

# @param preferred_term_id [String]
# @return [Nil]
def add_to_db(preferred_term_id)
super
xrefs = AuthorityBrowse.db[:subjects_xrefs]
Expand All @@ -104,6 +130,8 @@ def kind
"broader"
end

# @param preferred_term_id [String]
# @return [Nil]
def add_to_db(preferred_term_id)
super
xrefs = AuthorityBrowse.db[:subjects_xrefs]
Expand All @@ -121,6 +149,8 @@ def kind
"narrower"
end

# @param preferred_term_id [String]
# @return [Nil]
def add_to_db(preferred_term_id)
super
xrefs = AuthorityBrowse.db[:subjects_xrefs]
Expand Down
11 changes: 11 additions & 0 deletions lib/authority_browse/subjects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ def kind
"subject"
end

# Fetches and writes the MARCXML Authority record data for remediated
# subjects to a file.
#
# @param file_path [String] Path to remediated subjects config file
# @param set_id [String] Alma Set ID for remediated subjects authority records
# @return [Nil]
def generate_remediated_authorities_file(file_path: S.remediated_subjects_file, set_id: S.subject_heading_remediation_set_id)
conn = Faraday.new do |conn|
conn.options.timeout = 10 * 60
Expand Down Expand Up @@ -76,6 +82,11 @@ def reset_db(loc_file_getter = lambda { fetch_skos_file })
end
end

# Adds the remediated subject information to the `subjects` and
# `subjects_xrefs` table of the database
#
# @param file_path [String] Path to remediated subjects config file
# @return [Nil]
def incorporate_remediated_subjects(file_path = S.remediated_subjects_file)
subjects = AuthorityBrowse::RemediatedSubjects.new(file_path)
AuthorityBrowse.db.transaction do
Expand Down

0 comments on commit 10841e9

Please sign in to comment.