Class: MARC::Record

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

Overview

A class that represents an individual MARC record. Every record is made up of a collection of MARC::DataField objects.

MARC::Record mixes in Enumerable to enable access to constituent DataFields. For example, to return a list of all subject DataFields:

  record.find_all {|field| field.tag =~ /^6../}

The accessor ‘fields’ is also an Array of MARC::DataField objects which the client can access or modifyi if neccesary.

  record.fields.delete(field)

Other accessor attribute: ‘leader’ for record leader as String

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Record) initialize

A new instance of Record



88
89
90
91
92
93
94
95
96
# File 'lib/marc/record.rb', line 88

def initialize
  @fields = FieldMap.new
  # leader is 24 bytes
  @leader = ' ' * 24
  # leader defaults:
  # http://www.loc.gov/marc/bibliographic/ecbdldrd.html
  @leader[10..11] = '22'
  @leader[20..23] = '4500'
end

Instance Attribute Details

- (Object) leader

the record leader



86
87
88
# File 'lib/marc/record.rb', line 86

def leader
  @leader
end

Class Method Details

+ (Object) new_from_hash(h)



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/marc/record.rb', line 252

def self.new_from_hash(h)
  r = self.new
  r.leader = h['leader']
  if h['fields']
    h['fields'].each do |position|
      position.each_pair do |tag, field|
        if field.is_a?(Hash)
          f = MARC::DataField.new(tag, field['ind1'], field['ind2'])
          field['subfields'].each do | pos |
            pos.each_pair do |code, value|
              f.append MARC::Subfield.new(code, value)
            end
          end
          r << f
        else
          r << MARC::ControlField.new(tag, field)
        end
      end
    end
  end  
  return r            
end

+ (Object) new_from_marc(raw, params = {})

Factory method for creating a MARC::Record from MARC21 in transmission format.

  record = MARC::Record.new_from_marc(marc21)

in cases where you might be working with somewhat flawed MARC data you may want to use the :forgiving parameter which will bypass using field byte offsets and simply look for the end of field byte to figure out the end of fields.

 record = MARC::Record.new_from_marc(marc21, :forgiving => true)


179
180
181
# File 'lib/marc/record.rb', line 179

def self.new_from_marc(raw, params={})
  return MARC::Reader.decode(raw, params)
end

+ (Object) new_from_marchash(mh)

Factory method for creating a new MARC::Record from a marchash object

record = MARC::Record->new_from_marchash(mh)



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/marc/record.rb', line 228

def self.new_from_marchash(mh)
  r = self.new()
  r.leader = mh['leader']
  mh['fields'].each do |f|
    if (f.length == 2) 
      r << MARC::ControlField.new(f[0], f[1])
    elsif 
      r << MARC::DataField.new(f[0], f[1], f[2], *f[3])
    end
  end
  return r
end

Instance Method Details

- (Object) <<(field)

alias to append



107
108
109
# File 'lib/marc/record.rb', line 107

def <<(field)
  append(field)
end

- (Object) ==(other)

For testing if two records can be considered equal.



287
288
289
# File 'lib/marc/record.rb', line 287

def ==(other)
  return self.to_s == other.to_s
end

- (Object) =~(regex)

Handy for using a record in a regex:

  if record =~ /Gravity's Rainbow/ then print "Slothrop" end


295
296
297
# File 'lib/marc/record.rb', line 295

def =~(regex)
  return self.to_s =~ regex 
end

- (Object) [](tag)

You can lookup fields using this shorthand:

  title = record['245']


138
139
140
# File 'lib/marc/record.rb', line 138

def [](tag)
  return self.find {|f| f.tag == tag}
end

- (Object) append(field)

add a field to the record

  record.append(MARC::DataField.new( '100', '2', '0', ['a', 'Fred']))


101
102
103
# File 'lib/marc/record.rb', line 101

def append(field)
  @fields.push(field)
end

- (Object) each

each() is here to support iterating and searching since MARC::Record mixes in Enumerable

iterating through the fields in a record:

  record.each { |f| print f }

getting the 245

  title = record.find {|f| f.tag == '245'}

getting all subjects

  subjects = record.find_all {|f| ('600'..'699') === f.tag}


123
124
125
126
127
# File 'lib/marc/record.rb', line 123

def each
  for field in @fields
    yield field
  end
end

- (Object) each_by_tag(filter)

A more convenient way to iterate over each field with a given tag. The filter argument can be a string, array or range.



131
132
133
# File 'lib/marc/record.rb', line 131

def each_by_tag(filter)
  @fields.each_by_tag(filter) {|tag| yield tag }
end

- (Object) fields(filter = nil)

Provides a backwards compatible means to access the FieldMap. No argument returns the FieldMap array in entirety. Providing a string, array or range of tags will return an array of fields in the order they appear in the record.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/marc/record.rb', line 146

def fields(filter=nil)
  return @fields.to_a unless filter
  @fields.reindex unless @fields.in_sync?
  flds = []
  if filter.is_a?(String) && @fields.tags[filter]
    @fields.tags[filter].each do |idx|
      flds << @fields[idx]
    end
  elsif filter.is_a?(Array) || filter.is_a?(Range)
    @fields.each_by_tag(filter) do |tag|
      flds << tag
    end
  end
  flds
end

- (Object) tags

Returns an array of all of the tags that appear in the record (not necessarily in the order they appear).



163
164
165
# File 'lib/marc/record.rb', line 163

def tags
  return @fields.tag_list
end

- (Object) to_dublin_core

Handy method for returning a hash mapping this records values to the Dublin Core.

  dc = record.to_dublin_core()
  print dc['title']


209
210
211
# File 'lib/marc/record.rb', line 209

def to_dublin_core
  return MARC::DublinCore.map(self)
end

- (Object) to_hash

Returns a (roundtrippable) hash representation for MARC-in-JSON



244
245
246
247
248
249
250
# File 'lib/marc/record.rb', line 244

def to_hash
  record_hash = {'leader'=>@leader, 'fields'=>[]}
  @fields.each do |field|
    record_hash['fields'] << field.to_hash
  end
  record_hash
end

- (Object) to_marc

Returns a record in MARC21 transmission format (ANSI Z39.2). Really this is just a wrapper around MARC::MARC21::encode

  marc = record.to_marc()


189
190
191
# File 'lib/marc/record.rb', line 189

def to_marc 
  return MARC::Writer.encode(self)
end

- (Object) to_marchash

Return a marc-hash version of the record



214
215
216
217
218
219
220
221
# File 'lib/marc/record.rb', line 214

def to_marchash
  return {
    'type' => 'marc-hash',
    'version' => [MARCHASH_MAJOR_VERSION, MARCHASH_MINOR_VERSION],
    'leader' => self.leader,
    'fields' => self.map {|f| f.to_marchash}
  }
end

- (Object) to_s

Returns a string version of the record, suitable for printing



276
277
278
279
280
281
282
# File 'lib/marc/record.rb', line 276

def to_s
  str = "LEADER #{leader}\n"
  for field in fields
    str += field.to_s() + "\n"
  end
  return str
end

- (Object) to_xml

Handy method for returning the MARCXML serialization for a MARC::Record object. You’ll get back a REXML::Document object. Really this is just a wrapper around MARC::XMLWriter::encode

  xml_doc = record.to_xml()


199
200
201
# File 'lib/marc/record.rb', line 199

def to_xml
  return MARC::XMLWriter.encode(self, :include_namespace => true)
end