Class: Kiba::Extend::Transforms::Marc::ExtractSubfieldsFromField

Inherits:
Object
  • Object
show all
Includes:
FieldLinkable
Defined in:
lib/kiba/extend/transforms/marc/extract_subfields_from_field.rb

Overview

For each occurrence of given field tag, outputs a row with the following columns: marcid, fullfield, and one column per specified subfield. If there are more than one occurrnces of a subfield within a given field, the multiple values are separated by the delim value

Examples:

# =001  008000103-3
# =260  \\$aLahore :$bZia-ul-Qurʾaan Publications,$c1996.
rec = get_marc_record(index: 0)
xform = Marc::ExtractSubfieldsFromField.new(
  tag: '260', subfields: %w[a b e f]
)
results = []
xform.process(rec){ |row| results << row }
expect(results.length).to eq(1)
first = {
  :full260=>"260    $a Lahore : $b Zia-ul-Qurʾaan Publications, "\
    "$c 1996. ",
  :_260a=>"Lahore :", :_260b=>"Zia-ul-Qurʾaan Publications,",
  :_260e=>nil, :_260f=>nil, :marcid=>"008000103-3"
}
expect(results[0]).to eq(first)
# =001  008000411-3
# =260  \\$aSan Jose, Calif. ;$aNew York, NY :$bH.M. Gousha
#           Co.,$c[1986?]
rec = get_marc_record(index: 3)
xform = Marc::ExtractSubfieldsFromField.new(
  tag: '260', subfields: %w[a b e f]
)
results = []
xform.process(rec){ |row| results << row }
expect(results.length).to eq(1)
first = {
  :full260=>"260    $a San Jose, Calif. ; $a New York, NY : $b "\
    "H.M. Gousha Co., $c [1986?] ",
  :_260a=>"San Jose, Calif. ;|New York, NY :",
  :_260b=>"H.M. Gousha Co.,",
  :_260e=>nil, :_260f=>nil, :marcid=>"008000411-3"
}
expect(results[0]).to eq(first)

Since:

  • 4.0.0

Instance Method Summary collapse

Methods included from FieldLinkable

#select_fields

Constructor Details

#initialize(tag:, subfields:, indicators: false, id_target: Kiba::Extend::Marc.id_target_field, delim: Kiba::Extend.delim) ⇒ ExtractSubfieldsFromField

Returns a new instance of ExtractSubfieldsFromField.

Parameters:

  • tag (String)

    MARC tag from which to extract subfield values

  • subfields (Array<String>)

    subfield codes from which to extract values

  • indicators (Boolean) (defaults to: false)

    whether to output indicators as separate field values

  • id_target (Symbol) (defaults to: Kiba::Extend::Marc.id_target_field)

    name of field in which to write id value

  • delim (String) (defaults to: Kiba::Extend.delim)

    used when joining multiple values from recurring subfield

Since:

  • 4.0.0



63
64
65
66
67
68
69
70
71
72
# File 'lib/kiba/extend/transforms/marc/extract_subfields_from_field.rb', line 63

def initialize(tag:, subfields:, indicators: false,
  id_target: Kiba::Extend::Marc.id_target_field,
  delim: Kiba::Extend.delim)
  @tag = tag
  @subfields = subfields
  @indicators = indicators
  @id_target = id_target
  @delim = delim
  @idextractor = Kiba::Extend::Utils::MarcIdExtractor.new
end

Instance Method Details

#process(record) {|row| ... } ⇒ Object

Parameters:

  • record (MARC::Record)

Yield Parameters:

  • row (Hash{ Symbol => String, nil })

Since:

  • 4.0.0



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kiba/extend/transforms/marc/extract_subfields_from_field.rb', line 76

def process(record)
  fields = select_fields(record, [tag])
    .reject { |fld| fld.codes.intersection(subfields).empty? }
  return nil if fields.empty?

  idhash = {id_target => idextractor.call(record)}

  prepare_rows(fields, idhash).each do |row|
    yield row
  end

  nil
end