Class: MARC::Record
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
-
#leader ⇒ Object
the record leader.
Class Method Summary collapse
- .new_from_hash(h) ⇒ Object
-
.new_from_marc(raw, params = {}) ⇒ Object
Factory method for creating a MARC::Record from MARC21 in transmission format.
-
.new_from_marchash(mh) ⇒ Object
Factory method for creating a new MARC::Record from a marchash object.
Instance Method Summary collapse
-
#<<(field) ⇒ Object
alias to append.
-
#==(other) ⇒ Object
For testing if two records can be considered equal.
-
#=~(regex) ⇒ Object
Handy for using a record in a regex: if record =~ /Gravity’s Rainbow/ then print “Slothrop” end.
-
#[](tag) ⇒ Object
You can lookup fields using this shorthand: title = record.
-
#append(field) ⇒ Object
add a field to the record record.append(MARC::DataField.new( ‘100’, ‘2’, ‘0’, [‘a’, ‘Fred’])).
-
#each ⇒ Object
each() is here to support iterating and searching since MARC::Record mixes in Enumerable.
-
#each_by_tag(filter) ⇒ Object
A more convenient way to iterate over each field with a given tag.
-
#fields(filter = nil) ⇒ Object
Provides a backwards compatible means to access the FieldMap.
-
#initialize ⇒ Record
constructor
A new instance of Record.
-
#tags ⇒ Object
Returns an array of all of the tags that appear in the record (not necessarily in the order they appear).
-
#to_dublin_core ⇒ Object
Handy method for returning a hash mapping this records values to the Dublin Core.
-
#to_hash ⇒ Object
Returns a (roundtrippable) hash representation for MARC-in-JSON.
-
#to_marc ⇒ Object
Returns a record in MARC21 transmission format (ANSI Z39.2).
-
#to_marchash ⇒ Object
Return a marc-hash version of the record.
-
#to_s ⇒ Object
Returns a string version of the record, suitable for printing.
-
#to_xml ⇒ Object
Handy method for returning the MARCXML serialization for a MARC::Record object.
Constructor Details
#initialize ⇒ Record
Returns a new instance of Record.
68 69 70 71 72 73 74 75 76 |
# File 'lib/marc/record.rb', line 68 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
#leader ⇒ Object
the record leader
66 67 68 |
# File 'lib/marc/record.rb', line 66 def leader @leader end |
Class Method Details
.new_from_hash(h) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/marc/record.rb', line 236 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 |
.new_from_marc(raw, params = {}) ⇒ Object
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)
163 164 165 |
# File 'lib/marc/record.rb', line 163 def self.new_from_marc(raw, params={}) return MARC::Reader.decode(raw, params) end |
.new_from_marchash(mh) ⇒ Object
Factory method for creating a new MARC::Record from a marchash object
record = MARC::Record->new_from_marchash(mh)
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/marc/record.rb', line 212 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
#<<(field) ⇒ Object
alias to append
88 89 90 |
# File 'lib/marc/record.rb', line 88 def <<(field) append(field) end |
#==(other) ⇒ Object
For testing if two records can be considered equal.
271 272 273 |
# File 'lib/marc/record.rb', line 271 def ==(other) return self.to_s == other.to_s end |
#=~(regex) ⇒ Object
Handy for using a record in a regex:
if record =~ /Gravity's Rainbow/ then print "Slothrop" end
279 280 281 |
# File 'lib/marc/record.rb', line 279 def =~(regex) return self.to_s =~ regex end |
#[](tag) ⇒ Object
You can lookup fields using this shorthand:
title = record['245']
119 120 121 |
# File 'lib/marc/record.rb', line 119 def [](tag) return self.find {|f| f.tag == tag} end |
#append(field) ⇒ Object
81 82 83 84 |
# File 'lib/marc/record.rb', line 81 def append(field) @fields.push(field) @fields.clean = false end |
#each ⇒ Object
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}
104 105 106 107 108 |
# File 'lib/marc/record.rb', line 104 def each for field in @fields yield field end end |
#each_by_tag(filter) ⇒ Object
A more convenient way to iterate over each field with a given tag.
The filter argument can be a string, array or range.
112 113 114 |
# File 'lib/marc/record.rb', line 112 def each_by_tag(filter) @fields.each_by_tag(filter) {|tag| yield tag } end |
#fields(filter = nil) ⇒ Object
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.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/marc/record.rb', line 127 def fields(filter=nil) unless filter @fields.clean = false return @fields end @fields.reindex unless @fields.clean flds = [] if filter.is_a?(String) && @fields.[filter] @fields.[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 |
#tags ⇒ Object
Returns an array of all of the tags that appear in the record (not necessarily in the order they appear).
147 148 149 |
# File 'lib/marc/record.rb', line 147 def return @fields.tag_list end |
#to_dublin_core ⇒ Object
Handy method for returning a hash mapping this records values to the Dublin Core.
dc = record.to_dublin_core()
print dc['title']
193 194 195 |
# File 'lib/marc/record.rb', line 193 def to_dublin_core return MARC::DublinCore.map(self) end |
#to_hash ⇒ Object
Returns a (roundtrippable) hash representation for MARC-in-JSON
228 229 230 231 232 233 234 |
# File 'lib/marc/record.rb', line 228 def to_hash record_hash = {'leader'=>@leader, 'fields'=>[]} @fields.each do |field| record_hash['fields'] << field.to_hash end record_hash end |
#to_marc ⇒ Object
Returns a record in MARC21 transmission format (ANSI Z39.2). Really this is just a wrapper around MARC::MARC21::encode
marc = record.to_marc()
173 174 175 |
# File 'lib/marc/record.rb', line 173 def to_marc return MARC::Writer.encode(self) end |
#to_marchash ⇒ Object
Return a marc-hash version of the record
198 199 200 201 202 203 204 205 |
# File 'lib/marc/record.rb', line 198 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 |
#to_s ⇒ Object
Returns a string version of the record, suitable for printing
260 261 262 263 264 265 266 |
# File 'lib/marc/record.rb', line 260 def to_s str = "LEADER #{leader}\n" for field in fields str += field.to_s() + "\n" end return str end |
#to_xml ⇒ Object
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()
183 184 185 |
# File 'lib/marc/record.rb', line 183 def to_xml return MARC::XMLWriter.encode(self, :include_namespace => true) end |