Class: MARC4J4R::Record
- Inherits:
-
Object
- Object
- MARC4J4R::Record
- Includes:
- Enumerable
- Defined in:
- lib/marc4j4r/record.rb
Class Method Summary collapse
-
.from_string(str, encoding = nil) ⇒ Object
Give a marc record in a string, turn it into an object Note that the normal way of defining this class (self.from_string) didn’t work; I assume it has something to do with the fact that it’s actually jrst aliased to the Java class.
-
.from_xml_string(str) ⇒ Object
Give a marc-xml record in a string, turn it into an object.
- .new_from_hash(hash) ⇒ Object
- .new_from_marc_in_json(jsonstring) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
Show equality.
-
#[](tag) ⇒ Field
Get the first field associated with a tag to mirror ruby-marc, this returns a single field.
-
#each ⇒ Object
Cycle through the fields in the order the appear in the record.
-
#find_by_tag(tags, originalorder = false) ⇒ Array<Field>
Get a (possibly empty) list of fields with the given tag(s).
-
#hashify ⇒ Object
Create a local hash by tag number; makes some stuff faster Called automatically if you use reader.each.
-
#leader ⇒ Object
Get the leader as a string (marc4j would otherwise return Leader object).
-
#leader=(str) ⇒ Object
Set the leader.
-
#rehash ⇒ Object
Force a re-hash.
-
#to_hash ⇒ Object
Turn it into a marc-in-json hashmap.
- #to_marc(encoding = 'UTF-8') ⇒ Object
-
#to_marc_in_json ⇒ Object
Turn it into a marc-in-json JSON string using Jackson.
- #to_marchash ⇒ Object
-
#to_s ⇒ Object
Create a nice string of the record.
-
#to_xml ⇒ Object
Return the record as valid MARC-XML.
Class Method Details
.from_string(str, encoding = nil) ⇒ Object
Give a marc record in a string, turn it into an object Note that the normal way of defining this class (self.from_string) didn’t work; I assume it has something to do with the fact that it’s actually jrst aliased to the Java class
233 234 235 236 237 |
# File 'lib/marc4j4r/record.rb', line 233 def Record.from_string str, encoding=nil s = Java::java.io.ByteArrayInputStream.new(str.to_java_bytes) # return MARC4J4R::Reader.new(StringIO.new(str), :strictmarc, encoding).first return MARC4J4R::Reader.new(s, :strictmarc, encoding).first end |
.from_xml_string(str) ⇒ Object
Give a marc-xml record in a string, turn it into an object
243 244 245 |
# File 'lib/marc4j4r/record.rb', line 243 def Record.from_xml_string str return MARC4J4R::Reader.new(StringIO.new(str), :marcxml).first end |
.new_from_hash(hash) ⇒ Object
247 248 249 |
# File 'lib/marc4j4r/record.rb', line 247 def Record.new_from_hash hash return Java::org.marc4j.MarcInJSON.new_from_hash(hash) end |
.new_from_marc_in_json(jsonstring) ⇒ Object
251 252 253 |
# File 'lib/marc4j4r/record.rb', line 251 def Record.new_from_marc_in_json jsonstring return Java::org.marc4j.MarcInJSON.new_from_marc_in_json(jsonstring) end |
Instance Method Details
#==(other) ⇒ Object
Show equality
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/marc4j4r/record.rb', line 18 def == other return false unless (self.leader == other.leader) self.zip(other) do |so| unless so[0] == so[1] puts "self <> other\n#{so[0]}\n#{so[1]}" return false; end end other.zip(self) do |so| unless so[0] == so[1] puts "#{so[0]}\n#{so[1]}" return false; end end return true end |
#[](tag) ⇒ Field
Get the first field associated with a tag to mirror ruby-marc, this returns a single field
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/marc4j4r/record.rb', line 90 def [] tag if defined? @hashedtags if @hashedtags[tag] return @hashedtags[tag][0] else return nil end else return self.getVariableField(tag) end end |
#each ⇒ Object
Cycle through the fields in the order the appear in the record
79 80 81 82 83 |
# File 'lib/marc4j4r/record.rb', line 79 def each self.getVariableFields.each do |f| yield f end end |
#find_by_tag(tags, originalorder = false) ⇒ Array<Field>
Get a (possibly empty) list of fields with the given tag(s)
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 originalorder == false will use an internal hash and be faster in many cases (see #hashify)
# The results are ordered first by tag as passed in, then by original order within the tag
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/marc4j4r/record.rb', line 141 def find_by_tag(, originalorder = false) self.hashify unless @hashedtags and !originalorder if !.is_a? Array return @hashedtags[] || [] end if originalorder return self.find_all {|f| .include? f.tag} else # puts "Tags is #{tags}: got #{@hashedtags.values_at(*tags)}" return @hashedtags.values_at(*).flatten.compact end end |
#hashify ⇒ Object
Create a local hash by tag number; makes some stuff faster Called automatically if you use reader.each
39 40 41 42 43 44 45 46 |
# File 'lib/marc4j4r/record.rb', line 39 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 |
#leader ⇒ Object
Get the leader as a string (marc4j would otherwise return Leader object)
64 65 66 |
# File 'lib/marc4j4r/record.rb', line 64 def leader self.get_leader.toString end |
#leader=(str) ⇒ Object
Set the leader
70 71 72 73 74 75 76 |
# File 'lib/marc4j4r/record.rb', line 70 def leader= str begin self.set_leader Java::org.marc4j.marc.impl.LeaderImpl.new(str) rescue Java::java.lang.StringIndexOutOfBoundsException => e raise RuntimeError.new("'#{str}' not a legal leader: #{e.}") end end |
#rehash ⇒ Object
Force a re-hash
49 50 51 52 |
# File 'lib/marc4j4r/record.rb', line 49 def rehash @hashedtags = nil hashify end |
#to_hash ⇒ Object
Turn it into a marc-in-json hashmap. Note that this won’t really work like a ruby hash; you need to know what you’re getting, since stuff like #each won’t work.
Better to just use to_marc_in_json if you want a json string
211 212 213 |
# File 'lib/marc4j4r/record.rb', line 211 def to_hash return Java::org.marc4j.MarcInJSON.record_to_hash(self) end |
#to_marc(encoding = 'UTF-8') ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/marc4j4r/record.rb', line 165 def to_marc encoding='UTF-8' # begin s = Java::java.io.ByteArrayOutputStream.new writer = org.marc4j.MarcPermissiveStreamWriter.new(s, encoding) writer.write(self) return s.to_string # writer.close # @marcbinary = s.to_string # return @marcbinary # rescue # # "Woops! to_marc failed for record #{self['001'].data}: #{$!}" # "Whoops! Failed: #{$!}" # end end |
#to_marc_in_json ⇒ Object
Turn it into a marc-in-json JSON string using Jackson
217 218 219 |
# File 'lib/marc4j4r/record.rb', line 217 def to_marc_in_json return Java::org.marc4j.MarcInJSON.record_to_marc_in_json(self) end |
#to_marchash ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/marc4j4r/record.rb', line 180 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 |
#to_s ⇒ Object
Create a nice string of the record
55 56 57 58 59 60 61 |
# File 'lib/marc4j4r/record.rb', line 55 def to_s arr = ['LEADER ' + self.leader] self.each do |f| arr.push f.to_s end return arr.join("\n") end |
#to_xml ⇒ Object
Return the record as valid MARC-XML
160 161 162 |
# File 'lib/marc4j4r/record.rb', line 160 def to_xml return Java::org.marc4j.MarcXmlWriter.record_to_XML(self) end |