Module: BerkeleyLibrary::AV::Marc::Util

Extended by:
Util
Includes:
Util
Included in:
Util, BerkeleyLibrary::AV::Metadata::Field, Track
Defined in:
lib/berkeley_library/av/marc/util.rb

Constant Summary

Constants included from Util

Util::DEFAULT_USER_AGENT

Instance Method Summary collapse

Methods included from Util

#class_name, #compare_by_attributes, #compare_values, #do_get, #tidy_value

Instance Method Details

#group_subfield_values(data_field, order: nil) ⇒ Array<Hash<Symbol, String>>

Extracts the subfield values from the specifed MARC data field and returns the groups of related subfield values in order as they appear. For instance, for the following data field:

“‘ 505 00$tQuatrain II$g(16:35) –$tWater ways$g(1:57) –$tWaves$g(10:49). “`

this method would return:

“‘ [

{t: 'Quatrain II', g: '(16:35)'},
{t: 'Water ways', g: '(01:57)'},
{t: 'Waves', g: '(10:49)'}

] “‘

If an order is provided, each group will be reordered according to that order. E.g., given the order ‘[:g, :t]`, the above would be returned instead as:

“‘ [

{g: '(16:35)', t: 'Quatrain II'},
{g: '(01:57)', t: 'Water ways'},
{g: '(10:49)', t: 'Waves'},

] “‘

Parameters:

  • data_field (MARC::DataField)

    the data field

  • order (Array<Symbol>, nil) (defaults to: nil)

    the order of subfield codes

Returns:

  • (Array<Hash<Symbol, String>>)

    the grouped values



43
44
45
46
47
48
49
50
# File 'lib/berkeley_library/av/marc/util.rb', line 43

def group_subfield_values(data_field, order: nil)
  # TODO: do we still need this?
  grouped_subfields = group_subfields(data_field.subfields, order:)
  grouped_subfields.each_with_object([]) do |subfield_group, value_groups|
    value_group = subfield_group.transform_values { |sf| tidy_value(sf.value) }
    value_groups << value_group
  end
end

#group_subfields(subfields, order: nil) ⇒ Array<Hash<Symbol, String>>

Extracts the subfieldsfrom the specifed MARC data field and returns the groups of related subfieldsin order as they appear. For instance, for the following data field:

“‘ 505 00$tQuatrain II$g(16:35) –$tWater ways$g(1:57) –$tWaves$g(10:49). “`

this method would return:

“‘ [

{t: #<Subfield @code='t', @value='Quatrain II'>, g: #<Subfield @code='g', @value='(16:35)'>},
{t: #<Subfield @code='t', @value='Water ways'>, g: #<Subfield @code='g', @value='(01:57)'>},
{t: #<Subfield @code='t', @value='Waves'>, g: #<Subfield @code='g', @value='(10:49)'>}

] “‘

If an order is provided, each group will be reordered according to that order. E.g., given the order ‘[:g, :t]`, the above would be returned instead as:

“‘ [

{g: #<Subfield @code='g', @value='(16:35)'>, t: #<Subfield @code='t', @value='Quatrain II'>},
{g: #<Subfield @code='g', @value='(01:57)'>, t: #<Subfield @code='t', @value='Water ways'>},
{g: #<Subfield @code='g', @value='(10:49)'>, t: #<Subfield @code='t', @value='Waves'>},

] “‘

Parameters:

  • data_field (MARC::DataField)

    the data field

  • order (Array<Symbol>, nil) (defaults to: nil)

    the order of subfield codes

Returns:

  • (Array<Hash<Symbol, String>>)

    the grouped values



85
86
87
88
89
# File 'lib/berkeley_library/av/marc/util.rb', line 85

def group_subfields(subfields, order: nil)
  by_code = subfields_by_code(subfields)
  order = by_code.keys if order.nil? || order.empty?
  group_by_code(by_code, order)
end