Class: MARC4J4R::Record

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

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • String

    str The record as a MARC binary string

Returns:

  • MARC4J4R::Record The first record encoded in the string



231
232
233
234
235
# File 'lib/marc4j4r/record.rb', line 231

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

Parameters:

  • String

    str The record as a MARC-XML string

Returns:

  • MARC4J4R::Record The first record encoded in the string



241
242
243
# File 'lib/marc4j4r/record.rb', line 241

def Record.from_xml_string str
  return MARC4J4R::Reader.new(StringIO.new(str), :marcxml).first
end

.new_from_hash(hash) ⇒ Object



245
246
247
# File 'lib/marc4j4r/record.rb', line 245

def Record.new_from_hash hash
  return Java::org.marc4j.MarcInJSON.new_from_hash(hash)
end

.new_from_marc_in_json(jsonstring) ⇒ Object



249
250
251
# File 'lib/marc4j4r/record.rb', line 249

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

Parameters:

  • tag (String)

    The tag

Returns:

  • (Field)

    The first matching field, or nil if none. Note that



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

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

#each(&blk) ⇒ Object

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



79
80
81
# File 'lib/marc4j4r/record.rb', line 79

def each(&blk)
  self.getVariableFields.each(&blk)
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

Examples:

originalorder == false

# Given a record that looks like 
# 010    $a 68027371 
# 035    $a (RLIN)MIUG0001728-B 
# 035    $a (CaOTULAS)159818044 
# 035    $a (OCoLC)ocm00001728

r.find_by_tag(['035', '010']).each {|f| puts f.to_s}
# 035    $a (RLIN)MIUG0001728-B 
# 035    $a (CaOTULAS)159818044 
# 035    $a (OCoLC)ocm00001728
# 010    $a 68027371 

Just get all fields for a single tag

ohThirtyFives = r.find_by_tag('035')

Get a bunch of standard identifiers

standardIDs = r.find_by_tag(['022', '020', '010'])

originalorder == true

r.find_by_tag(['035', '010'], true).each {|f| puts f.to_s}
# 010    $a 68027371 
# 035    $a (RLIN)MIUG0001728-B 
# 035    $a (CaOTULAS)159818044 
# 035    $a (OCoLC)ocm00001728

Parameters:

  • tags (String, Array<String>)

    A string (or Array of strings) with the tags you're interested in

  • originalorder (Boolean) (defaults to: false)

    Whether or not results should be presented in the original order within the

Returns:

  • (Array<Field>)

    Either an empty list or a list of one or more matched fields will be returned.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/marc4j4r/record.rb', line 139

def find_by_tag(tags, originalorder = false)
  self.hashify unless @hashedtags and !originalorder
  if !tags.is_a? Array
    return @hashedtags[tags] || []
  end
  if originalorder
    return self.find_all {|f| tags.include? f.tag}
  else
    # puts "Tags is #{tags}: got #{@hashedtags.values_at(*tags)}"
    return @hashedtags.values_at(*tags).flatten.compact
  end
end

#hashifyObject

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

#leaderObject

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.message}")
  end
end

#rehashObject

Force a re-hash



49
50
51
52
# File 'lib/marc4j4r/record.rb', line 49

def rehash
  @hashedtags = nil
  hashify
end

#to_hashObject

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



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

def to_hash
  return Java::org.marc4j.MarcInJSON.record_to_hash(self)
end

#to_marc(encoding = 'UTF-8') ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/marc4j4r/record.rb', line 163

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_jsonObject

Turn it into a marc-in-json JSON string using Jackson



215
216
217
# File 'lib/marc4j4r/record.rb', line 215

def to_marc_in_json
  return Java::org.marc4j.MarcInJSON.record_to_marc_in_json(self)
end

#to_marchashObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/marc4j4r/record.rb', line 178

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_sObject

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_xmlObject

Return the record as valid MARC-XML

Parameters:

  • String

    encoding The encoding to use

Returns:

  • String A MARC-XML representation of the record, including the XML header



158
159
160
# File 'lib/marc4j4r/record.rb', line 158

def to_xml
  return Java::org.marc4j.MarcXmlWriter.record_to_XML(self)
end