Class: RecordImpl

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/marc4j4r.rb

Overview

Open up RecordImpl to add some sugar, including Enumberable as well

Author:

Instance Method Summary

Instance Method Details

- (Field) [](tag)

Get the first field associated with a tag

Parameters:

  • (String) tag — The tag

Returns:

  • (Field) — The first matching field, or nil if none. Note that to mirror ruby-marc, this returns a single field


263
264
265
266
267
268
269
270
271
272
273
# File 'lib/marc4j4r.rb', line 263

def [] tag
  if defined? @hashedtags
    if @hashedtags[tag]
      return @hashedtags[tag][0]
    else 
      return nil
    end
  else    
    return self.getVariableField(tag)
  end
end

- (Object) each

Cycle through the fields in the order the appear in the record



252
253
254
255
256
# File 'lib/marc4j4r.rb', line 252

def each
  self.getVariableFields.each do |f|
    yield f
  end
end

- (Array<Field>) find_by_tag(tags, originalorder = false)

Get a (possibly empty) list of fields with the given tag(s)

Examples:

originalorder == false

 # Given a record that looks like 
 # 010    $a 68027371 
 # 035    $a (RLIN)MIUG0001728-B 
 # 035    $a (CaOTULAS)159818044 
 # 035    $a (OCoLC)ocm00001728

 r.find_by_tag(['035', '010']).each {|f| puts f.to_s}
 # 035    $a (RLIN)MIUG0001728-B 
 # 035    $a (CaOTULAS)159818044 
 # 035    $a (OCoLC)ocm00001728
 # 010    $a 68027371 

# The results are ordered first by tag as passed in, then by original order within the tag

Just get all fields for a single tag

 ohThirtyFives = r.find_by_tag('035')

Get a bunch of standard identifiers

  standardIDs = r.find_by_tag(['022', '020', '010'])

originalorder == true

 r.find_by_tag(['035', '010'], true).each {|f| puts f.to_s}
 # 010    $a 68027371 
 # 035    $a (RLIN)MIUG0001728-B 
 # 035    $a (CaOTULAS)159818044 
 # 035    $a (OCoLC)ocm00001728

Parameters:

  • (String, Array<String>) tags — A string (or Array of strings) with the tags you’re interested in
  • (Boolean) originalorder (defaults to: false) — Whether or not results should be presented in the original order within the record or with a two-column sort of (a) Order of the tag in the list of tags sent, (b) order within that tag in the record

Returns:

  • (Array<Field>) — Either an empty list or a list of one or more matched fields will be returned. originalorder == false will use an internal hash and be faster in many cases (see #hashify)


314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/marc4j4r.rb', line 314

def find_by_tag(tags, originalorder = false)
  self.hashify unless @hashedtags and !originalorder
  if !tags.is_a? Array
    return @hashedtags[tags] || []
  end
  if originalorder
    return self.find_all {|f| tags.include? f.tag}
  else
    # puts "Tags is #{tags}: got #{@hashedtags.values_at(*tags)}"
    return @hashedtags.values_at(*tags).flatten.compact
  end
end

- (Object) hashify

Create a local hash by tag number; makes some stuff faster Called automatically if you use reader.each



227
228
229
230
231
232
233
234
# File 'lib/marc4j4r.rb', line 227

def hashify 
  return if @hashedtags # don't do it more than once
  @hashedtags = {}
  self.getVariableFields.each do |f|
    @hashedtags[f.tag] ||= []
    @hashedtags[f.tag].push f
  end
end

- (Object) leader

Get the leader as a string (marc4j would otherwise return Leader object)



246
247
248
# File 'lib/marc4j4r.rb', line 246

def leader
  self.get_leader.toString
end

- (Object) to_marc



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/marc4j4r.rb', line 345

def to_marc
  begin
    s = Java::java.io.ByteArrayOutputStream.new
    writer =  org.marc4j.MarcStreamWriter.new(s)
    writer.write(self)
    @marcbinary = s.to_string
    puts @marcbinary
    return @marcbinary
  rescue
    # "Woops! to_marc failed for record #{self['001'].data}: #{$!}"
    "Whoops! Failed: #{$!}"
  end
end

- (Object) to_marchash

Export as a MARC-Hash, as described at http://robotlibrarian.billdueber.com/marc-hash-the-saga-continues-now-with-even-less-structure/

Returns:

  • A marc-hash representation of the record, suitable for calling .to_json on or whatever


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/marc4j4r.rb', line 199

def to_marchash
  h = {}
  h['type'] = 'marc-hash'
  h['version'] = [1,0]
  h['leader'] = self.leader
  
  fields = []
  
  self.getVariableFields.each do |f|
    if f.controlField?
      fields << [f.tag, f.value]
    else 
      farray = [f.tag, f.indicator1 || ' ', f.indicator2 || ' ']
      subs = []
      f.each do |subfield|
        subs << [subfield.code, subfield.value]
      end
      farray.push subs
      fields << farray
    end
  end
  h['fields'] = fields
  return h
end

- (Object) to_s

Create a nice string of the record



237
238
239
240
241
242
243
# File 'lib/marc4j4r.rb', line 237

def to_s
  arr = ['LEADER ' + self.leader]
  self.each do |f|
    arr.push f.to_s
  end
  return arr.join("\n")
end

- (Object) to_xml

Return the record as valid MARC-XML

Returns:

  • String A MARC-XML representation of the record, including the XML header


331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/marc4j4r.rb', line 331

def to_xml
  return @xml if @xml
  begin
    @xml = java.io.StringWriter.new
    res = javax.xml.transform.stream.StreamResult.new(@xml)
    writer =  org.marc4j.MarcXmlWriter.new(res)
    writer.write(self)
    writer.writeEndDocument
    return @xml.toString
  rescue
    "Woops! to_xml failed for record #{self['001'].data}: #{$!}"
  end
end