lib/marc_cleanup/bib_rubymarc_fixes.rb
### Sort subfields for target fields with an arbitrary order
Sort subfields for target fields with an arbitrary order
Sort subfields for target fields with an arbitrary order
def subfield_sort(record, target_tags, order_array = nil)
    target_fields = record.fields.select { |f| target_tags.include?(f.tag) }
    return record if target_fields.empty?
    target_fields.each do |field|
      next unless field.class == MARC::DataField
      orig_codes = field.subfields.map { |subfield| subfield.code }.uniq.sort
      order_array = orig_codes if order_array.nil?
      new_subfields = []
      order_array.each do |code|
        next unless orig_codes.include?(code)
        target_subf = field.subfields.select { |subfield| subfield.code == code }
        target_subf.each { |subfield| new_subfields << subfield }
      end
      rem_subfields = field.subfields.select { |subf| !order_array.include?(subf.code) }
      rem_subfields.each do |subfield|
        new_subfields << subfield
      end
      field.subfields = new_subfields
    end
    record
  end
def subfield_sort ( record , target_tags , order_array = nil ) target_fields = record . fields . select { | f | target_tags . include? ( f . tag ) } return record if target_fields . empty? target_fields . each do | field | next unless field . class == MARC :: DataField orig_codes = field . subfields . map { | subfield | subfield . code } . uniq . sort order_array = orig_codes if order_array . nil? new_subfields = [ ] order_array . each do | code | next unless orig_codes . include? ( code ) target_subf = field . subfields . select { | subfield | subfield . code == code } target_subf . each { | subfield | new_subfields << subfield } end rem_subfields = field . subfields . select { | subf | ! order_array . include? ( subf . code ) } rem_subfields . each do | subfield | new_subfields << subfield end field . subfields = new_subfields end record end
Sort subfields for target fields with an arbitrary order
Sort subfields for target fields with an arbitrary order
record
target_tags
order_array