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

field_codes(tag, dedup=true) click to toggle source

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
field_count(tag) click to toggle source

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
field_ind(tag, which_ind, dedup=true) click to toggle source

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
tag_codes_in_880s(tag, dedup=true) click to toggle source

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
tags_for_unassociated_880s(dedup=true) click to toggle source

Get the tag of the associated field of an 880 when the |6 linkage occurrence number is 00 or

when the linkage refers to a field not present in the Marc::Record object.
e.g.  880 has subfield 6 with value 260-00, so '260' is added to the accumulator.

@param [Boolean] dedup - set to false if duplicate values should produce duplicate output values

# File lib/traject/profiling/f880_macros.rb, line 55
def tags_for_unassociated_880s(dedup=true)
  lambda do |record, accumulator, _context|
    record.each_by_tag('880') do |field|
      if field['6'][4, 2] == '00' || record.fields(field['6'][0, 3]).empty?
        accumulator << field['6'][0, 3]
      end
    end
  end
end
tags_with_880s(dedup=true) click to toggle source

Get the tags of fields associated with every 880 field

If multiple occurrences, there is a single output value for each unique indicator value unless dedup=false

@param [Boolean] dedup - set to false if duplicate values should produce duplicate output values counts the number of occurrences of a field 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 neccesarily 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/f880_macros.rb, line 15
def tags_with_880s(dedup=true)
  lambda do |record, accumulator, _context|
    record.each_by_tag('880') do |field|
      tag = field['6'][0, 3]
      if dedup
        accumulator << tag unless accumulator.include? tag
      else
        accumulator << tag
      end
    end
  end
end