Module: Traject::Profiling::Macros

Defined in:
lib/traject/profiling/f880_macros.rb,
lib/traject/profiling/field_macros.rb

Overview

traject “macros” to be used with #to_field in a traject config file

Instance Method Summary collapse

Instance Method Details

#field_codes(tag, dedup = true) ⇒ lambda

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.

Parameters:

  • tag (String)
    • marc field tag; three chars (usually but not necessarily numeric)

  • dedup (Boolean) (defaults to: true)
    • set to false if duplicate values should produce duplicate output values

Returns:

  • (lambda)

    lambda expression appropriate for “to_field”, with the subfield codes for tag param added to in the lambda’s accumulator param



56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ lambda

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)

Parameters:

  • tag (String)
    • marc field tag; three chars (usually but not necessarily numeric)

Returns:

  • (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



12
13
14
15
16
17
# 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) ⇒ lambda

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.

Parameters:

  • tag (String)
    • marc field tag; three chars (usually but not necessarily numeric)

  • which_ind (Object)
    • 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)

  • dedup (Boolean) (defaults to: true)
    • set to false if duplicate values should produce duplicate output values

Returns:

  • (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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object

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.

Parameters:

  • dedup (Boolean) (defaults to: true)
    • set to false if duplicate values should produce duplicate output values



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object

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.

Parameters:

  • dedup (Boolean) (defaults to: true)
    • set to false if duplicate values should produce duplicate output values



55
56
57
58
59
60
61
62
63
# 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) ⇒ lambda

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

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)

Parameters:

  • dedup (Boolean) (defaults to: true)
    • set to false if duplicate values should produce duplicate output values

  • tag (String)
    • marc field tag; three chars (usually but not neccesarily numeric)

Returns:

  • (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



15
16
17
18
19
20
21
22
23
24
25
26
# 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