#MARCAdditions are some useful additions made for processing MARC records. module MARC class Record # linter depends on Perl's MARC::Lint and hence Perl's MARC::Record # Use cpan to install them. def linter #puts self.to_xml #puts (self.to_xml).to_s rec = self.dup #rec.leader[9] = ' ' #puts rec xml_rec = (rec.to_xml).to_s #puts xml_rec if LINTER == false puts 'You do not have the Perl MARC::Lint module installed or have disabled this feature.' return end #xml_record = self.to_xml #puts self #STDIN.gets #puts #contents = `perl linter.pl "#{self}"` contents = `perl "#{File.expand_path("~")}"/.zcc/linter.pl "#{xml_rec}"` #aFile = File.new("temp_lint.mrc","w") #aFile.write(self.to_marc) #aFile.close #contents = `perl linter.pl` if contents.empty? puts "there were no errors detected by the linter." else puts contents end if self.leader[18,1] == 'a' puts "AACR is good." elsif self.leader[18,1] == 'i' puts "ISBD is ok" else puts "Is this record any good?" end end def blank_field_prompt(field, subfield) #puts "subfield: #{subfield}" print "\nYour MARC record does not contain #{field}#{subfield}.\nEnter the intended value for #{field}#{subfield} or hit ENTER to leave blank\n#{field}#{subfield} > " value = STDIN.gets.chomp value end # To use marc_to_csv it must be passed a csv template in the order of the fields. # See the zoomer.yaml file for instructions on creating a template. # See the main script for turning this feature on. def marc_to_csv(template) values = [] # blank_field_prompt = 'puts "subfield: #{subfield}"; print "There is no valid value in your MARC record.\n Please enter the intended value for #{field}#{subfield} or Enter to leave blank\n#{field}#{subfield}> "; value = STDIN.gets.chomp' template.each do |template| field, subfield, leng = template field = field.to_s subfield = subfield.to_s if field == 'prompt' puts "prompt: please enter the #{field} #{subfield}" #--subfield here is a label for the prompt value = STDIN.gets.chomp value = "\"#{value}\"," values << value elsif field.length == 3 and field.match('\d') fields = self.find_all { | f | f.tag =~ Regexp.new( field.gsub('X','.'))} value = '' if fields.empty? #puts "oh, no!" value = blank_field_prompt(field, subfield) #eval(blank_field_prompt) else fields.each do |f| if f[subfield] value += f[subfield] + "|" else value = blank_field_prompt(field, subfield) end end end value.sub!(/\|$/, '') value = value[0, leng] if leng #puts "value: #{value}" value = "\"#{value}\"," values << value else puts "this is not a valid value for marc_to_csv" end end #puts values values[-1].sub!(/\,$/, '') values = values.to_s values += "\n" end #++ The local_script method is for automating changes to the record before saving it. See the main script to turn on this feature. See zoomer.yaml for instructions on creating a script for your purposes. def local_script(script) #print $clear_code record = self #--creating my procs for creating fields and appending subfields # these two should probably be moved to methods create_datafield = proc{|record, field, i1, i2| puts "Creating datafield: #{field}"; record.append(MARC::DataField.new(field.to_s, i1.to_s, i2.to_s)); puts "****", record, "***" if $testing;} append_subfield = proc{|record, field, subfield, val| field = field.to_s subfield = subfield.to_s value = val.dup value = value.to_s if value.include?('proc ') #I've had problems with this bit but it seems to work now value = value.sub(/^proc /, '') real_value = eval("#{$procs[value]}") next if real_value == nil record[field].append(MARC::Subfield.new(subfield, real_value)) elsif record[field].append(MARC::Subfield.new(subfield, value)) end } script.each do |single_script| puts "--------------------------------------" if $testing puts single_script.join(', ') if $testing op, field, subfield, filler= single_script field = field.to_s subfield = subfield.to_s operation = op.dup if operation.include?('proc ') operation = operation.dup.sub(/^proc /, '') eval("#{$procs[operation]}") elsif operation == 'create-field' create_datafield.call(record, field, subfield, filler) elsif operation == 'append-subfield' append_subfield.call(record, field, subfield, filler) elsif operation == 'prompt' print "What do you want to go into the #{field}#{subfield} #{filler}? " field_data = STDIN.gets.chomp next if field_data.empty? m_fields = record.find_all{|x| x.tag == field} if m_fields.empty? puts "m_fields is empty!!" create_datafield.call(record, field, ' ', ' ') m_fields = record.find_all{|x| x.tag == field} end m_fields.each {|field| field.append(MARC::Subfield.new(subfield, field_data))} elsif operation == 'remove' to_remove = record.find_all {|f| f.tag =~ Regexp.new( field.gsub('X','.'))} to_remove.each do |remove| record.fields.delete(remove) end elsif operation == 'sort-tags' ##This doesn't work right now puts "sorting by tag" puts record , "################" record = record.sort_by{| field | field.tag} puts record STDIN.gets else puts "there's nothing for that yet in local_script" end puts record if $testing end record end #++ compare() is a method to print out a comparison of two MARC records tag by tag (not by subfields). # A match between lines is denoted with a 'm'. If there are differences between the records, # the object that recieves the compare call is denoted with a '+' and the object passed in # parens is denoted with '-'. def compare_marc(rec2) orig = self.dup for ft in ('000'..'999') fields_original = orig.find_all {|f| f.tag == ft} fields_record2 = rec2.find_all {|f| f.tag == ft} fields_orig = [] fields_rec2 = [] fields_original.each {|f| fields_orig << f.to_s} fields_record2.each {|f| fields_rec2 << f.to_s} matches = fields_orig & fields_rec2 matches.each { |f| puts "m #{f}" } if matches fields_orig -= matches fields_orig.each {|f| puts "+ #{f}"} if fields_orig fields_rec2 -= matches fields_rec2.each {|f| puts "- #{f}"} if fields_rec2 end end end end module ZCC def ZCC.lccn_conversion lccn split_lccn = lccn.split('-') year_len = split_lccn[0].length serial_len = split_lccn[1].length start_length = year_len + serial_len if year_len == 2 final_lccn = lccn.gsub('-', "#{'0' * (8 - start_length)}") elsif year_len == 4 final_lccn = lccn.gsub('-', "#{'0' * (10 - start_length)}") end final_lccn end end #class String # def to_proc # eval "Proc.new #{self} " # or "Proc.new #{self}" in my case # end #end