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)


267
268
269
270
271
272
# File 'lib/marc4j4r.rb', line 267

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



319
320
321
322
323
# File 'lib/marc4j4r.rb', line 319

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

- (Object) indicator1

Get first indicator as a one-character string



309
310
311
# File 'lib/marc4j4r.rb', line 309

def indicator1
  return self.getIndicator1.chr
end

- (Object) indicator2

Get second indicator as a one-character string



314
315
316
# File 'lib/marc4j4r.rb', line 314

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.


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/marc4j4r.rb', line 292

def sub_values(code, myorder = false)
  unless [Set, Array].include? code.class
    code = [code] 
    # puts "Arrayified for code #{code} / #{code.class}"
  end
  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


256
257
258
259
260
261
262
# File 'lib/marc4j4r.rb', line 256

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


327
328
329
330
# File 'lib/marc4j4r.rb', line 327

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