Class: MARC4J4R::DataField

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

Instance Method Summary collapse

Constructor Details

#initialize(tag = nil, ind1 = ' ', ind2 = ' ') ⇒ DataField

Returns a new instance of DataField.



15
16
17
# File 'lib/marc4j4r/datafield.rb', line 15

def initialize(tag = nil, ind1 = ' ', ind2 = ' ')
  self.oldinit(tag, ind1[0].ord, ind2[0].ord)
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/marc4j4r/datafield.rb', line 23

def == other
  
  basics =  ((self.tag == other.tag) and (self.indicator1 == other.indicator1) and  (self.indicator2 == other.indicator2))
  unless basics
    # puts "Failed basics"
    return false 
  end
  selfsubs  = self.to_a
  othersubs = other.to_a
  
  return false if selfsubs.size != othersubs.size
  
  # puts "#{self} vs #{other}"
  while (selfsubs.length > 0)
    ssf  = selfsubs.shift
    osf  = othersubs.shift
    unless ssf == osf
      # puts "#{ssf} <> #{osf}"
      return false 
    end
  end
  
  if ((selfsubs.size > 0) or (othersubs.size > 0))
    # puts "sizes unequal"
    return false 
  end
  return true
end

#[](code) ⇒ String Also known as: sub, first

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

Parameters:

  • code (String)

    1-character string of the subfield code

Returns:

  • (String)

    The value of the first matched subfield

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
# File 'lib/marc4j4r/datafield.rb', line 67

def [] code
  raise ArgumentError, "Code must be a one-character string, not #{code}" unless code.is_a? String and code.size == 1
  # need to send a char value that the underlying java can deal with
  sub = self.getSubfield(code[0].ord)
  if (sub)
    return sub.getData
  else 
    return nil
  end
end

#controlField?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/marc4j4r/datafield.rb', line 19

def controlField?
  return false
end

#eachObject

Iterate over the subfields



141
142
143
144
145
# File 'lib/marc4j4r/datafield.rb', line 141

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

#indicator1Object Also known as: ind1

Get first indicator as a one-character string



118
119
120
# File 'lib/marc4j4r/datafield.rb', line 118

def indicator1
  return self.getIndicator1.chr
end

#indicator1=(char) ⇒ Object Also known as: ind1=



127
128
129
# File 'lib/marc4j4r/datafield.rb', line 127

def indicator1= char
  self.setIndicator1 char[0].ord
end

#indicator2Object Also known as: ind2

Get second indicator as a one-character string



123
124
125
# File 'lib/marc4j4r/datafield.rb', line 123

def indicator2
  return self.getIndicator2.chr
end

#indicator2=(char) ⇒ Object Also known as: ind2=



131
132
133
# File 'lib/marc4j4r/datafield.rb', line 131

def indicator2= char
  self.setIndicator2 char[0].ord
end

#oldinitObject

Override the initialize to allow creation with just a tag (marc4j only allows either no args or the tag and both indicators)



14
# File 'lib/marc4j4r/datafield.rb', line 14

alias_method :oldinit, :initialize

#sub_values(code = nil) ⇒ Array<String>

Get all values from the subfields for the given code or array of codes code is included in the given codes (or all subfields is code is empty)

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"]

Examples:

Quick examples:

# 260    $a New York, $b Van Nostrand Reinhold Co. $c 1969

Parameters:

  • code (String, Array<String>) (defaults to: nil)

    (Array of?) 1-character string(s) of the subfield code

Returns:

  • (Array<String>)

    A possibly-empty array of Strings made up of the values in the subfields whose



112
113
114
# File 'lib/marc4j4r/datafield.rb', line 112

def sub_values(code=nil)
  return self.subs(code).collect {|s| s.value}
end

#subs(code = false) ⇒ Array<MARC4J4R::SubField] The matching subfields, or an empty array

Get all subfields, optionally restricting to those with a given code

Parameters:

  • code (String, Array<String>) (defaults to: false)

    A (array of?) 1-character strings; the code(s) to collect. Default is all

Returns:

  • (Array<MARC4J4R::SubField] The matching subfields, or an empty array)

    Array<MARC4J4R::SubField] The matching subfields, or an empty array



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

def subs code = false
  unless code
    return self.to_a
  end
  
  # Is it a singleton?
  unless code.is_a? Array
    code = [code]
  end
  
  return self.select {|s| code.include? s.code}
end

#to_s(joiner = ' ') ⇒ Object

Pretty-print

Parameters:

  • joiner (String) (defaults to: ' ')

    What string to use to join the subfields

  • The (String)

    pretty string



55
56
57
58
59
60
61
# File 'lib/marc4j4r/datafield.rb', line 55

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

#value(joiner = ' ') ⇒ Object

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

Parameters:

  • joiner (String) (defaults to: ' ')

    The string used to join the subfield values



149
150
151
152
# File 'lib/marc4j4r/datafield.rb', line 149

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