Class: RecordImpl
- Inherits:
-
Object
- Object
- RecordImpl
- Includes:
- Enumerable
- Defined in:
- lib/marc4j4r.rb
Overview
Open up RecordImpl to add some sugar, including Enumberable as well
Instance Method Summary
- - (Field) [](tag) Get the first field associated with a tag.
- - (Object) each Cycle through the fields in the order the appear in the record.
- - (Array<Field>) find_by_tag(tags, originalorder = false) Get a (possibly empty) list of fields with the given tag(s).
- - (Object) hashify Create a local hash by tag number; makes some stuff faster Called automatically if you use reader.each.
- - (Object) leader Get the leader as a string (marc4j would otherwise return Leader object).
- - (Object) to_marc
- - (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/.
- - (Object) to_s Create a nice string of the record.
- - (Object) to_xml Return the record as valid MARC-XML.
Instance Method Details
- (Field) [](tag)
Get the first field associated with a tag
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/marc4j4r.rb', line 263 def [] tag if defined? if [tag] return [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)
314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/marc4j4r.rb', line 314 def find_by_tag(, originalorder = false) self.hashify unless and !originalorder if !.is_a? Array return [] || [] end if originalorder return self.find_all {|f| .include? f.tag} else # puts "Tags is #{tags}: got #{@hashedtags.values_at(*tags)}" return .values_at(*).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 # don't do it more than once = {} self.getVariableFields.each do |f| [f.tag] ||= [] [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/
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
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 |