Class: DataFieldImpl

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

Instance Method Summary

Instance Method Details

- (String) [](code)

Get the value of the first subfield of this field with the given code

Parameters:

  • (String) code — 1-character string of the subfield code

Returns:

  • (String) — The value of the first matched subfield

Raises:

  • (ArgumentError)


296
297
298
299
300
301
# File 'lib/marc4j4r.rb', line 296

def [] code
  raise ArgumentError, "Code must be a one-character string, not #{code}" unless code.is_a? String and code.size == 1
  # note that code[0] is just converting the one-character string into an integer 
  # char value that the underlying java can deal with
  self.getSubfield(code[0]).getData
end

- (Object) each

Iterate over the subfields



345
346
347
348
349
# File 'lib/marc4j4r.rb', line 345

def each
  self.getSubfields.each do |s|
    yield s
  end
end

- (Object) indicator1

Get first indicator as a one-character string



335
336
337
# File 'lib/marc4j4r.rb', line 335

def indicator1
  return self.getIndicator1.chr
end

- (Object) indicator2

Get second indicator as a one-character string



340
341
342
# File 'lib/marc4j4r.rb', line 340

def indicator2
  return self.getIndicator2.chr
end

- (Array<String>) sub_values(code, myorder = false)

Get all values from the subfields for the given code or array of codes

Examples:

Quick examples:

 # 260    $a New York, $b Van Nostrand Reinhold Co. $c 1969
rec['260'].sub_values('a') #=> ["New York,"]
rec['260'].sub_values(['a', 'c']) #=> ["New York,", "1969"]
rec['260'].sub_values(['c', 'a']) #=> ["New York,", "1969"]
rec['260'].sub_values(['c', 'a'], true) #=> ["1969", "New York"]

Parameters:

  • (String, Array<String>) code — (Array of?) 1-character string(s) of the subfield code
  • (Boolean) myorder (defaults to: false) — Use the order of subfields that I gave instead of the order they’re in the record

Returns:

  • (Array<String>) — A possibly-empty array of Strings made up of the values in the subfields whose code is included in the given codes. If myorder == true, use the order in which they are passed in; if a code is repeated (ocassionally legal) subfield values will appear first ordered by the passed array, then by order within the document. If myorder is false, just return the values for matching subfields in the order they appear in the field.


321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/marc4j4r.rb', line 321

def sub_values(code, myorder = false)
  code = [code] unless code.is_a? Array
  if myorder
    subs = []
    code.each do |c|
      subs << self.find_all {|s| c == s.code}
    end
    return subs.flatten.map {|s| s.data}
  else
    return self.find_all{|s| code.include? s.code}.map {|s| s.data}
  end
end

- (Object) to_s(joiner = ' ')

Pretty-print

Parameters:

  • (String) joiner (defaults to: ' ') — What string to use to join the subfields
  • (String) The — pretty string


285
286
287
288
289
290
291
# File 'lib/marc4j4r.rb', line 285

def to_s (joiner = ' ')
  arr =  [self.tag + ' ' + self.indicator1 + self.indicator2]
  self.each do |s|
    arr.push s.to_s
  end
  return arr.join(joiner)
end

- (Object) value(joiner = ' ')

Get the concatentated values of the subfields in order the appear in the field

Parameters:

  • (String) joiner (defaults to: ' ') — The string used to join the subfield values


353
354
355
356
# File 'lib/marc4j4r.rb', line 353

def value joiner=' '
  data = self.getSubfields.map {|s| s.data}
  return data.join(joiner)
end

- (Object) via_tagspec(tagspec)



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/marc4j4r.rb', line 260

def via_tagspec (tagspec)
  if (tagspec._subs == nil) or (tagspec._subs.length == 0)
    if (self.value.length > 0)
      return self.value(tagspec.joiner)
    else 
      return nil
    end
  else
    str = []
    # str = ""
    self.each do |s|
      if tagspec._subs[s.getCode]
        str.push s.data if s.data.length > 0
        # str << tagspec.joiner <<  s.data
      end
    end
    return str.join(tagspec.joiner)
    # return str
  end
end