Class: MARC::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/zcc/subfieldeditor.rb,
lib/zcc/marcadditions.rb

Overview

edit is a wrapper method to loop until done

Instance Method Summary collapse

Instance Method Details

#blank_field_prompt(field, subfield) ⇒ Object



48
49
50
51
52
53
# File 'lib/zcc/marcadditions.rb', line 48

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

#change_subfield(fs) ⇒ Object

edit_subfield is passed to a MARC::Record object and give a subfield



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/zcc/subfieldeditor.rb', line 26

def change_subfield(fs)
  field_subfield = subfield_parse(fs)
  field = field_subfield[0]
  subfield = field_subfield[1]

  fields = self.find_all {|f| f.tag == field}
  subfields = []
  for field in fields
   subfields << field.find_all {|s| s.code == subfield}
  end
  subfields.flatten!
  if subfields.length > 1
    i = 0
    for subfield in subfields
     puts "#{i}\t#{subfield}"
      i += 1
    end
   num_to_change = ask("Which subfield do you want to change?"){|q| q.readline = true}
   subfield_to_change = subfields[num_to_change.to_i]
  elsif subfields.length == 1
    subfield_to_change = subfields[0]
  elsif subfields.empty?
    puts "No such subfield!"
   return nil
  end
  

  puts self; puts

  print "Currently:\t"
  puts subfield_to_change.value 
  edit = ask("Your edit:\t"){|q| q.readline = true}
  subfield_to_change.value = edit
  puts; puts self; puts

end

#compare_marc(rec2) ⇒ Object

++ 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 ‘-’.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/zcc/marcadditions.rb', line 178

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

#editObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zcc/subfieldeditor.rb', line 7

def edit
  puts "Subfield Editing"
  puts "To denote which subfield you want to edit put it in the form of 245a.\nIn the case of repeating fields or subfields you will be prompted\nto choose the one you wish to edit. "
  continue = true
  while continue
   fs = ask("What field and subfield would you like to edit (in form 245a) or quit?\n>   "){|q| q.readline = true}
    if fs == 'q' || fs == 'quit'
      continue = false
    next
    end
    if !(fs =~/\d\d\d\w/ )
      puts "That's not a valid value"
      next
    end
    self.change_subfield(fs)
  end
end

#linterObject

linter depends on Perl’s MARC::Lint and hence Perl’s MARC::Record Use cpan to install them.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zcc/marcadditions.rb', line 6

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

#local_script(script) ⇒ Object

++ 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.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/zcc/marcadditions.rb', line 105

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

#marc_to_csv(template) ⇒ Object

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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zcc/marcadditions.rb', line 58

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

#subfield_parse(combined) ⇒ Object

subfield_parse method for getting from ‘245a’ to [‘245’,‘a’]



64
65
66
67
68
# File 'lib/zcc/subfieldeditor.rb', line 64

def subfield_parse(combined)
  field_subfield = []
  field_subfield << combined[0, 3]
  field_subfield << combined[3,1]
end