module Traject::Profiling::Macros
traject “macros” to be used with to_field in a traject config file
traject “macros” to be used with to_field in a traject config file
Public Instance Methods
gets the all the subfield codes for a tag in a marc record.
If no occurrences of the tag in the marc record, accumulator is not altered (field should be missing in output_hash). If multiple occurrences, there is a single output value for each unique subfield code unless dedup=false.
@param [String] tag - marc field tag; three chars (usually but not necessarily numeric) @param [Boolean] dedup - set to false if duplicate values should produce duplicate output values @return [lambda] lambda expression appropriate for “to_field”, with the subfield codes
for tag param added to in the lambda's accumulator param
# File lib/traject/profiling/field_macros.rb, line 56 def field_codes(tag, dedup=true) lambda do |record, accumulator, _context| codes = [] record.each_by_tag(tag) do |fld| codes << fld.codes(dedup) end if dedup accumulator.replace codes.flatten.uniq else accumulator.replace codes.flatten end end end
counts the number of occurrences of a tag in a marc record.
If no occurrences, accumulator is not altered (field should be missing in output_hash)
@param [String] tag - marc field tag; three chars (usually but not necessarily numeric) @return [lambda] lambda expression appropriate for “to_field”, with the number of marc fields
matching the tag param added to in the lambda's accumulator param
# File lib/traject/profiling/field_macros.rb, line 12 def field_count(tag) lambda do |record, accumulator, _context| num_fields = record.fields(tag).size accumulator << num_fields.to_s if num_fields > 0 end end
gets the all the values of an indicator for a tag in a marc record.
If no occurrences of the tag in the marc record, accumulator is not altered (field should be missing in output_hash). If multiple occurrences, there is a single output value for each unique indicator value unless dedup=false.
@param [String] tag - marc field tag; three chars (usually but not necessarily numeric) @param [Object] which_ind - can be '1' or '2' (Strings) or 1 or 2 (int);
any other value and accumulator is not altered (field should be missing in output_hash)
@param [Boolean] dedup - set to false if duplicate values should produce duplicate output values @return [lambda] lambda expression appropriate for “to_field”, with the values of the specified
indicator for tag param added to in the lambda's accumulator param
# File lib/traject/profiling/field_macros.rb, line 29 def field_ind(tag, which_ind, dedup=true) lambda do |record, accumulator, _context| ind_vals = [] record.each_by_tag(tag) do |fld| case which_ind when '1', 1 ind_vals << fld.indicator1.to_s when '2', 2 ind_vals << fld.indicator2.to_s end end if dedup accumulator.replace ind_vals.uniq else accumulator.replace ind_vals end end end
gets the all the subfield codes in 880s for a tag in a marc record.
If no occurrences of the 880 for the tag in the marc record, accumulator is not altered (field should be missing in output_hash). If multiple occurrences for a code, there is a single output value for each unique subfield code unless dedup=false.
@param [Boolean] dedup - set to false if duplicate values should produce duplicate output values
# File lib/traject/profiling/f880_macros.rb, line 33 def tag_codes_in_880s(tag, dedup=true) lambda do |record, accumulator, _context| codes = [] record.each_by_tag('880') do |field| tag_in_880 = field['6'][0, 3] if tag_in_880 == tag codes << field.codes(dedup) codes.flatten! if dedup accumulator.replace codes.uniq - ['6'] else accumulator.replace codes - ['6'] # 6 is a non-repeatable code end end end end end