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


163
164
165
166
167
168
169
# File 'lib/marc4j4r.rb', line 163

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

- (Object) each

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



152
153
154
155
156
# File 'lib/marc4j4r.rb', line 152

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

- (Object) find_by_tag(tags, originalorder = false)



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/marc4j4r.rb', line 205

def find_by_tag(tags, originalorder = false)
  if !tags.is_a? Array
    tags = [tags]
  end
  if (originalorder == false and @hashedtags == nil)
    self.hashify
  end
  if originalorder
    return self.find_all {|f| tags.include? f.tag}
  else
    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



129
130
131
132
133
134
135
# File 'lib/marc4j4r.rb', line 129

def hashify 
  @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)



147
148
149
# File 'lib/marc4j4r.rb', line 147

def leader
  self.get_leader.toString
end

- (Object) to_s

Create a nice string of the record



138
139
140
141
142
143
144
# File 'lib/marc4j4r.rb', line 138

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


222
223
224
225
226
227
228
229
# File 'lib/marc4j4r.rb', line 222

def to_xml
  return @xml if @xml
  @xml = java.io.StringWriter.new
  res = javax.xml.transform.stream.StreamResult.new(@xml)
  writer =  org.marc4j.MarcXmlWriter.new(res)
  writer.write(self)
  return @xml.toString
end